When you
learn to build a website yourselfduring daily website updates and operations, you may also need
to 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 plugin
WordPress 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.“
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.

2. When publishing a new post (in classic editor mode, and the
Permalinkformat is numeric.), the ID will also be displayed in the permalink.
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:
- Adding a password to the post (can be selected under visibility when publishing the post)
- Installing a plugin that requires registration/login to view
- Determining display based on user permissions
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.