Rizki Pratama A former programmer • rizkipratama.com

Change Jetpack’s Related Posts Category to the Primary One from Yoast SEO

2 min read

Jetpack's Related Posts feat. Yoast SEO's Primary Category

One small tweak can change the way a post catches the visitor attention, and that includes adding a filter to just modify one line of information of a post, or integrating one with another plugin features. But doing that small act can be pretty tough when the process to achieve it is a way longer than expected, especially when you find yourself lost looking up for the documentation.

For example, I have two plugins installed: Jetpack and Yoast SEO. I want to integrate the feature of one with another. Let me tell you a story that made this post happen:

This one I got from a (really) quick sight, after seeing the category text on the Jetpack’s Related Posts. And then something caught my mind…

“Oh, it appears that the category text used by the Jetpack’s related posts is the first one found in the array iteration. I think I can make it more appealing to the reader by changing it to the primary one, since I am using the Yoast SEO plugin and it provides a primary category feature.”

Let’s get straight. The purpose here is that I want to change the category text that appears on the Jetpack’s Related Posts to the primary category I chose for this related post instead of the default one which is found first in the iteration of category array (look at the /wp-content/plugins/jetpack/modules/related-posts/jetpack-related-posts.php line 1148 inside _generate_related_post_context function), so for this illustration, the category text will change from In “Tutorial” (that you can see in Figure 1) to In “UWP (Universal Windows Platform)”, since the “UWP (Universal Windows Platform)” is the primary category of this post, that you can see in Figure 2.

Figure 1. Jetpack's Related Posts showing "Tutorial" category, which is the first one found in the iteration of category array
Figure 1. Jetpack’s Related Posts showing “Tutorial” category, which is the first one found in the iteration of category array
Figure 2. The primary category of the post is "UWP (Universal Windows Platform)"
Figure 2. The primary category of the post is “UWP (Universal Windows Platform)”

When I wanted to google the solution, it took me about 10 minutes to figure out the correct keyword until I found myself searching for keyword “jetpack related posts hook category”. Then I visited the second link, which tells you about the hook for customizing Jetpack’s Related Posts (written by the Jetpack team itself). That hook is jetpack_relatedposts_filter_post_context.

After that, I was searching for the way to retrieve the Yoast SEO primary category of a post. Then I found that it uses a _yoast_wpseo_primary_category meta key to get the primary category ID.

The complete code to achieve it is pretty straightforward. Just put this code to your theme’s functions.php.

<?php
/**
 * Modify Jetpack's Related Posts category context output, 
 * replacing it with the primary category (from Yoast SEO plugin).
 * Add this code to your theme's functions.php.
 */
function jp_rp_context_cat_primary_yoast( $context, $post_id ) {
    $primary_cat_id = get_post_meta( $post_id , '_yoast_wpseo_primary_category', true );
    if ( $primary_cat_id ) {
        $post_cat_name = get_the_category_by_ID( $primary_cat_id );
        if ( isset( $post_cat_name ) ) {
            $context = sprintf( 
                _x( 'In "%s"', 'in {category/tag name}', 'jetpack' ),
                $post_cat_name
            );
        }
    }
    return $context;
}
add_filter( 'jetpack_relatedposts_filter_post_context', 'jp_rp_context_cat_primary_yoast', 10, 2 );

The filter function looks for the Yoast SEO primary category ID (yoast_wpseo_primary_category) of the post from the post meta table. If it is found, retrieve the category name, set it to the context variable, and return it.

After I added the code above, the result is exactly what I wanted:

Figure 3. The category has been changed to the primary one
Figure 3. The category has been changed to the primary one

Actually there exist one specific hook to alter the category output (I found it by traversing the original hook before coding the solution), that is, jetpack_relatedposts_post_category_context, but it doesn’t include the post_id of the related post that made me unable to retrieve the Yoast SEO primary category post meta, so I moved one level up back to the jetpack_relatedposts_filter_post_context.


If you find something unclear or experience a few error, reach me by the comment section and ask whatever your problem regarding to this topic.

Rizki Pratama A former programmer • rizkipratama.com