Sync Hooks

Control real-time synchronization between WordPress and your search engine.

Sync Control

celersearch_should_sync_post

Control whether a post should be synced.

PHP
add_filter('celersearch_should_sync_post', function($should_sync, $post, $index) {
    // Don't sync posts marked as excluded
    if (get_post_meta($post->ID, 'exclude_from_search', true)) {
        return false;
    }

    // Don't sync password-protected posts
    if (!empty($post->post_password)) {
        return false;
    }

    // Only sync specific post types to this index
    if ($index->get_slug() === 'blog-posts') {
        return $post->post_type === 'post';
    }

    return $should_sync;
}, 10, 3);
ParameterTypeDescription
$should_syncboolDefault sync decision
$postWP_PostThe post being synced
$indexBaseIndexTarget index

celersearch_should_delete_post

Control whether a post deletion should be processed.

PHP
add_filter('celersearch_should_delete_post', function($should_delete, $post, $index) {
    // Log all deletions
    error_log("Deleting post {$post->ID} from index {$index->get_slug()}");

    return $should_delete;
}, 10, 3);

celersearch_should_sync_order

Control WooCommerce order sync.

PHP
add_filter('celersearch_should_sync_order', function($should_sync, $order, $index) {
    // Only sync completed orders
    if (!in_array($order->get_status(), ['completed', 'processing'], true)) {
        return false;
    }

    return $should_sync;
}, 10, 3);

celersearch_should_delete_order

Control WooCommerce order deletion.

PHP
add_filter('celersearch_should_delete_order', function($should_delete, $order, $index) {
    return $should_delete;
}, 10, 3);

Meta Key Watching

celersearch_watch_post_meta_keys

Define which meta key changes trigger re-indexing.

PHP
add_filter('celersearch_watch_post_meta_keys', function($keys, $post_id) {
    // Default is only '_thumbnail_id'

    // Add custom fields that affect search
    $keys[] = 'price';
    $keys[] = 'sale_price';
    $keys[] = 'product_brand';
    $keys[] = 'is_featured';

    // Add ACF fields
    $keys[] = 'custom_description';
    $keys[] = 'product_specifications';

    return $keys;
}, 10, 2);

Performance

Only add meta keys that actually affect search results. Each watched key triggers re-indexing on change.


Custom Observers

celersearch_observer_for_index

Provide custom observer for custom index types.

PHP
add_filter('celersearch_observer_for_index', function($observer, $index) {
    // Custom observer for events index
    if ($index instanceof \MyPlugin\Indices\EventsIndex) {
        return new \MyPlugin\Observers\EventObserver($index);
    }

    // Custom observer for members index
    if ($index instanceof \MyPlugin\Indices\MembersIndex) {
        return new \MyPlugin\Observers\MemberObserver($index);
    }

    return $observer;
}, 10, 2);

Global Toggle

celersearch_real_time_sync_enabled

Enable or disable real-time sync globally.

PHP
// Disable during migrations
add_filter('celersearch_real_time_sync_enabled', function($enabled) {
    if (defined('DOING_MIGRATION') && DOING_MIGRATION) {
        return false;
    }
    return $enabled;
});

// Disable on specific environments
add_filter('celersearch_real_time_sync_enabled', function($enabled) {
    return wp_get_environment_type() !== 'staging';
});

celersearch_queue_enabled

Control queue system.

PHP
// Force direct sync (no queue)
add_filter('celersearch_queue_enabled', '__return_false');

// Conditional queue usage
add_filter('celersearch_queue_enabled', function($enabled) {
    // Disable queue during imports
    return !defined('WP_IMPORTING');
});

Split Records

celersearch_max_post_splits

Control maximum record splits per post.

PHP
add_filter('celersearch_max_post_splits', function($max) {
    // Allow more splits for long-form content
    return 20;  // Default: 10
});

This affects deletion - ensures all split records are removed when a post is deleted.


Complete Example

Comprehensive sync customization:

PHP
<?php
// 1. Control what gets synced
add_filter('celersearch_should_sync_post', function($sync, $post, $index) {
    // Skip drafts for public index
    if ($index->get_slug() === 'public-posts' && $post->post_status !== 'publish') {
        return false;
    }

    // Skip excluded posts
    if (get_post_meta($post->ID, 'no_search', true)) {
        return false;
    }

    return $sync;
}, 10, 3);

// 2. Watch custom meta keys
add_filter('celersearch_watch_post_meta_keys', function($keys, $post_id) {
    $post_type = get_post_type($post_id);

    // Product-specific keys
    if ($post_type === 'product') {
        $keys = array_merge($keys, [
            '_price',
            '_sale_price',
            '_stock_status',
            '_featured',
        ]);
    }

    // Event-specific keys
    if ($post_type === 'event') {
        $keys = array_merge($keys, [
            'event_date',
            'event_venue',
            'event_status',
        ]);
    }

    return $keys;
}, 10, 2);

// 3. Custom observer for custom index
add_filter('celersearch_observer_for_index', function($observer, $index) {
    if ($index instanceof MyEventIndex) {
        return new MyEventObserver($index);
    }
    return $observer;
}, 10, 2);

// 4. Disable during bulk operations
add_filter('celersearch_real_time_sync_enabled', function($enabled) {
    // Check for bulk edit
    if (isset($_REQUEST['bulk_edit'])) {
        return false;
    }

    // Check for WP-CLI
    if (defined('WP_CLI') && WP_CLI) {
        return false;
    }

    return $enabled;
});

Debugging Sync Issues

Add logging to troubleshoot sync:

PHP
add_filter('celersearch_should_sync_post', function($sync, $post, $index) {
    error_log(sprintf(
        '[CelerSearch] Sync decision for post %d (%s) to %s: %s',
        $post->ID,
        $post->post_type,
        $index->get_slug(),
        $sync ? 'YES' : 'NO'
    ));

    return $sync;
}, 999, 3);  // High priority to run last