Wordpress get_posts()?


Algolia looks like an amazing product!

I just have a question that I could not find an answer to in the documentation or online forums.

I know that Algolia does amazing things with the WordPress Search box, but I’m looking for some clarity regarding something else:

I have a Wordpress site that uses get_posts() with a custom query (using custom taxonomies) to customise which posts I show during the loop. I want to use Algolia to intercept the get_posts() call and provide the results from Algolia rather than the default WordPress get_posts behaviour.

Does Algolia do this?


Michael Rodrigues

1 Like

Hi @microd,

The behaviour you describe is what we do in our plugin when you select to replace the backend search with Algolia.
The logic is in this file https://github.com/algolia/algoliasearch-wordpress/blob/develop/includes/class-algolia-search.php

You could probably inspire from that to achieve your goals.

Keep us posted :wink:

1 Like

Thank you for your reply.

I have:

  • Signed up to Algolia
  • Installed the Wordpress Plugin
  • Set the Search Page to “Use Algolia in the backend”
  • Indexed my content

If I perform a normal Wordpress Search, I can see the requests pop up in Latest Operations in my Algolia dashboard. That’s great.

My get_posts queries, though, are not showing up in the Latest operations. According to the above reply I would expect these also to show up.

Am I doing something wrong?

Michael Rodrigues

@microd by default we only hook into the query of the search page, not on every get_posts call.
If you want to do so, you will have to do a custom implementation and get inspired by the Class I shared.

Interesting use case, let us know how it goes,