Support Forum
Support › MB Relationships › How to get related posts in admin area?Resolved
We 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!
Hi 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/
Is 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();
}
Hi 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;
}
I 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.
This 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 an else
in the event that no posts were found. Thanks for your help, you can mark this resolved!
I 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