Posted on

Indexing Polylang Pro synced posts

Polylang Pro has a feature to sync translations between posts. The problem with this and Relevanssi is that Relevanssi does not index the changes to the synced posts. With this filter function added to your site, Relevanssi will also index the synced posts:

add_action( 'save_post', 'index_post_in_all_languages' );
public function index_post_in_all_languages( $post_id ) {
    if ( ! function_exists( 'pll_languages_list' ) ||
     ! function_exists( 'relevanssi_insert_edit' ) ) {
        return;
    }
    $all_languages      = pll_languages_list( ['fields' => 'slug'] );
    $post_language      = pll_get_post_language( $post_id, 'slug' );
    $languages_to_index = array_diff( $all_languages, [ $post_language] );
    foreach ( $languages_to_index as $lang ) {
        $post_id_to_index = pll_get_post( $post_id, $lang );
        if ( $post_id_to_index > 0 ) {
            relevanssi_insert_edit( $post_id_to_index );
        }
    }
}

Thanks to Nico von Niederhäusern / Cubetech.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published.