Skip to main contentSkip to footer

Showing related posts from the same category

If you want the Related Posts to only include posts from the same category as the current post, you can’t adjust that from the Relevanssi Related Posts settings. It’s a simple thing to do with a little code snippet, though.

Add this function to your site:

add_filter( 'relevanssi_related_args', function( $args ) {
    global $post;
    $cats = wp_get_object_terms( $post->ID, 'category', array('fields' => 'ids') );
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => $cats,
        ),
    );
    return $args;
} );

This will make sure the Related Posts share at least one same category as the current post. If you want to use a different taxonomy, just replace the two occurrances of category with the slug of your custom taxonomy.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Related Posts:

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed