Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop wordpress search showing a custom post type

I have one custom post type I use for some text blocks on a page built using uncode theme. I need these blocks to be public so they display on the page but I want to stop them appearing in search results.

The search.php isn't like a normal wordpress search file, it is the uncode-theme file and doesn't have normal queries in I don't think so I'm thinking I need a function maybe?

Can anyone please advise how to achieve this?

The CPT is 'staticcontent'

Thanks!

like image 640
igloobob Avatar asked Oct 03 '16 17:10

igloobob


1 Answers

The answer here depends on whether you're creating the CPT via your own code, or if another plugin is creating the CPT. See this link for a great explanation of both approaches:

http://www.webtipblog.com/exclude-custom-post-type-search-wordpress/

The basic gist is this:

If you're creating your own CPT, you can add an argument to the register_post_type() call of 'exclude_from_search' => true

If another plugin / theme is creating the CPT, you need to set this exclude_from_search variable later on, as part of a filter to the CPT, as such:

// functions.php

add_action( 'init', 'update_my_custom_type', 99 );

function update_my_custom_type() {
    global $wp_post_types;

    if ( post_type_exists( 'staticcontent' ) ) {

        // exclude from search results
        $wp_post_types['staticcontent']->exclude_from_search = true;
    }
}
like image 138
Greg Burkett Avatar answered Oct 10 '22 00:10

Greg Burkett