Skip Navigation

[Resolved] Search Result Page for all CPT, grouped by post type

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a search results page that contains all custom post types, with results organized by post type.

Solution: You can create multiple Views, one for each post type, and insert them in the search results WordPress Archive. Add custom code that applies the search term to each View using the wpv_filter_query API.

add_filter( 'wpv_filter_query', 'add_url_query_search_term_filter',99,3 );
function add_url_query_search_term_filter( $query_args, $views_settings, $view_id) {
  $view_ids = array( 12345 );
  if (in_array($view_id, $view_ids)){
    $query_args['s'] = isset($_GET['s']) ? $_GET['s'] : '';
  }
  return $query_args;
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Christian Cox 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#906146

Hey

We have a search widget on the Home Page. We want that when a search query is submitted, the search result page will list all the posts, including CPT that have the search term in their title or body. We have 3 CPT (in addition to the regular post and page):

1. Job (via the wp job manager plugin).
2. Event (via the Event Calander PRO plugin).
3. Organisation (CPT via types).

The Page should be sectioned according to post types, this is the rough format:

POSTs
----------------------------
Title
Text

Title
Text

JOBS
----------------------------
Title
Text

Title
Text

Organisations.
----------------------------
Title
Text

Title
Text
...

Is this possible, and if so how?

Thanks!

#906302

Hi, Toolset's WordPress Archives can be used to design the search results page, but the results are not organized by post type. You can use conditional HTML to style each post type differently, and you can sort them by post type, but there's not much customization available for that type of sorting. For example, you can't add section headers that break up the different post types.

You can accomplish something similar to your mockup with multiple Views nested in a WordPress Archive, and get much more control over the design. It will require a bit of API programming, but I can offer some help. Here's how I would approach this:
- Create a View of Posts and build the Loop Output to show the results list with title, link, and excerpt or contents as needed. Do not add any filters to this View.
- Create a WordPress Archive and assign it to the Search Results archive. In the Loop Output of the archive, insert the View of Posts just before the wpv-items-found tag. Remove everything inside the wpv-loop tags and everything inside the wpv-no-items-found tags.
- Add the following code to your child theme's functions.php file to filter the Views automatically using the search term passed into the search archive page URL:

add_filter( 'wpv_filter_query', 'add_url_query_search_term_filter',99,3 );
function add_url_query_search_term_filter( $query_args, $views_settings, $view_id) {
  $view_ids = array( 12345 );
  if (in_array($view_id, $view_ids)){
    $query_args['s'] = isset($_GET['s']) ? $_GET['s'] : '';
  }
  return $query_args;
}

- Replace 12345 with the numeric ID of your View of Posts
- Try out the search widget and see if the posts search is working as expected. If it is, then you can begin creating duplicates of the View of Posts to display results from each different post type.
- Insert all the duplicate Views in the WordPress Archive just before the wpv-items-found tag.
- Modify the code in functions.php to include a comma-separated list of all the numeric View IDs you created:

$view_ids = array( 12345, 67890, 98765 );

Let me know if you have questions about this approach.

#906308

I should also mention that pagination is not supported in this approach, so it's not really practical if you have a large amount of content.