- This topic has 3 replies, 2 voices, and was last updated 2 years, 7 months ago by .
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!
- You must be logged in to reply to this topic.