In WordPress, there are times when you may want to hide specific posts from public view. This guide will explain in detail how to hide one or more posts from the homepage, search results, widgets, and across your entire site.
1. Basic Function: Hiding Specific Posts
We will begin by using the pre_get_posts
hook to modify the query. The basic code structure is as follows:
function exclude_specific_posts($query) {
if ($query->is_main_query()) {
// Ensure it only affects the main query
$query->set('post__not_in', array(ID1, ID2)); // Replace with the IDs of the posts you wish to hide
}
}
add_action('pre_get_posts', 'exclude_specific_posts');
Explanation:
is_main_query()
: This ensures that only the main query is modified, preventing interference with widgets or other queries.
2. Hiding Posts from the Homepage
If you want to hide specific posts only on the homepage, adjust the condition to is_home()
:
function exclude_posts_from_home($query) {
if ($query->is_home() && $query->is_main_query()) {
$query->set('post__not_in', array(ID1, ID2)); // Replace with the IDs of the posts you wish to hide
}
}
add_action('pre_get_posts', 'exclude_posts_from_home');
3. Hiding Posts in Search Results
To hide specific posts from search results, use the following code:
function exclude_posts_from_search($query) {
if ($query->is_search() && $query->is_main_query()) {
$query->set('post__not_in', array(ID1, ID2)); // Replace with the IDs of the posts you wish to hide
}
}
add_action('pre_get_posts', 'exclude_posts_from_search');
4. Hiding Posts in Widgets
To hide specific posts in widgets (such as sidebar widgets), add the following condition to the main query:
function exclude_posts_from_widgets($query) {
if ($query->is_main_query() && (is_active_widget(false, false, 'your_widget_id', true))) {
$query->set('post__not_in', array(ID1, ID2)); // Replace with the IDs of the posts you wish to hide
}
}
add_action('pre_get_posts', 'exclude_posts_from_widgets');
Explanation:
is_active_widget()
: This checks whether a specific widget is active.
5. Hiding Posts from Category Pages
To hide specific posts from category pages, you can add the following code to your functions.php
file:
function exclude_posts_from_category($query) {
if ($query->is_category() && $query->is_main_query()) {
$query->set('post__not_in', array(ID1, ID2)); // Replace with the IDs of the posts you wish to hide
}
}
add_action('pre_get_posts', 'exclude_posts_from_category');
6. Hiding Posts Across the Entire Site
If you want to hide specific posts site-wide, simply remove the conditions for is_home()
and is_search()
like this:
function exclude_posts_from_all($query) {
if ($query->is_main_query()) {
$query->set('post__not_in', array(ID1, ID2)); // Replace with the IDs of the posts you wish to hide
}
}
add_action('pre_get_posts', 'exclude_posts_from_all');
Practical Tips
Backup: Always back up your website before modifying the
functions.php
file to prevent any issues that may lead to a crash.Testing: After implementing changes, be sure to test your site in various areas to ensure the hiding functions operate correctly.
By following these methods, you can effectively hide WordPress articles on the homepage, search results, category pages, and widgets. Simply replace the placeholder IDs with the actual IDs of the posts you’d like to hide. This guide aims to help even beginners understand and implement how to hide specific posts in WordPress with ease.
Further Learning
Interested in how to hide specific categories in WordPress? Check out our additional resources: [How to Hide Specific Categories](insert_post_link id=”2106″).
By optimizing your content based on these strategies, you can enhance your WordPress article management, ensuring a cleaner site presentation.