MB Relationships - Using each_connected multiple times
Support › MB Relationships › MB Relationships - Using each_connected multiple timesResolved
- This topic has 3 replies, 2 voices, and was last updated 7 years, 2 months ago by
Anh Tran.
-
AuthorPosts
-
January 26, 2018 at 9:54 PM #8357
ecurtain
ParticipantI'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!
January 29, 2018 at 8:46 AM #8375Anh Tran
KeymasterHello,
I've just tested again with a similar code and here is my result:
Can you check your code again please?
January 29, 2018 at 11:26 PM #8388ecurtain
ParticipantHello 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.January 31, 2018 at 2:13 PM #8410Anh Tran
KeymasterNo 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();
-
AuthorPosts
- You must be logged in to reply to this topic.