For some purposes I made a plugin that automatically makes posts with a specific category. Now in admin posts section, posts with that specific category have been too plentyfull and finding other posts has been difficult.

For this I wrote another plugin and want to prevent showing posts of that specific category id or slug in admin posts section without using any third party plugin. And I want to make a menu in admin section to show posts of that specific category in that.

My target category slug is 'eventscat' .

But I don't know how do it, what hook must I use?

  • Should be possible with the pre_get_posts action - why do you want to do this?
    – kero
    Aug 20, 2018 at 12:58
  • @kero I edited question and added my reason Aug 20, 2018 at 13:05

1 Answer 1


You can use the pre_get_posts action to affect any queries, both in frontend and admin. In your case you should make sure you affect only admin queries and you can even use get_current_screen to narrow it down further. Here's an example that would modify the query only on the regular posts page:

add_action ('pre_get_posts', 'wpse311946_restrict_cats');
function wpse311946_restrict_cats ($query) {
   // retrieve the id of the category to be excluded
   $idObj = get_category_by_slug ('eventscat'); 
   $id = $idObj->term_id;
   // find current admin page
   $current_screen = get_current_screen ();
   // conditionally exclude category
   if (is_admin() && $current_screen->id == "edit-post" ) {
     $query->set ('cat', -$id);

You can use the same filter, slightly modified, to make sure only posts of this category are shown on the other posts page you want to create (this will have another screen id).

  • Does $current_screen->id == "edit" suffice to only apply this for posts (not pages, other CPT, ..)?
    – kero
    Aug 20, 2018 at 13:24
  • 1
    @kero. You're right, that should be edit-post. Fixed it.
    – cjbj
    Aug 20, 2018 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.