This Is How to Fix WordPress's "Getting Related Articles" Feature That's Not Working
One of the most frustrating issues WordPress developers can face is when the "Related Articles" feature on their site just doesn't seem to be working correctly. The articles being displayed are irrelevant, or nothing is showing up at all.
There are quite a few potential coding errors that could be the culprit, and identifying the right one is key to getting your related content functioning properly. In this article, we'll dive into the most common issues and provide step-by-step solutions to fix them.
The Importance of Related Content
Before we get into the technical details, let's quickly discuss why the "Related Articles" feature is so important for WordPress sites in the first place.
Providing relevant, related content to your visitors is a crucial part of improving user experience and boosting key metrics like time on page and pages per session. When readers can easily navigate from one article to the next on your site, it encourages them to stick around and consume more of your content.
This not only keeps them engaged, but also increases the chances they'll convert, whether that's signing up for your email list, making a purchase, or some other desired action. Studies have shown that sites with effective related content features see 20-40% more pageviews on average.
From an SEO perspective, related articles also help search engines better understand the topical relevance and overall structure of your website. This can lead to improved rankings and visibility in the SERPs.
In short, getting your "Related Articles" working correctly is well worth the effort. Let's dive into how to do that.
Common Coding Errors and How to Fix Them
The most likely reasons your "Related Articles" feature isn't functioning as expected fall into a few main categories. Let's address each one in turn.
1. Incorrect Query Arguments
One of the most common issues is having the wrong arguments defined in the WordPress query that retrieves the related posts. This can lead to the wrong content being displayed, or no content at all.
Here's an example of what your code might look like:
$related = new WP_Query( array(
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 3,
'post__not_in' => array($post->ID)
) );
There are a few problems with this:
$post->ID
should be replaced with get_the_ID()
to ensure the current post ID is always used, rather than relying on a global $post
variable.
numberposts
is an outdated argument that has been replaced by the more commonly used posts_per_page
.
- The argument should be
post__not_in
, not post_not_in
.
Here's the refactored version that addresses these issues:
$categories = wp_get_post_categories(get_the_ID());
$args = array(
'category__in' => $categories,
'posts_per_page' => 3,
'post__not_in' => array(get_the_ID())
);
$related = new WP_Query($args);
This query will retrieve up to 3 posts that are in the same categories as the current post, excluding the current post itself.
2. Incorrect Category Filtering
Another common issue is related to how the categories are being filtered. If the wp_get_post_categories()
function is not returning the right categories, the related posts query will be off-target.
For example, let's say a post is assigned to multiple categories, but the code is only looking at the first category. This could lead to related posts being shown that don't actually match the current post's topics.
To fix this, you can modify the query to include all categories the post is assigned to:
$categories = wp_get_post_categories(get_the_ID());
$args = array(
'category__in' => $categories,
'posts_per_page' => 3,
'post__not_in' => array(get_the_ID())
);
$related = new WP_Query($args);
By using the category__in
parameter and passing in the full list of categories, you ensure the related posts query is as accurate as possible.
3. Incorrect Taxonomy Usage
In some cases, the issue may not be with the post categories at all, but rather with a custom taxonomy being used incorrectly.
For example, let's say your site has a "Tags" taxonomy that you want to use for related content. Your code might look like this:
$tags = wp_get_post_tags($post->ID);
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args = array(
'tag__in' => $tag_ids,
'posts_per_page' => 3,
'post__not_in' => array($post->ID)
);
$related = new WP_Query($args);
However, this approach has a few potential issues:
- Again,
$post->ID
should be replaced with get_the_ID()
.
- The
tag__in
parameter is looking for tag IDs, when it should be using the tag_slug__in
parameter instead.
Here's the corrected version:
$tags = wp_get_post_tags(get_the_ID());
$tag_slugs = array();
foreach($tags as $tag) $tag_slugs[] = $tag->slug;
$args = array(
'tag_slug__in' => $tag_slugs,
'posts_per_page' => 3,
'post__not_in' => array(get_the_ID())
);
$related = new WP_Query($args);
By using tag_slug__in
instead of tag__in
, we're ensuring the query looks for posts with the same tags as the current post, rather than just the tag IDs.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.
4. Incorrect Template Usage
Finally, it's possible that the issue isn't with the code itself, but rather how that code is being used within your WordPress theme templates.
For example, if the "Related Articles" section is being included in a template file that doesn't have access to the necessary global variables (like $post
), the related posts query won't work correctly.
Make sure you're including the related posts code in the appropriate template file, and that all necessary variables are accessible. A common best practice is to create a dedicated template part (e.g. content-related.php
) that can be included wherever you need the related content to appear.
Putting It All Together
Let's recap the key steps to fixing a broken "Related Articles" feature in WordPress:
- Check your query arguments: Ensure you're using the correct parameters, like
posts_per_page
instead of numberposts
, and post__not_in
instead of post_not_in
.
- Validate your category filtering: Make sure the
wp_get_post_categories()
function is returning all the relevant categories for the current post.
- Confirm your taxonomy usage: If you're using custom taxonomies like tags, double-check that you're using the right parameters (e.g.
tag_slug__in
instead of tag__in
).
- Verify your template placement: Ensure the related posts code is included in the appropriate template file and has access to all necessary variables.
By addressing these common coding errors, you should be able to get your WordPress "Related Articles" feature working correctly and providing a great user experience for your site visitors.
If you're still having trouble, consider using a tool like Flowpoint.ai to analyze your website's technical performance and user behavior. Flowpoint can help identify all the issues impacting your related content, and even generate specific recommendations to fix them