Skip Navigation

[Resolved] Excluding custom post type from search results

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Marcel 1 year, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2573729

Hi,

I've created a custom search archive using Toolset, but I would like to exclude a certain post type (Blocksy's content blocks) from the search results.

#2574099

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know in what mode you created the archive. Are you using blocks or you are using the classic views?

Can you please share edit screenshot of your archive where I can see to what content type you assigned your archive?

#2574111

Hi Minesh,

I'll do even better: see this video 🙂
hidden link

If you want, I can give you admin access too.

#2574141

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Based on the screenshot you shared I can see that you are using the Toolset block based archive.

With the block based archive currently there is no feature what post types should be included/excluded.

In such a case you will have to use the WordPress hook: pre_get_posts

For example:
- You can add the following code to "Cudeom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

function func_search_archive_exclude_posts($query) {
    if (!$query->is_admin && $query->is_search) {
        $allowed_post_types = array('post', 'page', 'book');
        $query->set('post_type',$allowed_post_types );
    }
    return $query;
}
add_action('pre_get_posts', 'func_search_archive_exclude_posts');

Where:
- you can adjust the post type slugs you want to allow with $allowed_post_types.

#2574157

Hi Minesh,

Thanks, that seems too work 🙂

Just one more question. I've added a single field quering the post type to the view, but that shows the post type slug. I would rather have it display the post type's user friendly name, since I can edit that in Toolset's settings. Can't edit the slug for pages and posts.

#2574169

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that the solution I shared help you to resolve your issue.

As per our support policy - we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery. May I kindly ask you to open a new ticket with every new question you may have. Thank you for understanding.

#2574177

My issue is resolved now. Thank you!