How To Hide Posts From The Front Page Of WordPress?

How To Hide Posts From The Front Page Of WordPress?

You can now customize your homepage and blog archive page by hiding posts that you don’t want to show up!

You can hide single or multiple posts from the home page. This method requires you to add some code to your WordPress site.

WordPress uses a database query to fetch and display posts that are relevant to each page. However, we can modify the query before it is run to filter out certain posts.

We will be using those hooks to modify the WordPress query and hide the WordPress posts, pages, and custom post types in different sections.

How to hide posts from the front page of WordPress using code snippets?

You can add the custom code to your theme’s functions.php. However, we will be using the code snippet plugin which is safer and does not break your site. you just need to enter your code and some necessary details, such as The snippet’s title.

Also, you can modify the code snippet as per your requirement.

How to hide posts from the front page?

First, you need to add custom code in the code snippets plugin. You will also need the IDs of the post or pages that you want to hide. Your posts are identified by post id. To get to the content of each post ID you have to visit the individual post and click on edit. Post ID will be visible in your browser’s address bar. So for example, in the following post, the post id is 1.

img 6094a84c9ebcc

Now, add the below code in the code snippet plugin.

function exclude_from_home($query) {
      if ($query->is_home() ) {
          $query->set('post__not_in', array(1, 5));
      }
}
add_action('pre_get_posts', 'wpb_exclude_from_home');

The following code uses is_home() conditional tag to find out if the user is viewing the homepage. If they are, then it excludes the post IDs from the query.

Don’t forget to replace the IDs inside the array with the actual IDs of posts or pages that you want to exclude. You can insert multiple post id separated by a comma.

How to hide posts from the blog archives?

To hide posts from the blog archives use the is_archive() conditional tag.

function exclude_from_archives($query) {
      if ( $query->is_archive() ) {
          $query->set('post__not_in', array(1, 5));
      }
}
add_action('pre_get_posts', 'exclude_from_archives');

How WordPress Posts or Pages from RSS Feed?

If you want to hide a WordPress post from the RSS feed, then you use the is_feed conditional tag in the code as below.

function exclude_from_feed($query) {
      if ($query->is_feed() ) {
          $query->set('post__not_in', array(1, 5));
      }
}
add_action('pre_get_posts', 'exclude_from_feed');

Note that, this code will hide WordPress posts from the homepage as well as the RSS feed.

If you are logged in as an administrator you will still see the posts listed in the RSS feed but other users will not be able to see the excluded posts when they view your RSS feed.

Hide WordPress Post or Page from Site Search?

If you wanted to hide specific posts from the WordPress site search add the is_search conditional tag to the code.

function exclude_from_search($query) {
      if ( $query->is_search() ) {
          $query->set('post__not_in', array(1, 5));
      }
}
add_action('pre_get_posts', 'exclude_from_search');

The excluded post will not appear in search results, even though the posts are public.

How to hide posts Post or Page from Everywhere?

What if you want to hide a WordPress post from all the above areas at once. Well, it’s possible with simple or conditions.

function exclude_from_everywhere($query) {
      if ( $query->is_home() || $query->is_feed() ||  $query->is_search() || $query->is_archive() ) {
          $query->set('post__not_in', array(1, 5));
      }
}
add_action('pre_get_posts', 'exclude_from_everywhere');

This code snippet will hide the given posts from the homepage, RSS feed, search results, and archive pages.

How to hide a post from the ‘Recent Posts’ widget?

You can exclude posts from the recent posts widget via the widget_posts_args filter:

add_filter( 'widget_posts_args', 'exclude_from_recent'); 
function exclude_from_recent( $args ){ 
	$args['post__not_in'] = array(1); 
	return $args; 
}

Conclusion

This method allows you to hide posts that you don’t want to show up on the front page of your WordPress site.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *