最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Hide a specific category in admin All Posts page (Wordpress)

programmeradmin0浏览0评论

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

After some searching, I found this hook but it's not working in this instance.

function exclude_category_posts( $query ) {

    if ( $query->is_main_query() && !is_admin() && $query->is_home() ) {
        $query->set( 'cat', '-13, -14, -15, -16' );
    }

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

Thanks in advance

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

After some searching, I found this hook but it's not working in this instance.

function exclude_category_posts( $query ) {

    if ( $query->is_main_query() && !is_admin() && $query->is_home() ) {
        $query->set( 'cat', '-13, -14, -15, -16' );
    }

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

Thanks in advance

Share Improve this question edited Jul 14, 2018 at 12:19 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Jul 14, 2018 at 11:12 vexxvexx 113 bronze badges 2
  • 1 Keep in mind that telling the DB to exclude something makes for an expensive/slow query. It would be faster to list every single category except the ones you want, always ask for what you want, not what you don't want – Tom J Nowell Commented Jul 14, 2018 at 11:19
  • 1 Your example is explicity applying when it's not the admin: !is_admin(). – Jacob Peattie Commented Jul 14, 2018 at 11:22
Add a comment  | 

1 Answer 1

Reset to default 0

If we look at the official documentation for WP_Query:

https://codex.wordpress/Class_Reference/WP_Query#Category_Parameters

Show posts associated with certain categories.

  • cat (int) - use category id.
  • category_name (string) - use category slug.
  • category__and (array) - use category id.
  • category__in (array) - use category id.
  • category__not_in (array) - use category id.

The problem is that cat demands a category ID, not an array of values.

We can also see a more appropriate option of category__not_in, and we can use an actual array, not a comma separated list:

$query->set( 'category__not_in', [13,14,15,16] );

Which leads to the other problem, the condition:

if ( $query->is_main_query() && !is_admin() && $query->is_home() ) {

If you want this to run on the admin, why is this part in the check?

!is_admin() && $query->is_home()

It says if it is not the admin, and we're on the homepage, do X. But you want the opposite! Remove the is home bit, and reverse the is_admin by removing the !. This part will require beginner level PHP skills to do.

Finally, asking the database to exclude things, leave stuff out, is expensive, and gets excessively expensive as the amount of content grows.

Additionally, you should never hardcode IDs, all it takes is somebody deleting the category and recreating it, a site migration, etc and it's broken

发布评论

评论列表(0)

  1. 暂无评论