0
votes

I have a wordpress archives page that is displaying a custom post type that I've created that I actually want hidden.

I used the plugin, CPT UI, to create the post type "Event"

On my blog, I have a category labeled Featured (website.com/category/featured/), and on this Featured category page I have a few Event CPT's showing up that I want to not be displaying.

I've tried the following code within my functions.php file that did not work:

add_action( 'pre_get_posts', 'exclude_cpt' );
function exclude_cpt( $query ) {
    if ( $query->is_category('featured') ) {
        $query->set( 'post_type', array('event') );
    }
    return $query;
}

Thoughts??

1

1 Answers

0
votes

By $query->set( 'post_type', array('event') ); you are not excluding the Events. you are including it.

To exclude events you have to pass all post type you have and want to show, except 'event'

like this.

if you don't have any custom post type.

$query->set('post_type', array( 'post', 'page' ) );

or

$query->set('post_type', array( 'post', 'page', 'post_type_1', 'post_type_2' ) );

so your code should look like this

add_action( 'pre_get_posts', 'exclude_cpt' );
function exclude_cpt( $query ) {
    if ( $query->is_category('featured') ) {
        $query->set( 'post_type', array( 'post' ) ); // this will display only posts and pages
    }
    return $query;
}