I have an archive page for a custom post type and 3 search filters: hidden link
We have some products within that CPT that are almost identical as others ones, a special certification being the only difference, we would like to hide those specific posts from the search results, in order to avoid having 2 identical posts listed at the same time.
Also, our idea would be to add a checkbox that allows the search for that posts that would be excluded from the main search. These posts have a specific taxonomy applied to them that might be used to list them apart.
Custom archives made with Toolset are built on top of the underlying WordPress archive query, and to modify that query (in your case to exclude certain posts from the results) you would use the WordPress pre_get_posts hook.
You would provide the IDs of the posts you wanted to exclude from the results.
I'm not sure I follow what you are describing in "Also, our idea...".
Do you mean that when editing individual posts you want to add a checkbox to say whether the post should appear in the archive or not? Or the posts to be excluded have a specific taxonomy term applied, and that could be used to exclude them?
Please forget about the second part (also, our idea...). I am thinking about a different approach:
As I said, there is one type of product that has an ISO certification, and that is the only difference with other products. We are trying not to show the 2 versions at the same time.
In order to achieve that, I have just added a taxonomy value within one of the taxonomy groups ("trazabilidad") that is only used by the "special", ISO-labelled products. This way, when that option is selected, only the labelled products are shown.
This allows me to make it like I want, but only when that option is selected. Otherwise, selecting other options will still show in the same results the similar products side by side.
I am not sure if there is a not-too-difficult way to solve this.
One acceptable way would be to make it so that the iso-labelled product appear at the end of the results, but I am not sure if this is something easy to do...
Note: You'll replace 'taxonomy-slug' and '123' with your website's target taxonomy slug and the special term's ID, that should be excluded.
( you should use a special taxonomy term with posts which you don't want to be included in this post type archive for 'autoclaves' )
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors: https://toolset.com/contractors/
Thank you very much Waqar,
the problem is that we would like to exclude a specific taxonomy by default, but then put it back into the search results only when that specific taxonomy is selected in the checkbox options.
I guess the code you've so kindly provided wouldn't work right?
The taxonomy slug will remain excluded even though that very taxonomy was selected as a search field is that so?
I've tested a code on my website and can confirm that when taxonomy search field will be used, it will be override the tax query set by the custom code.
The custom code will only exclude the results from the specific term, when taxonomy search has not been performed.
That is odd, as the same function is working on my test website.
To troubleshoot this, I'll suggest the following steps:
1. Please make sure that WordPress, active theme, and plugins are all updated to the latest versions.
2. It would be interesting to test this with all non-Toolset plugins disabled and a default theme like Twenty Twenty-One.
If it's fixed, you can start adding the disabled items, one by one, to narrow down to a possible conflicting one.
3. In case the issue still persists, I'll need to see how this archive and the post type are set up in the admin area. You're welcome to share temporary admin login details in reply to this message.
I'll also need your permission to download a clone/snapshot of the website, in case it needs to be investigated on a different server.
Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.