How to display related products only the primary category (product carousel element)

This topic has 4 replies, 2 voices, and was last updated 2 months, 3 weeks ago ago by Luca Rossi

  • Avatar: kmc
    kmc
    Participant
    September 15, 2025 at 09:53

    I want to display product sliders with “related products” only those in its same primary category (I set using Yoast SEO), however using this snippet it doesn’t work, can you help to check what is wrong, thanks!

    add_filter( ‘woocommerce_product_related_posts_query’, ‘ml_related_products_primary_or_default’, 10, 2 );
    function ml_related_products_primary_or_default( $query, $product_id ) {

    // Make sure Yoast SEO Exist
    if ( ! class_exists( ‘WPSEO_Primary_Term’ ) ) {
    return $query;
    }

    // Get Primary Category
    $primary_term = new WPSEO_Primary_Term( ‘product_cat’, $product_id );
    $primary_cat_id = $primary_term->get_primary_term();

    // fallback
    if ( ! $primary_cat_id || $primary_cat_id <= 0 ) { return $query; } // Use Primary Category $primary_args = $query; $primary_args['tax_query'] = array( array( 'taxonomy' => ‘product_cat’,
    ‘field’ => ‘term_id’,
    ‘terms’ => array( $primary_cat_id ),
    ),
    );

    $related_products = get_posts( $primary_args );

    // At least 6 paroducts to use Yoast Primary Category
    if ( count( $related_products ) < 6 ) { return $query; // Back to WooCommerce default } return $primary_args; // }

    3 Answers
    Avatar: Justin
    Luca Rossi
    Support staff
    September 16, 2025 at 06:52

    Dear @kmc,

    We hope this message finds you well.

    As an alternative, you may consider using the following filter: woocommerce_related_products_args

    Please find the example code below for your reference:

    
    $args = apply_filters( 'woocommerce_related_products_args', array(
        'post_type'           => 'product',
        'ignore_sticky_posts' => 1,
        'no_found_rows'       => 1,
        'posts_per_page'      => $posts_per_page,
        'orderby'             => $orderby,
        'post__in'            => $related,
        'post__not_in'        => array( $product->get_id() )
    ) );
    

    Should you have any further questions or require additional assistance, please do not hesitate to reach out.

    Best regards,
    The 8Theme Team

    Avatar: kmc
    kmc
    Participant
    September 16, 2025 at 07:02

    Sorry I am not very good at programing, I used this code with fall back, but seems it doesn’t work.

    Please contact administrator
    for this information.
    Avatar: Justin
    Luca Rossi
    Support staff
    September 16, 2025 at 12:01

    Dear @kmc,

    We hope this message finds you well.

    Please try using the following code snippet to filter related products by the primary category:

    
    add_filter( 'woocommerce_products_widget_query_args', 'filter_related_products_by_primary_category', 999, 1 );
    
    function filter_related_products_by_primary_category( $args ) {
        global $product;
        if ( ! $product ) {
            return $args;
        }
    
        // 1. Get Yoast Primary Category
        $primary_cat_id = get_post_meta( $product->get_id(), '_yoast_wpseo_primary_product_cat', true );
    
        // 2. If no primary category is set, fall back to the default
        if ( ! $primary_cat_id ) {
            $terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
            if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
                $primary_cat_id = $terms[0]->term_id;
            }
        }
    
        // 3. Add tax_query to filter by category
        if ( $primary_cat_id ) {
            $args['tax_query'][] = array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => array( $primary_cat_id ),
            );
        }
    
        return $args;
    }
    

    Please note that any further customization beyond this may fall outside the scope of our standard support services.

    Thank you for your understanding.

    Best regards,
    The 8Theme Team

  • Viewing 4 results - 1 through 4 (of 4 total)

The issue related to '‘How to display related products only the primary category (product carousel element)’' has been successfully resolved, and the topic is now closed for further responses

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.