Wednesday

Easy Methods to Hide a Post from Homepage in WordPress


As a WordPress Developer, sometimes you want to hide a blog post from your WordPress homepage or blog archive page but allow those who have direct access to the link to view it without using a password. If you are wondering how you can do this, relax and read this post as we take you through the process of hiding posts from selected pages in WordPress like homepage, category archives, and search results.


How to hide a WordPress Post from Homepage Using a Plugin


If you are a beginner, this is the best method for you due to its simplicity. Like any other plugin, the first thing you have to do is to install and activate the WordPress Hide Posts plugin. After installing the plugin, the next step is to edit the post that you want hidden. You can do this from the right column of the editor, where you will see a new ‘Hide Posts’ section. Click on it and you will be given plugin options which include hiding the post on the front page and blog page, author’s page, category or tag pages, and site search results. 


Pick your preferred option and save your post. If you want to confirm if the method has worked, just visit those pages and you’re the post you chose to hide won’t be listed. The only people who will see the post are users with direct post URL (permalink), and they can access the hidden post by entering the URL. 


Although this is the simplest option perfect for any beginner, several powerful options are lacking. One of the excluded options includes failure to hide a page or a custom post type like Woo-Commerce products. Another setback is that it does not allow you to hide a post from WordPress RSS feed.


How to Manually Hide WordPress Posts and Pages


This is a more technical method that requires the user to have some technical skills since code will need to be added on during your WordPress Development. If you have not done this before there are tutorials on how to copy and paste code snippets in WordPress available online. To fetch and display posts based on the page a user is viewing, WordPress uses a database query. There are also built-in hooks that are used to modify the query before running it.


In this method, you are required to use these hooks for modification of WordPress query so that you can hide posts, pages, and custom post types in different sections. Using code snippets, you can add custom code that won’t break your site since it’s much safer. Another way is by adding the custom code to your theme’s functions.php file or a site-specific plugin. Another requirement for hiding your post or pages that you want to hide is their IDs. To get the ID you need to edit a post or a page in your browser’s address bar.


Now that we know what is required, let’s take a look at the code part.


Hiding WordPress Posts or Pages from Homepage


To find out whether the users have access to the homepage, you will have to use the following code uses is_home() conditional tag. You can exclude the post Ids from the query if they are.


Function wpb_exclude_from_home(​$query​)


     If ($query->is_home() )


         $query​->set(​‘post__not_in’​, array(1737, 1718));


     



Add_action(​‘pre_get_posts’​, ‘wpb_exclude_from_home’​);


Remember to replace the Ids inside the array with the actual Ids of posts or pages that you want to be excluded from. 


Hiding WordPress Posts or Pages from RSS Feed


You can simply use the is_feed conditional tag in the code if you want to hide a WordPress post from the homepage and WordPress RSS feed. 


Function wpb_exclude_from_feed(​$query​)


     If ($query->is_feed() )


         $query​->set(​‘post__not_in’​, array(1737, 1718));


     



Add_action(​‘pre_get_posts’​, ‘wpb_exclude_from_feed’​);


By entering this code correctly as an admin, you will still see the posts listed when you visit your WordPress RSS feed while other users will not see the excluded posts when they view your RSS feed.


Hiding WordPress Post or Page from Site Search


You can also hide specific posts from WordPress site search if you want to. This is done by simply adding the is_search conditional tag to the code.


Function wpb_exclude_from_search(​$query​)


     If ( $query->is_search() )


         $query​->set(​‘post__not_in’​, array(1737, 1718));


     



Add_action(​‘pre_get_posts’​, ‘wpb_exclude_from_search’​);


To confirm whether it has worked you can visit your website and search for the hidden costs. The posts may be public but they will not appear in the search results if a user enters keywords in the search bar. 


Hiding WordPress Post or Page from Archives


Hiding specific WordPress posts or pages from archive is also possible if you want to. You can hide category, tags, and date archives. To do this, just use the is_archive() conditional tag.


Function wpb_exclude_from_archives(​$query​)


     If ( $query->is_archive() )


         $query​->set(​‘post__not_in’​, array(1737, 1718));


     



Add_action(​‘pre_get_posts’​, ‘wpb_exclude_from_archives’​);


Hiding WordPress Post or Page from Everywhere


All the above methods have only shown you how to hide a WordPress post or page from specific areas. But did you know that you can also completely hide a WordPress post from all these areas at the same time? To achieve this, you will need a combination of all the conditional tags used earlier in a single code snippet.


Function wpb_exclude_from_everywhere(​$query​)


     If ( $query->is_home()


Add_action(​‘pre_get_posts’​, ‘wpb_exclude_from_everywhere’​);


By entering this code in you WordPress website, you will hide the given posts from homepage, RSS feeds, search results, and archive pages.


Final remarks 


That is all we had time for on how to hide a WordPress post from the homepage and other areas of your website. We trust that this article was helpful to you and you can go ahead and apply the knowledge that we have passed on in your WordPress website about hiding specific posts and pages.


Author Bio


Naman Modi is a Professional Blogger, SEO Expert & Guest blogger at NamanModi.com, He is an Award-Winning Freelancer & Web Entrepreneur helping new entrepreneur’s launches their first successful online business.