Trying to display all the possible connection to a post
Support › MB Relationships › Trying to display all the possible connection to a post
- This topic has 7 replies, 2 voices, and was last updated 6 years, 2 months ago by
Anh Tran.
-
AuthorPosts
-
February 13, 2019 at 9:52 AM #13283
Vee Jay
ParticipantHere's my code
$my_query = new WP_Query( array( 'post_type' => array( 'post', 'page', 'conference', 'publication', 'press'), 'post_status' => array ('future', 'publish'), 'paged' => $paged, 'posts_per_page' => 24 ) ); MB_Relationships_API::each_connected( array( 'id' => 'conf2pub', 'from' => $wp_query->posts, 'property' => 'connected_confs', ) ); MB_Relationships_API::each_connected( array( 'id' => 'post2pub', 'from' => $wp_query->posts, 'property' => 'connected_posts', ) ); MB_Relationships_API::each_connected( array( 'id' => 'pub_and_press', 'from' => $wp_query->posts, 'property' => 'connected_press_items', ) ); while ( $my_query->have_posts() ) : $my_query->the_post(); // Display connected pages foreach ( $post->connected as $post ) : setup_postdata( $post ); the_title(); endforeach; wp_reset_postdata(); // Set $post back to original post. endwhile;
What am I doing wrong? I followed the documentation but it's not working...
February 13, 2019 at 10:48 AM #13286Vee Jay
ParticipantI even tried the following
$related = new WP_Query( array( 'relationship' => array( 'relation' => 'OR', array( 'id' => 'conf2pub', 'to' => get_the_ID(), ), array( 'id' => 'post2pub', 'to' => get_the_ID(), ), array( 'id' => 'pub_and_press', 'from' => get_the_ID(), ), ), 'nopaging' => true, )); while ( $related->have_posts() ) : $related->the_post(); the_title(); endwhile; wp_reset_postdata();
February 13, 2019 at 1:50 PM #13295Anh Tran
KeymasterHi Vee Jay,
In
MB_Relationships_API::each_connected
, you setproperty
different for each connection. So to get them all, use this code:while ( $my_query->have_posts() ) : $my_query->the_post(); // Display connected pages foreach ( $post->connected_confs as $post ) : setup_postdata( $post ); the_title(); endforeach; wp_reset_postdata(); foreach ( $post->connected_posts as $post ) : setup_postdata( $post ); the_title(); endforeach; wp_reset_postdata(); foreach ( $post->connected_press_items as $post ) : setup_postdata( $post ); the_title(); endforeach; wp_reset_postdata(); endwhile;
February 14, 2019 at 9:10 AM #13316Vee Jay
ParticipantI tried this and it still doesn't work :
$my_query = new WP_Query( array( 'post_type' => array( 'post', 'page', 'conference', 'publication', 'press'), 'post_status' => array ('future', 'publish'), 'paged' => $paged, 'posts_per_page' => 24 ) ); MB_Relationships_API::each_connected( array( 'id' => 'conf2pub', 'from' => $my_query->posts, 'property' => 'connected_confs', ) ); MB_Relationships_API::each_connected( array( 'id' => 'post2pub', 'from' => $my_query->posts, 'property' => 'connected_posts', ) ); MB_Relationships_API::each_connected( array( 'id' => 'pub_and_press', 'from' => $my_query->posts, 'property' => 'connected_press_items', ) ); while ( $my_query->have_posts() ) : $my_query->the_post(); foreach ( $post->connected_confs as $post ) : setup_postdata( $post ); the_title(); endforeach; wp_reset_postdata(); foreach ( $post->connected_posts as $post ) : setup_postdata( $post ); the_title(); endforeach; wp_reset_postdata(); foreach ( $post->connected_press_items as $post ) : setup_postdata( $post ); the_title(); endforeach; wp_reset_postdata(); endwhile;
I get a " Invalid argument supplied for foreach() in " error.
February 14, 2019 at 3:33 PM #13323Anh Tran
KeymasterHi Vee Jay, I've tried the code and it works for me. Here is my video record: http://recordit.co/1e1j1PkNs5
And here is the code I used to test: https://ghostbin.com/paste/kyufr
February 18, 2019 at 1:09 AM #13389Vee Jay
ParticipantI think I understand what the problem is.
You are using this code to display in an archive page where all your posts are listed. But what about when you are inside a single post?
Would the code change? Here's what I have in both function.php and the single post: https://ghostbin.com/paste/ksgheFebruary 18, 2019 at 3:58 AM #13390Vee Jay
ParticipantI can get it to work like this:
$connected_conf2pub = new WP_Query( array( 'relationship' => array( 'id' => 'conf2pub', 'to' => get_the_ID(), // You can pass object ID or full object ), 'nopaging' => true, ) ); while ( $connected_conf2pub->have_posts() ) : $connected_conf2pub->the_post(); echo 'Connected Conf: <a href="' . get_permalink($post->ID) . '"><p>' . get_the_title($post->ID) . '</p></a>'; endwhile; wp_reset_postdata(); $connected_post2pub = new WP_Query( array( 'relationship' => array( 'id' => 'post2pub', 'to' => get_the_ID(), // You can pass object ID or full object ), 'nopaging' => true, ) ); while ( $connected_post2pub->have_posts() ) : $connected_post2pub->the_post(); echo 'Connected Posts: <a href="' . get_permalink($post->ID) . '"><p>' . get_the_title($post->ID) . '</p></a>'; endwhile; wp_reset_postdata(); $connected_pub2press = new WP_Query( array( 'relationship' => array( 'id' => 'pub_and_press', 'from' => get_the_ID(), // You can pass object ID or full object ), 'nopaging' => true, ) ); while ( $connected_pub2press->have_posts() ) : $connected_pub2press->the_post(); echo 'Connected Press: <a href="' . get_permalink($post->ID) . '"><p>' . get_the_title($post->ID) . '</p></a>'; endwhile; wp_reset_postdata();
But would like to be able to display all the connections as one for design purposes. I don't really need to identify what's what, I just want to display all connections in date order.
February 18, 2019 at 8:47 AM #13394Anh Tran
KeymasterHi Vee Jay,
In the first post, you used the
each_connected
function to demo your purpose. So, I thought you were working on an archive template. It's great that you have resolved the problem for single post.Regarding to list all posts in all relationships at once, we're working on that. It'll be available in the next version of the plugin.
-
AuthorPosts
- You must be logged in to reply to this topic.