0
votes

Basically, when I view the All Posts page in Wordpress Admin, I want to filter out all posts (exclude them) from a specific category to not be shown there.

After some searching, I found this hook, it works but it works in all admin

function exclude_category_posts( $query ) {

if ( $query->is_main_query() && is_admin()) {
    $query->set( 'cat', '-13' );
}

 }
   add_filter( 'pre_get_posts', 'exclude_category_posts' );

I've created another menu in WP Admin that shows only posts from that excluded category but when I apply this function, it shows nothing there too. Not sure if this is possible but I thought I ask some of you kind folk here.

1
You used is_admin() in your code. So it is only for admin.Ranjit

1 Answers

1
votes

Change like this.

function exclude_category_posts( $query ) {
    if ( $query->is_main_query() && is_admin()) {
        if($_REQUEST['page_type']=="single_cat")
            $query->set( 'cat', '13' );
        else
            $query->set( 'cat', '-13' );
    }
}
add_filter( 'pre_get_posts', 'exclude_category_posts' );

Then the URL of your new menu could be like http://example.com/wp-admin/edit.php?page_type=single_cat.