Search and replace all indexed ULRs for headless WordPress

Hello :wave: we have a headless WordPress site that we’re trialling Algolia on. The site has two domains:

We’ve connected Algolia to site and successfully have search working on the However, the search results on are being returned as This isn’t surprising, Algolia is pulling its data from the backend site.

My question is; how do we perform an automated search and replace on the indexed results, to change them to in Algolia? Any help appreciated.


You could search/replace in the url at indexing time:

Hey @jerome.schneider thanks for responding. I was looking at this yesterday but I wasn’t clear on how this would help in this situation.

Is it the 'objectID' => implode('#', [$post->post_type, $post->ID]), that I need to be looking at here?

Hey @dan6 actually it’s the 'url' => get_post_permalink($post->ID), representing the url of your post, that you would need to change to make the urls as desired.

Thanks again, I’m just not clear on what needs to be done in this snippet to change the domain. Is there any further documentation on what needs to be done with this?

I’m not sure how I’d need to edit 'url' => get_post_permalink($post->ID) to change to being returned in the search results.

Hey @dan6, you’d need to change the content of the url, something like this:

'url' => str_replace('', '', get_post_permalink($post->ID)),

Really appreciate your help on this @jerome.schneider this is the solution we’ve implemented for now:

 * Algolia URL Filter for use on a decoupled WordPress 
class XYZ_Algolia_URL_Filter {

    const LIVE_URL = '';

     * Class constructor.
    public function __construct() {
        add_filter( 'algolia_post_shared_attributes', array( $this, 'replace_url' ), 10, 1 );
        add_filter( 'algolia_searchable_post_shared_attributes', array( $this, 'replace_url' ), 10, 1 );

     * Convert staging URL to live URL.
     * @param array $shared_attributes from filter.
     * @return array The array after replacement.
    public function replace_url( $shared_attributes ) {
        $live_url          = self::LIVE_URL;
        $staging_url       = site_url();
        $shared_attributes['permalink'] = str_replace( $staging_url, $live_url, $shared_attributes['permalink'] );
        return $shared_attributes;
new XYZ_Algolia_URL_Filter();