Skip Navigation

[Resolved] Exclude specific posts from search on an archive page

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 19 replies, has 3 voices.

Last updated by Waqar 11 months, 2 weeks ago.

Assisted by: Waqar.

Author
Posts
#2666931

Hi,

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.

Is there a way to achieve that?

Thank you in advance

Luca

#2666951

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Luca

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.

In the official documentation they have an example of targeting a particular custom post type archive, here: https://developer.wordpress.org/reference/hooks/pre_get_posts/#change-the-number-of-posts-per-page-by-post-type

In your case you would need to set the query args for the post__not_in property, see https://developer.wordpress.org/reference/classes/WP_Query/parse_query/

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?

If that were the case you would add a tax_query argument to the query, see https://developer.wordpress.org/reference/classes/wp_tax_query/__construct/

#2667143

Hi Nigel,

thank you for your support.

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

Any idea?

Thank you

#2667563

Hi Luca,

Thank you for sharing this update.

Here is an example of a code snippet, involving the 'pre_get_posts' filter, as Nigel recommended earlier:


function custom_post_type_archive_autoclaves( $query ) {
	if ( ! is_admin() && is_post_type_archive('autoclaves') ) {
		$taxquery = array(
			array(
				'taxonomy' => 'taxonomy-slug',
				'field' => 'term_id',
				'terms' => array( 123 ),
				'operator'=> 'NOT IN'
			)
		);
		$query->set( 'tax_query', $taxquery );

	}
}
add_action( 'pre_get_posts', 'custom_post_type_archive_autoclaves' );

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' )

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

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/

regards,
Waqar

#2667579

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?

Thank you again

#2667831

Thanks for writing back.

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.

#2667841

Thank you very much Waqar.
That is great news.

I am afraid I did not get what is the "special term's ID, that should be excluded."
Are you referring to the tag_id?

#2667843

You're very welcome and glad I could help.

Your understanding is correct and I meant 'tag_id' of the term whose results should be excluded.

#2668399

Thank you Waqar, I have added the code with the correct slug and id, but the page is then empty, and the main menu is broken.

I'd say that the code is the same you have provided and the fields I had to update are correct...

//eliminar equipos médicos de resultados de búsquedas en página filtros
function custom_post_type_archive_autoclaves( $query ) {
    if ( ! is_admin() && is_post_type_archive('autoclaves') ) {
        $taxquery = array(
            array(
                'taxonomy' => 'autoclaves-medicas',
                'field' => '96',
                'terms' => array( 123 ),
                'operator'=> 'NOT IN'
            )
        );
        $query->set( 'tax_query', $taxquery );
 
    }
}
add_action( 'pre_get_posts', 'custom_post_type_archive_autoclaves' );

Do you know what am I doing wrong?
hidden link

UPDATE: I have just noticed a message in the error log related to this. This is the message:

mod_fcgid: stderr: Toolset Views: The WordPress Archive Helper post had to be recreated for the WordPress Archive: "Series de autoclaves Raypa"

There is also another error that wasn not there before adding the code to the functions.php archive, so it might be related as well:

Broken pipe: [client 93.189.95.142:60890] mod_fcgid: ap_pass_brigade failed in handle_request_ipc function

I hope this can give you some hint about what is happening...

#2668489

In your code, can you please change the line:


'field' => '96',

To:


'field' => 'term_id',

And the line:


'terms' => array( 123 ),

To:


'terms' => array( 96 ),

This should do the trick.

#2668493

Ops that was a stupid mistake, sorry.

Anyway, I have made the correction, but nothing has changed. It's also appearing the page title, although I have disabled it...

#2668845

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.

#2669321

Hi, I have share the access details. I guess when the reply is private you don't receive a notification.

As said in that message this morning, I have tried the steps suggested but nothing changes I am afraid...

#2669527

The access details worked, earlier but now I'm getting the 'Error: Invalid username or incorrect password.' message.

Have you made any changes to the website or this user?

#2669537

Sorry about that. I suppose it's a password problem. Can you please try once more with the same password I gave you?