Hide individual posts from the homepage in GeneratePress Theme

If you’re using the GeneratePress theme and want to hide certain individual posts from your homepage, there’s a straightforward way to do it.

Whether you want to remove promotional content, old articles, or specific posts from appearing in your main blog feed, this guide will show you how to achieve it using a simple PHP snippet.

Why Hide Posts from the Homepage?

Sometimes, you may want to exclude certain posts from appearing on your homepage while still keeping them accessible on your site. This can be useful if:

  • You have content that is seasonal or time-sensitive.
  • You want to highlight specific posts and hide others.
  • You have content that doesn’t fit with your homepage’s aesthetic or content strategy.

Step-by-Step Guide to Hiding Posts

To hide individual posts from the homepage in the GeneratePress theme, follow these simple steps:

Step 1: Create a New Category

First, you need to create a category that will be used to exclude the posts from your homepage feed.

  • Go to your WordPress dashboard.
  • Navigate to Posts > Categories.
  • Create a new category, for example, “News.”
  • After creating the category, edit it by clicking on the category name.

When you edit the category, look at the browser URL in the address bar. You’ll see something like this:

yourwebsite.com/wp-admin/term.php?taxonomy=category&tag_ID=4post_type=post

In this example, the category ID is 4. Note down this number because you’ll need it later.

Step 2: Assign Posts to the New Category

Next, assign the posts you want to hide to this newly created category:

  • Go to Posts > All Posts.
  • Edit the posts you want to hide from the homepage.
  • Assign them to the category you created, like “News.”

Step 3: Add a PHP Snippet to Exclude the Category

Now, you need to add a PHP snippet to your site that will exclude the posts in this category from appearing on the homepage. Here’s how to do it:

Picture: Adding PHP Snippet to hide individual post in GeneratePress theme.
  • Install and activate the WPCode plugin.
  • Go to Code Snippets > Add Snippet in your WordPress dashboard.
  • Add the following code snippet:
add_action('pre_get_posts', 'exclude_category_posts');
function exclude_category_posts( $query ) {
    if($query->is_main_query() && !is_admin()) {
        $query->set('cat', array( -4 ));
    }
}
  • Replace -4 with the ID of the category you want to exclude. For example, if your category ID is 66, the code will look like this:
$query->set('cat', array( -66 ));<br>
  • Save and activate the snippet.

How the Code Works

This code snippet hooks into the pre_get_posts action, which modifies the main WordPress query before it retrieves posts. The condition if($query->is_main_query() && !is_admin()) ensures that this modification only applies to the main query on the front end, not in the WordPress admin area.

By setting the category ID as a negative number (-4 or -66), the code tells WordPress to exclude posts from that category on the homepage.

Final Thoughts

That’s it! You’ve successfully hidden specific posts from your homepage in the GeneratePress theme.

This method is flexible and allows you to easily control which posts appear on your homepage without affecting the rest of your site. Whether you’re managing a blog, a news site, or a business website, this simple trick can help you curate your homepage content more effectively.

Leave a Comment