0

I need to display results of three different search queries on one page. More precisely, there is a searchbox on my page where a user enters a search phrase. Once the search phrase is submitted, I need to take the generated WP search query and modify it to build three different custom queries.

The results of these three queries will be then displayed on a page in separate blocks.

So for example, a user searches for the 'Honda' phrase. Then he gets the results of three search queries:

  1. 'Honda' in custom posts called 'Cars',
  2. 'Honda' in custom posts called 'Motorbikes',
  3. 'Honda' in custom posts called 'Tracks',

I know that the simplest way to modify a WP search query is to use the pre_get_posts action hook, but I'm not sure how I can use it to create three separate queries.

Your advice will be much appreciated.

3

You can use pre_get_posts to modify the main query, but you will need to create new queries for the other searches. Use get_query_var('s') to get the search terms and create new WP_Query objects to perform your search.

Without more information it is hard to provide much more detail to the answer.

  • Thanks you for this suggestion. I'd like, however, to aviod building three separate queries from scratch, and modify the main search query as I would with query->set() when using pre_get_posts() hook. I will soon update my question with more details. – luqo33 May 28 '15 at 10:31
  • Which is exactly what I said-- "You can use pre_get_posts to modify the main query, but you will need to create new queries for the other searches." – s_ha_dum May 28 '15 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.