Filter Posts by Excluding Categories

I am logged on as admin

I am on the Posts dashboard

I have Posts assigned to multiple Categories and I have posts that are assigned to only one category.

I want to display posts that are assigned to only one category.

if a post is assigned to multiple categories, then it that post should not be displayed.

The result should be posts that are assigned to one category and as I edit them and assign them to more than one category, the Post will not show in the List after I click save.

How can this be done?

1 Answer
1

You can hook into the load-edit.php action to modify how wp-admin lists pages. Add this to your functions.php:

<?php

add_action( 'load-edit.php', 'namespace_modify_admin_list' );

function namespace_modify_admin_list() {
    global $typenow;

    if ( 'post' !== $typenow )
        return;

    add_action( 'pre_get_posts', 'namespace_modify_admin_list_posts' );
}

function namespace_modify_admin_list_posts( $query ) {
    /**
     * Modify query here to change how posts are listed, using
     * the set method of the query object.
     *
     * Example:
     * $query->set( 'posts_per_page', 10 );
     */
}

?>

May I ask why would you want to hide posts that have more than one category? Is this for access-control or filtering purposes?

Leave a Comment