🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

How to Hide Specific Posts on WordPress Homepage or Category Pages

When youLearn to build your own websiteAfterwards, during the daily website update and operation process, you may also need tohide specific posts from being displayed on the website homepage or category pages.This article teaches you how to use plugins and code to hide specific posts in specific locations.

Using a plugin to hide posts

WordPress Pluginis very extensive, so you can directly use plugins to solve the problem of hiding posts. Here, Naibabiji recommends the pluginWordPress Hide Posts. Using this plugin, you can „choose on which archive pages and on the homepage to hide the post when creating a new post or editing a post.“WordPress Hide Posts Plugin DownloadYou can download the plugin from the link above and upload it to the website's Admin Dashboard for installation.WordPress Plugin Installation Tutorial WordPress Hide PostsAfter installation, on the right side of your post editing page, you can see hidden post options like in the image above, which are:
  • Hide on frontpage
  • Hide on categories
  • Hide on search
  • Hide on tags page
  • Hide on authors page
You only need to check the hiding location when editing the post and then publish or update the post to achieve the function of hiding the post in specific places.

Using code to hide posts

Using code to hide posts is suitable for website owners who do not want to install too many plugins. The plugin method is also simple. If you don„t know how to add code, it is recommended to first install a plugin introduced in “Safe method to add code to the functions.php file: Code Snippets". (Through this Code Snippets plugin, you can manage many codes you add yourself.)If you don't want to install this code management plugin, then manually add the following code to the Theme template functions (functions.php). Code to hide specific posts on the homepage:
//在首页隐藏特定文章
function naibabiji_exclude_from_home($query) {
      if ($query->is_home() ) {
          $query->set('post__not_in', array(1276));
      }
}
add_action('pre_get_posts', 'naibabiji_exclude_from_home');
The code above is the code to hide specific posts on the homepage. The number 1276 in the code is the ID of the post to be hidden. Separate multiple posts with English commas.Methods to obtain the Post ID1. On the post list page, move the mouse over the post title. The number in the URL at the bottom left of the browser is the ID of this post.文章ID获取方法2. When publishing a new post (in Classic Editor mode, and thePermalinkformat is numeric.), the ID will also be displayed in the Permalink.文章ID获取方法 Code to hide posts everywhere:
//所有地方隐藏文章
function naibabiji_exclude_from_everywhere($query) {
      if ( $query->is_home() || $query->is_feed() ||  $query->is_search() || $query->is_archive() ) {
          $query->set('post__not_in', array(1276));
      }
}
add_action('pre_get_posts', 'naibabiji_exclude_from_everywhere');
In the code above,$query->is_home()||$query->is_feed()||$query->is_search()||$query->is_archive()Determines where to hide posts. The code represents: homepage, feed page, search page, archive page. If you don't need certain locations, delete the corresponding code.

Related articles to this post

The above code and plugin only hide the display of posts in specific locations on the website. If someone knows the post URL, they can still access it. If you want to hide the post and prevent direct access even if the URL is known, you can consider:
  1. Add a password to the post (can be selected under visibility when publishing the post).
  2. Install a plugin that requires registration and login to view content.
  3. Determine whether to display based on user permissions.
 

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step instead.

「Naibabiji WordPress Website Building Coaching」 — From selecting a domain and purchasing hosting to installing themes and publishing posts, I「ll guide you through every step, helping you avoid detours and reach your goals directly.

👉 Learn about Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan to Follow