Best method to show Related Products using Yoast Primary Category

WooCommerce with Yoast SEO plugin is the best combination for your online store to have a good SEO score. Yoast SEO provides a primary category feature that helps you point out which product category is more important for a particular post if you select multiple product categories for the product.

As we have the primary product category for the product selected, it’s better to show products of the same primary product category as related products on the product detail page. Add below code in functions.php file of your active theme to show products from the same Yoast primary category as related products in the WooCommerce.

 * Show related products from Yoast Primary Category in WooCommerce
add_filter( 'woocommerce_get_related_product_cat_terms', function( $terms, $product_id ) {
    if ( function_exists( 'yoast_get_primary_term_id' ) ) {
        $primary_term_product_id = yoast_get_primary_term_id( 'product_cat', $product_id );
        if ( $primary_term_product_id ) {
            return array( $primary_term_product_id );
    return $terms;
}, 10, 2 );

Disclaimer: *This post may contain affiliate links. I may earn a small commission when you click on the links and buy the products with no additional cost to you. Thank you.


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments