Can I hide a specific post from latest posts page?

Let’s say that I have a post that I only want to be shown in the page of the category that it belongs to. The problem is that this post is also visible in the latest posts page.

Is there a way to hide this post from the latest post page and show it only in the page of the category of the post?

Related posts

Leave a Reply

3 comments

  1. function exclude_single_posts_home($query) {
      if ($query->is_home() && $query->is_main_query() && !is_admin()) {
        $query->set('post__not_in', array(post-id));
      }
    }
    
    add_action('pre_get_posts', 'exclude_single_posts_home');
    

    Source: pre_get_posts

  2. Since you’re using the core “Page for Posts” settings, you should be able to use the awesome pre_get_posts action to remove the specific ID you want to exclude.

    This should work in your functions.php file or /mu-plugins/ plugin:

    wpse94273_hide_post( $query ) {
        // only remove post ID from page_for_posts and in the main query
        if( $query->is_home() && $query->is_main_query() ) {
            // remove specific page ID from query
            query->set( 'post__not_in', array( {your post id} ) );
        }
    }
    add_action( 'pre_get_posts', 'wpse_94273_hide_post' );
    

    Replace “{your post id}” with the ID of the post you want to exclude. It’s an array, so you can enter multiple IDs separated by commas if desired.

    If the post ID changes, then you’d need to setup some kind of option using the Settings API to store the ID you want to exclude.

  3. Your question is hard to understand.. Do you mean a specific post or somthing automatic? if you mean a specific post and If you are using wp_query / query posts to loop for recent posts you can add this parameter.

    'post__not_in' => array($postId)
    

    Example query:

    $relatedargs = array(
        'post__not_in'      => array($postId) // THIS IS THE LINE
    );
    $the_query = new WP_Query( $relatedargs );
    
    // The Loop
    if( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) : $the_query->the_post();
    
    // YOUR LOOP HERE
    
    endwhile; 
    }
    

    Hope this helps ;