-
Notifications
You must be signed in to change notification settings - Fork 58
Automated Sync
Understand the indexing flow and the generated operations.
By default, this plugin will ensure that your WordPress content that is flagged for indexing stays in sync with Algolia.
Automatic synchronization is supported for 3 types of content:
- Post Types,
- Taxonomies,
- Users.
Each content type has its own indexing flow.
Indexing has been automated as much as possible. This plugin listens for events like 'post was saved' to push the new data to Algolia.
Posts are synchronized when:
- A post was created or updated
- A post was deleted
- The featured image was changed
Taxonomy terms are synchronized when:
- A term was created or updated
- A term was deleted
Users are synchronized when:
- A user was created or updated
- A term was deleted
- A post was created or updated
- A post was deleted
Data is synchronized without checking whether or not the update changes the content. This ensures consistency and easier extensibility.
The existence of an indexing task does not necessarily mean that the data will be pushed to Algolia.
Before each post, term and user indexing, it is determined whether on not the post should be indexed. If it should, then the data for the post is re-pushed to Algolia. Otherwise, the data is removed from the index.
Default decisions:
Content Type | Rule |
---|---|
Searchable Post | A post is only indexed when it is in the published state and its post type is not excluded_from_search . |
Post | A post is only indexed when it is in the published state and it has no password. |
Term | A term is only indexed if it has been assigned to at least 1 post (count > 0). |
User | A user is only indexed if it has authored at least 1 post. |
You can hook into the indexing decision making for searchable posts, posts, terms and users by using the algolia_should_index_searchable_post
, algolia_should_index_post
, algolia_should_index_term
and algolia_should_index_user
filters.
Here is an example for filtering user indexing:
<?php
/**
* @param bool $should_index
* @param WP_User $user
*
* @return bool
*/
function filter_user( $should_index, $user ) {
if ( false === $should_index ) {
return false;
}
return $user->ID !== 1;
}
add_filter( 'algolia_should_index_user', 'filter_user', 10, 2 );
In the above example, a user with ID 1 would never get indexed.
Also note how the filter returns false
if the decision has already been made not to index the content.
Another example option to exclude posts from the searchable_posts index is where the noindex option from the Yoast SEO plugin is set to "noindex"
This example assumes you are using the Yoast SEO plugin for WordPress
<?php
/**
* Don't index pages where the robot index option
* in the Yoast SEO plugin is set to noindex.
*
* @param bool $should_index
* @param WP_Post $post
*
* @return bool
*/
function filter_post( $should_index, WP_Post $post )
{
if ( false === $should_index ) {
return false;
}
return get_post_meta($post->ID, '_yoast_wpseo_meta-robots-noindex', true) == 1 ? false : true;
}
// Hook into Algolia to manipulate the post that should be indexed.
add_filter( 'algolia_should_index_searchable_post', 'filter_post', 10, 2 );
Exclude post types from searchable_posts_index
In this example, the page
post type is excluded from the searchable_posts
index.
<?php
/**
* @param bool $should_index
* @param WP_Post $post
*
* @return bool
*/
function exclude_post_types( $should_index, WP_Post $post )
{
if ( false === $should_index ) {
return false;
}
// Add all post types you don't want to make searchable.
$excluded_post_types = array( 'page' );
return ! in_array( $post->post_type, $excluded_post_types, true );
}
// Hook into Algolia to manipulate the post that should be indexed.
add_filter( 'algolia_should_index_searchable_post', 'exclude_post_types', 10, 2 );
Add post type to searchable_posts_index
By default, the plugin will only index post types that are not flagged as excluded from search.
If you want to manually determine the post types to index, you can use the algolia_searchable_post_types
filter:
<?php
/**
* @param array $post_types
*
* @return array
*/
function exclude_searcbable_post_types( $post_types ) {
$post_types[] = 'custom_post_type';
return $post_types;
}
add_filter( 'algolia_searchable_post_types', 'exclude_searcbable_post_types' );
Exclude a post by it's ID
The following snippet excludes the post with ID 18517
.
<?php
/**
* @param bool $should_index
* @param WP_Post $post
*
* @return bool
*/
function filter_post( $should_index, WP_Post $post )
{
if ( 18517 === $post->ID ) {
return false;
}
return $should_index;
}
// Hook into Algolia to manipulate the post that should be indexed.
add_filter( 'algolia_should_index_searchable_post', 'filter_post', 10, 2 );
add_filter( 'algolia_should_index_post', 'filter_post', 10, 2 );
Every time a change of your content is detected, see previous section, the data is synchronized.
You can disable automatic synchronization by removing all of the watchers using the algolia_changes_watchers
filter:
<?php
/**
* @param array $watcehrs
*
* @return array
*/
function remove_all_changes_watchers( $watchers ) {
return [];
}
add_filter( 'algolia_changes_watchers', 'remove_all_changes_watchers' );