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

Method to Hide Specific Posts on the WordPress Website Homepage or Category Page

When youlearn to build a website yourselfduring daily website updates and operations, you may also needto hide 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 Plugins to Hide Posts

WordPress Pluginare very abundant, so you can directly use a plugin to solve the problem of hiding posts. Here, Naibabiji recommends the pluginWordPress Hide PostsUsing this plugin, you can achieve „When creating a new post or editing a post, you can choose on which archive pages and the homepage to hide that post.“WordPress Hide Posts Plugin DownloadYou can download the plugin from the link above and upload it to the website backend for installation.WordPress Plugin Installation Tutorial WordPress Hide PostsAfter installation, on the right side of your post editing page, you can see the hide post options as shown 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 hide location when editing the post, 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 webmasters who don„t 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 the plugin introduced in „Safe Method to Add Code to the functions.php File: Code Snippets". (Through this Code Snippets plugin, you can manage many of your own added codes.)If you don't want to install this code management plugin, then manually add the following code to the theme's 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 above code is for hiding specific posts on the homepage. The number 1276 in the code is the ID of the post to be hidden. Separate multiple post IDs with English commas.How to get the Post ID1. On the post list page, hover your mouse over the post title. The number in the URL at the bottom left of the browser is the ID of that 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 above code,$query->is_home()||$query->is_feed()||$query->is_search()||$query->is_archive()determines where to hide the post. The codes respectively represent: homepage, feed page, search page, archive page. If you don't need certain places, delete the corresponding code.

Related Articles

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

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

「Naibabiji WordPress Website Building Coaching Service」—From choosing a domain and buying hosting, to installing a Theme and publishing content, I「ll coach you through every step, helping you avoid detours and reach your goal directly.

👉 Learn about the 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 QR Code to Follow

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: