Hi,
I generating search results as WordPress Archive for search with different post types. Now I want to add a post type filter in the front end. How to achieve that? Please help.
See the search result page here: hidden link
Thanks.
Custom search with Views and WordPress Archives doesn't support filtering by standard post fields such as post_type.
However, the legacy editor for Views and Archives did allow you to insert a filter on the front end that is nominally connected to some query field which isn't actually used, so that you can then add some custom code to modify the query based upon what is selected in the filter.
So in the filter section of your custom archive, you can add a wpv-control shortcode like so:
You will likely want to wrap this with some markup for a form label etc.
Then you would need to add some code to modify the query if any filter value is provided. Because this is an archive (rather than a View) you will need to use the core pre_get_posts hook, like so:
Well it works fine but have some issues.
1. The reset button is not working. You can check with the previous instance I have given above.
2. The check box once I click stays selected even if I uncheck it and select another one for fresh search filtering.
3. The url keeps on growing with every submit button I click.
4. The archive is coming up with a pagination without my control.
Even if I have mentioned some problems earlier (4 points), I found that "AJAX results update when visitors change any filter values" works fine. So I would rather stick to this style.
But the problem is the filter is removed as soon as I go to other pages of the search results. Thanks for your help.
Hello and thank you for contacting Toolset Support.
If you want to have the filters selected after returning from a result page, you will need to reply on URL parameters. Even if you use the AJAX search, you can activate browser history in Toolset Settings. However, there may still be some issues with the post type filter. I'd like to take a closer look at your website and check this further. Your next reply will be private to let you share credentials safely. ** Please take a database backup before sharing credentials. **
Unfortunately, I am unable to log in. I get redirected to the profile page and I am asked to perform a 2FA authentication, but I can't enter the code from Google Authenticator, the page just does not let me enter the 4th 5th, and 6th numbers of the code.
Thank you, I can log in now and see the archive template.
Regarding the pagination, the second one is added by the theme, and I can't really tell, right now, how to remove it. But you can hide it with the following CSS code in the archive template:
div#primary>div.ast-pagination {
display: none;
}
Regarding the URL issues, I am afraid, any solution would be very hacky and may work in cases and break cases. The best solution I can think of is to mirror the post type into a custom field or a custom taxonomy. This way you will get working filters that handle the URL parameters correctly.
Such mirroring can be done by a snippet of code that will sync the post type, into the custom field or the custom taxonomy, after each update of a post. Basically after the save_post action https://developer.wordpress.org/reference/hooks/save_post/
Let me know if you need help with such a snippet.