2

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?

  • How are you displaying posts on latest post page? – Vinod Dalvi Apr 2 '13 at 12:07
  • @VinodDalvi: I only have configured ad the wp-admin interface in the Reading Settings that a page that I created show the posts (This page is not the front page of the blog). – Renato Lochetti Apr 2 '13 at 12:09
  • Which theme are you using? – Vinod Dalvi Apr 2 '13 at 12:17
  • I'm using the Chateau Theme, but if you tell me that I need to change the theme, this can be done. The main concern is that it must work in the Minileven mobile theme, because I have many users that access the blog from a mobile device. – Renato Lochetti Apr 2 '13 at 12:31
2
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

1

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.

0

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 ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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