Custom Fields
Re-Envisioned
Support MB Relationships MB Relationships – Using each_connected multiple times

This topic contains 3 replies, has 2 voices, and was last updated by  Anh Tran 1 year, 8 months ago.

  • Creator
    Topic
  • #8357
    Resolved
    ecurtain
    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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #8375

    Anh Tran
    Keymaster

    Hello,

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

    View post on imgur.com

    Can you check your code again please?

    #8388

    ecurtain
    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 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 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.