MB Relationships - Using each_connected multiple times

Support MB Relationships MB Relationships - Using each_connected multiple timesResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #8357
    ecurtainecurtain
    Participant

    I'm having trouble setting up the each_connected multiple times example. My project has three post types - products, groups, manufacturers - and I've defined products_to_groups and products_to_manufacturers relations.

    The single pages work as expected and correctly display the linked items.

    The archive page is where it has problems. I set up the loop:

    
        $my_query = new WP_Query( array( 
            'post_type' => 'products' 
        ) );
    

    And get the connected items:

    
        MB_Relationships_API::each_connected( array(
            'id'   => 'products_to_groups',
            'from' => $my_query->posts, 
            'property' => 'connected_groups',
        ) );
    
        MB_Relationships_API::each_connected( array(
            'id'   => 'products_to_manufacturers',
            'from' => $my_query->posts, 
            'property' => 'connected_manufacturers',
        ) );
    

    And try to perform the loop, displaying the connected items:

        
        while ( $my_query->have_posts() ) : $my_query->the_post();
    
            foreach ( $post->connected_groups as $post ) : setup_postdata( $post );
                the_title(); 
            endforeach;
            wp_reset_postdata(); 
    
            foreach ( $post->connected_manufacturers as $post ) : setup_postdata( $post );
                the_title();  
            endforeach;
            wp_reset_postdata();  
    
        endwhile; 
    

    The first foreach works and shows the connected groups. The second fails with an "invalid argument" warning. If I switch the inner foreach loops, the first succeeds & the second fails again.

    What am I doing wrong? Thanks!

    #8375
    Anh TranAnh Tran
    Keymaster

    Hello,

    I've just tested again with a similar code and here is my result:

    https://imgur.com/a/3FpVk

    Can you check your code again please?

    #8388
    ecurtainecurtain
    Participant

    Hello Anh,

    Thanks for your reply. I had a hard time following the animated gif because it zips through the code quickly. May I suggest you post the code from the gif as an example? It would be much easier to read.

    I'm afraid I'm unable to get it to work - I'm getting the same foreach warnings.
    For now, I'll need to keep using my current solution.

    #8410
    Anh TranAnh Tran
    Keymaster

    No problem. Here is the code for registering relationships:

    add_action( 'mb_relationships_init', function () {
    	MB_Relationships_API::register( array(
    		'id'   => 'posts_to_pages',
    		'from' => 'post',
    		'to'   => 'page',
    	) );
    	MB_Relationships_API::register( array(
    		'id'   => 'posts_to_posts',
    		'from' => 'post',
    		'to'   => 'post',
    	) );
    } );

    And here is the code to fetch connections (in index.php file):

    global $wp_query;
    MB_Relationships_API::each_connected( array(
    	'id'       => 'posts_to_pages',
    	'from'     => $wp_query->posts,
    	'property' => 'connected_pages',
    ) );
    
    MB_Relationships_API::each_connected( array(
    	'id'       => 'posts_to_posts',
    	'to'       => $wp_query->posts,
    	'property' => 'connected_posts',
    ) );

    Here is the code in the content.php file:

    foreach ( $post->connected_pages as $post ) : setup_postdata( $post );
    	the_title();
    endforeach;
    wp_reset_postdata();
    
    echo '<hr>';
    
    foreach ( $post->connected_posts as $post ) : setup_postdata( $post );
    	the_title();
    endforeach;
    wp_reset_postdata();
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.