WordPress: Hiding certain posts from the homepage
Last updated on November 28, 2011. Tags: functions.php, homepage, WordPress category ID
In a blog or website that runs in WordPress CMS, there are two options available for your homepage. One is a static webpage (common in non-blog websites) and the other is the list of articles or posts from latest to the earliest (common in blogs). Of course, there are other ways of configuring your homepage but they will require more complicated codes and methods that will not be covered in this article.
If you configured your homepage to be a list of latest posts, the post that was published on the most recent date goes to the top of the list, with the rest of the posts following in reverse chronological order. If there is a post configured as "sticky", the lasted post will come under the sticky.
In some cases, you wouldn't want a post to appear in the home page.
Why would I want to prevent some of my posts from appearing in the homepage
If you found this tutorial, then you might have your own reason for wanting to hide certain post on the homepage. The possible reasons that I know are:
- You classify your posts as major and minor, main article and supplements, long posts and short posts, etc. Get the drift? You want the main articles to be the only ones to appear in your home page, and the supplements are just side information to support the main articles and are not meant to stand on their own.
- If you have a sticky post and either of the following is true: (1) you have very few posts that they can all be listed on the homepage or (2) your earlier posts can be accessed using the previous posts link or some other form of navigation usually at the bottom of the homepage. Then, your sticky post will appear twice, once as the sticky on top of all the posts and once as part of the list in chronological order. You want only the former appearance of the sticky post and not the latter.
So how do I prevent certain posts from appearing on my homepage?
Open the functions.php of your theme. You may open it using the text editor provided by you hosting company in the file manager or control panel, or you may download it and open it using notepad. Then insert the following code somewhere between the very first <?php and the very last ?> tags (if not sure, insert it on line immediately above the very last ?>):
function HomeFilter($query) {
if ($query->is_home) {
$query->set('cat','-21');
}
return $query;
}
add_filter('pre_get_posts','HomeFilter');
Then click save, or if you edited it using notepad, save the file and upload it (you might need to delete the original file in your server; if prompted whether you will overwrite it or not upon uploading, choose to overwrite it).
In the code above, the 21 in the third line is the category ID of the category in which the posts we need to block from appearing on the homepage belong. Simply replace it with the category ID that applies to your website. If you wish to hide two or more categories in the homepage, simply indicate the other categories, each preceded by a minus sign (-) and separated by comma as shown below:
$query->set('cat','-21,-10,-54');
Important notes on using this tweak
- If the category ID you indicated in the third line belongs to a parent category, all posts assigned to its child category will be prevented from appearing in the homepage even if these posts are not directly assigned to the parent category and even if the child categories are not directly indicated.
- I tested this technique on WordPress ver. 2.8.5 and ver. 2.9.1. More likely, it would work in closely related versions and in all versions between these two.
- Some WordPress templates do not come with functions.php file. If there's none, just create it (by using your file manager or by creating a text file and changing its extension from .txt to .php), encode the tag <?php in the first line and the tag ?> in third or greater line, and insert all the functions that you need to insert in this file between these two tags.
Posted by Greten on July 27, 2010 under WordPress
Related Posts
You might also be interested (randomly generated):
Read Comments
Post Comments
Comment Rules and Reminders
- The links to the commentator's e-mail do not have nofollow tag. However, I will be very strict in approving comments.
- When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
- What you write in the name field may include keywords to your website provided that (1) it's only up to four words long and (2) at least one of these four words is your first name or nickname. I rather reply to Bob or to Joe Smith than to Online Marketing Tips.
- Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
- Fields marked with asterisks (*) are required. Your email will never be displayed in public.
Posted by Dimfizz on 01.18.12 12:07 pm
That’s awesome! The sticky post on my front page had been bugging me for over a year because it appeared on my front page and then again in the post list. I did set the date on it earlier than any other post so it would show on the very last page of my homepage. But it still bothered me that it was showing up twice. I followed your instructions here and it cleared up my problem. Now the sticky only shows up on my front page. Thank you very much. Good Work! BTW I found your page through Google, keyword phrase: “keep sticky post from appearing twice in post list”. Thanks again,
Dimfizz
Posted by Greten on 01.21.12 5:11 am
You’re welcome and thanks for informing me about the keywords in which my Codegrad is appearing