How to get related posts in admin area?
Support › MB Relationships › How to get related posts in admin area?Resolved
- This topic has 6 replies, 2 voices, and was last updated 2 years, 7 months ago by
John Rood.
-
AuthorPosts
-
August 16, 2022 at 10:42 PM #37529
John Rood
ParticipantWe are trying to populate a MB HTML field with some static data about related posts. We have the callback function working fine, as I am able to return the main post's ID in the field. However, upon attempting to use the methods described in the docs, I am unable to return any related posts. I am assuming this is because the MB API only works on the frontend, so I am wondering how the Connected To: section works in the post editor.
All i really need to do is return the list of related posts with a link to each one. If you could help me get the relationship, I think I can handle the rest.
Thanks!
August 17, 2022 at 11:11 AM #37541Long Nguyen
ModeratorHi John,
It is possible to query and get related posts in the backend/admin area. You can just use the WP Query to do that, please refer to this topic https://support.metabox.io/topic/query-advanced-select-field-by-relationship/
August 18, 2022 at 12:45 AM #37554John Rood
ParticipantIs the issue that I'm using a callback function in an HTML field then? We are only trying to show a list of related posts with a link to edit, however, when I use the following function as a callback, I get an empty array.
function ndig_get_accepted_offer_admin(){ $results = array(); global $post; $postID = $post->ID; $connected = new WP_Query( [ 'relationship' => [ 'id' => 'listing-to-offer-relationship', 'from' => $postID, // You can pass object ID or full object ], 'nopaging' => true, ] ); while ( $connected->have_posts() ) : $connected->the_post(); $results[] = '<a href="'.the_permalink().'">'.the_title().'</a>'; endwhile; //$pretty_results = implode(" ",$results); return var_dump($results); wp_reset_postdata(); }
August 19, 2022 at 8:54 AM #37576Long Nguyen
ModeratorHi John,
You should create a query to get posts in the callback function that register the meta box, like this
function ndig_get_accepted_offer_admin(){ $post_id = null; if ( isset( $_GET['post'] ) ) { $post_id = intval( $_GET['post'] ); } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = intval( $_POST['post_ID'] ); } $connected = new WP_Query( [ 'relationship' => [ 'id' => 'listing-to-offer-relationship', 'from' => $post_id, // You can pass object ID or full object ], 'nopaging' => true, ] ); $results = ''; while ( $connected->have_posts() ) : $connected->the_post(); $results .= '<a href="'.the_permalink().'">'.the_title().'</a>'; endwhile; $meta_boxes[] = [ ... 'fields' => [ [ 'type' => 'custom_html', 'std' => $results, ], ] ]; return $meta_boxes; }
August 19, 2022 at 10:06 PM #37946John Rood
ParticipantI will try this out and report back once I know more, but I think this should do it. Thank you for the clarification. I hadn't thought about doing it this way.
August 23, 2022 at 11:08 PM #37996John Rood
ParticipantThis worked perfectly with some modifications for our use case. One thing to note if anyone else find this helpful is that I changed your loop from
while ( $connected->have_posts() ) : $connected->the_post();
to
if( $connected->have_posts()) : while( $connected->have_posts()) : $connected->the_post();
so that I could do anelse
in the event that no posts were found. Thanks for your help, you can mark this resolved!August 23, 2022 at 11:11 PM #37997John Rood
ParticipantI also had to pass in the existing
meta_boxes
variable at the beginning of the function to include fields that I had previously added with the builder. Just in case anyone else is in a similar scenario. If you don't do that, this will be the only meta box that shows.add_filter( 'rwmb_meta_boxes', function($meta_boxes){ ...code goes here
instead of
add_filter( 'rwmb_meta_boxes', function(){ ...code goes here
-
AuthorPosts
- You must be logged in to reply to this topic.