Home Forums Front-end Issues Category excluded in WP_Query


Category excluded in WP_Query

  • Howdy!

    I’ve tried to get this question answered in some other places, with no result… Now I hope some bright and kind mind here will help!

    So: I am successfully using this functions.php snippet to exclude category 3 from the WordPress homepage:

    function exclude_category_home( $query ) {
    if ( $query->is_front_page ) {
    $query->set( 'cat', '-3' );
    return $query;
    add_filter( 'pre_get_posts', 'exclude_category_home' );

    A query (on a profile page tab by WPUM-plugin) for listing a users posts works fine:

    $the_query = wpum_get_posts_for_profile( $data->user->ID );

    But my homemade query on another profile page tab, for listing posts where the current_user-ID is the value of my ACF custom field “fetcher”:

    $the_query = new WP_Query( array( 'meta_key' => 'fetcher', 'meta_value' => $user_ID ) );

    …unintentionally excludes category 3, like on the frontpage (as soon as the snippet-filter is activated).

    Why is that?

  • I would recommend also adding a check that the snippet is only impacting the main page query so this line:

    if ( $query->is_front_page ) {

    Would change to:

    if ( $query->is_main_query() && $query->is_front_page() ) {

  • Perfect solution! ⭐
    I’m surprised I couldn’t find that answer elsewhere.
    Thank you very very much @wpfieldwork !

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.