How to Exclude Certain Categories from WordPress Related Posts: A Step-by-Step Guide
As a WordPress website owner, you understand the importance of related posts in keeping your visitors engaged and improving your site's overall user experience. However, there may be instances where you want to exclude specific categories from appearing in the related posts section.
Perhaps you have certain categories that cover sensitive or promotional content that you don't want to display alongside your other posts. Or maybe you want to tailor the related posts to better align with the primary focus of your website. Regardless of the reason, this article will provide you with a step-by-step guide on how to exclude specific WordPress categories from the related posts section.
Understanding the WordPress Related Posts Function
In WordPress, the related posts functionality is typically handled by a plugin or a custom code snippet. The default WordPress function that retrieves related posts is called get_related_posts()
. This function uses the WP_Query
class to fetch posts that are related to the current post being viewed.
The get_related_posts()
function takes several parameters, including the current post ID, the number of posts to display, and the taxonomy to use for the relationship. By default, the function uses the post's categories to determine the related posts.
One of the key parameters we'll be focusing on is the category__not_in
parameter. This parameter allows you to exclude specific categories from the related posts query, ensuring that those categories are not displayed in the related posts section.
Step 1: Identify the Categories to Exclude
Before you start implementing the code, you'll need to identify the specific categories you want to exclude from the related posts. You can do this by navigating to the WordPress dashboard, going to the "Posts" section, and then clicking on the "Categories" tab.
Here, you'll see a list of all the categories on your website. Take note of the category IDs for the categories you want to exclude. You'll need these IDs in the next step.
Step 2: Modify the WordPress Related Posts Query
To exclude specific categories from the related posts, you'll need to create a custom function that overrides the default get_related_posts()
function. Here's the code snippet you can use:
function custom_get_related_posts( $post_id, $posts_per_page = 5, $taxonomy = 'category' ) {
$args = array(
'posts_per_page' => $posts_per_page,
'orderby' => 'title',
'order' => 'ASC',
'category__not_in' => array( 6, 7 )
);
$related_posts = new WP_Query( $args );
return $related_posts;
}
In this code snippet, we're defining a custom function called custom_get_related_posts()
that takes three parameters:
$post_id
: The ID of the current post.
$posts_per_page
: The number of related posts to display.
$taxonomy
: The taxonomy to use for the related posts (in this case, "category").
Inside the function, we're setting up the $args
array with the following parameters:
posts_per_page
: The number of posts to display.
orderby
: The order in which the posts will be sorted (in this case, by title in ascending order).
order
: The order of the posts (in this case, ascending).
category__not_in
: An array of category IDs to exclude from the related posts query.
In the example, we're excluding categories with IDs 6 and 7. Replace these IDs with the ones you identified in the previous step.
Finally, we're creating a new WP_Query
object using the $args
array and returning the result.
Step 3: Replace the Default Related Posts Function
Now that you have the custom function ready, you need to replace the default get_related_posts()
function with your custom function. You can do this by adding the following code to your WordPress theme's functions.php file or a custom plugin:
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
function get_related_posts( $post_id, $posts_per_page = 5, $taxonomy = 'category' ) {
return custom_get_related_posts( $post_id, $posts_per_page, $taxonomy );
}
In this code, we're first removing the default adjacent_posts_rel_link_wp_head
action, which is responsible for the default related posts functionality. Then, we're creating a new get_related_posts()
function that simply calls our custom custom_get_related_posts()
function.
Step 4: Implement the Related Posts in Your Theme
Now that you've replaced the default related posts function, you can call the get_related_posts()
function in your WordPress theme to display the related posts. Here's an example of how you can do this:
<?php
$related_posts = get_related_posts( get_the_ID(), 5 );
if ( $related_posts->have_posts() ) {
echo '<h2>Related Posts</h2>';
echo '<ul>';
while ( $related_posts->have_posts() ) {
$related_posts->the_post();
echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
}
?>
In this example, we're calling the get_related_posts()
function with the current post ID and a limit of 5 posts. We then check if there are any related posts, and if so, we display them in an unordered list.
You can customize the HTML and styling to match the design of your WordPress theme.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.
Best Practices for Excluding Categories from Related Posts
Here are a few best practices to keep in mind when excluding categories from the WordPress related posts:
-
Regularly review excluded categories: Periodically review the categories you've excluded to ensure they still align with your content strategy. Categories that were once sensitive or promotional may become more relevant over time, and you may want to include them in the related posts.
-
Consider using tags instead of categories: Instead of excluding entire categories, you could consider using tags to better control the related posts. This gives you more granular control over which content is displayed in the related posts section.
-
Monitor the impact on user engagement: Excluding categories from the related posts may have an impact on user engagement, such as reduced time on site or lower bounce rates. Monitor your website's analytics to ensure that the changes you've made are having a positive impact on your overall content strategy.
-
Provide alternative content recommendations: If you're excluding certain categories from the related posts, consider providing alternative content recommendations, such as popular posts or editor's picks, to ensure that your visitors have engaging content to explore.
By following these best practices, you can effectively exclude specific categories from the WordPress related posts while maintaining a positive user experience and supporting your overall content strategy.
Flowpoint.ai can help you identify the technical errors and optimization opportunities that may be impacting your website's related posts functionality and overall user engagement. Our AI-powered analytics can provide actionable insights to help you improve your content strategy and drive better conversion rates