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!
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.
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.