Custom Fields
Re-Envisioned
Support MB Relationships Duplicate posts returned from relationship query

This topic contains 1 reply, has 2 voices, and was last updated by  Anh Tran 3 weeks, 1 day ago.

  • Creator
    Topic
  • #14719
    Resolved
    ecurtain
    Participant

    In our product we have custom post types for “products” and “groups”, and an mb_relationship “product_to_group”

    On the group archive page, we want to include products linked to current group as well as the group’s children.

    So the archive code first generates an array containing the group id, and the group’s children’s id’s.
    $group_array = array(1,2,3,4); // where 1=current id; 2,3,4=children

    The we perform our main query:

        $the_query = new WP_Query( array (
            'post_type'      => 'product',
            'post_status'  => 'publish',
            'posts_per_page' => -1,
            'relationship' => array(
                'id' => 'product_to_group',
                'to' => $group_array, 
            ),
        ) );
    
        if ( $the_query->have_posts() ) {
    
            while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
                // Process the post
    
            endwhile; 
    
        }
    

    The problem we are seeing is duplicate products. I think what’s happening is if a product is in multiple groups, it appears in the results for each matching group.

    My workaround will be to use a $used_products array to skip over duplicates, but this feels like a band-aid.

    Am I using the plugin incorrectly? Is this a bug / feature?

    Thanks

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #14764

    Anh Tran
    Keymaster

    Hi, it’s a bug when running the SQL. I’ve just fixed it here. Can you try it?

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.