[Resolved] How to hide some posts from blog page

Home Forums Support [Resolved] How to hide some posts from blog page

Home Forums Support How to hide some posts from blog page

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1603776
    Matteo

    I’d like to create a new posts category and hide those posts (the entire category) on the blog page.

    The posts will exist and they will be in the sitemap index, but they should be invisible to readers.

    How can I do?

    #1603826
    David
    Staff
    Customer Support

    Hi there,

    you can use this PHP Snippet to exclude a category:

    add_action( 'pre_get_posts', function( $query ) {
        if ( !is_admin() && $query->is_main_query() ) {
            $query->set( 'cat', -10 );
        }
    } );

    the 10 value is the Category ID. You can get the ID by editing the Category and checking the URL.

    #1623707
    Matteo

    This code hide also the post from Posts-All Posts in WP dashboard and I can’t find the post.

    #1623760
    David
    Staff
    Customer Support

    Updated the code above to fix that.

    #1623764
    Matteo

    Resolved with this code:

    function exclude_category_home( $query ) {
    if ( $query->is_home ) {
    $query->set( 'cat', '-55' );
    }
    return $query;
    }
    add_filter( 'pre_get_posts', 'exclude_category_home' );

    Where 55 is a random category number to change.

    #1623769
    David
    Staff
    Customer Support

    Glad to hear you found the answer

    #1623770
    Matteo

    Also your code works. Thanks.

    #1623780
    David
    Staff
    Customer Support

    You’re welcome

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.