Skip to main content
added 333 characters in body
Source Link
David Sword
  • 3.4k
  • 1
  • 14
  • 29

register_post_type() has a argument that you can set to specify which post types you want searched, thus excluded the undesired ones. This is the simplest method, just one line:

'exclude_from_search' = true,

Alternative to that, you can hook into the search query itself via pre_get_posts and then modify the search, specifying which post types you're after:

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

If you were dead set on doing the term hiding - although I don't recommend it, as it's more code, and with a higher chance of user error, and extra steps - you'd do somthing like:

add_action( 'pre_get_posts', function ( $query ) {
    global $wp_the_query;
    if($query === $wp_the_query && $query->is_search() && !is_admin()) {
        $tax_query = array(
            array(
                'taxonomy' => 'your_custom_tax_name',
                'field' => 'slug',
                'terms' => 'hidden',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
});

register_post_type() has a argument that you can set to specify which post types you want searched, thus excluded the undesired ones. This is the simplest method, just one line:

'exclude_from_search' = true,

Alternative to that, you can hook into the search query itself via pre_get_posts and then modify the search, specifying which post types you're after:

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

If you were dead set on doing the term hiding - although I don't recommend it, as it's more code, and a higher chance of user error, and extra steps - you'd do somthing like:

add_action( 'pre_get_posts', function ( $query ) {
    global $wp_the_query;
    if($query === $wp_the_query && $query->is_search() && !is_admin()) {
        $tax_query = array(
            array(
                'taxonomy' => 'your_custom_tax_name',
                'field' => 'slug',
                'terms' => 'hidden',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
});

register_post_type() has a argument that you can set to specify which post types you want searched, thus excluded the undesired ones. This is the simplest method, just one line:

'exclude_from_search' = true,

Alternative to that, you can hook into the search query itself via pre_get_posts and then modify the search, specifying which post types you're after:

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

If you were dead set on doing the term hiding - although I don't recommend it, as it's more code with a higher chance of user error - you'd do somthing like:

add_action( 'pre_get_posts', function ( $query ) {
    global $wp_the_query;
    if($query === $wp_the_query && $query->is_search() && !is_admin()) {
        $tax_query = array(
            array(
                'taxonomy' => 'your_custom_tax_name',
                'field' => 'slug',
                'terms' => 'hidden',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
});
added 333 characters in body
Source Link
David Sword
  • 3.4k
  • 1
  • 14
  • 29

register_post_type()register_post_type() has a argument exclude_from_searchargument that you can set to true for your to-be-exempt specify which post types, so you don't have to dabble with a "hidden" term work aroundwant searched, thus excluded the undesired ones. This is the simplest method, just one line:

'exclude_from_search' = true,

AlternativelyAlternative to that, you can hook into the search query itself via pre_get_posts pre_get_posts and then modify the search, specifying which post types you're after:

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

If you were dead set on doing the taxonomyterm hiding - although I don't recommend it, as it's more code, and a higher chance of user error, and extra steps - you'd do somthing like:

add_action( 'pre_get_posts', function ( $query ) {
    global $wp_the_query;
    if( $query === $wp_the_query && $query->is_search() && !is_admin()) {
        $tax_query = array(
            array(
                'taxonomy' => 'category''your_custom_tax_name',
                'field' => 'slug',
                'terms' => 'hidden',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
});

register_post_type() has a argument exclude_from_search that you can set to true for your to-be-exempt post types, so you don't have to dabble with a "hidden" term work around.

Alternatively you can hook into the search query itself via pre_get_posts and modify which post types you're after:

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

If you were set on the taxonomy hiding, you'd do somthing like:

add_action( 'pre_get_posts', function ( $query ) {
    global $wp_the_query;
    if( $query === $wp_the_query && $query->is_search() ) {
        $tax_query = array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'hidden',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
});

register_post_type() has a argument that you can set to specify which post types you want searched, thus excluded the undesired ones. This is the simplest method, just one line:

'exclude_from_search' = true,

Alternative to that, you can hook into the search query itself via pre_get_posts and then modify the search, specifying which post types you're after:

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

If you were dead set on doing the term hiding - although I don't recommend it, as it's more code, and a higher chance of user error, and extra steps - you'd do somthing like:

add_action( 'pre_get_posts', function ( $query ) {
    global $wp_the_query;
    if($query === $wp_the_query && $query->is_search() && !is_admin()) {
        $tax_query = array(
            array(
                'taxonomy' => 'your_custom_tax_name',
                'field' => 'slug',
                'terms' => 'hidden',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
});
Source Link
David Sword
  • 3.4k
  • 1
  • 14
  • 29

register_post_type() has a argument exclude_from_search that you can set to true for your to-be-exempt post types, so you don't have to dabble with a "hidden" term work around.

Alternatively you can hook into the search query itself via pre_get_posts and modify which post types you're after:

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

If you were set on the taxonomy hiding, you'd do somthing like:

add_action( 'pre_get_posts', function ( $query ) {
    global $wp_the_query;
    if( $query === $wp_the_query && $query->is_search() ) {
        $tax_query = array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => 'hidden',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
});