Support Forum
Support › MB Relationships › Query advanced select field by relationshipResolved
Hi,
I'm trying to query an Advanced Select field by relationship.
I have three CPTs: Events, Courses, and Scholars. Scholars are connected to Courses. Courses are connected to Events. I was able to populate the Advanced Select field (located on the Event CPT) with the Course CPT listings; however, it loads in all of them. I would like for it to load only the ones connected via relationship to the Event.
I've tried many variations with no luck; but, this is my code for the time being:
<?php
add_filter( 'rwmb_meta_boxes', 'qrsi_select_advanced' );
function qrsi_select_advanced( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'QRSI2', 'your-text-domain' ),
'id' => 'qrsi2',
'post_types' => ['events'],
'include' => [
'relation' => 'OR',
'event-category' => [15],
],
'fields' => array(
array(
'name' => __( 'Courses Block 1', 'your-text-domain' ),
'id' => $prefix . 'courses_block_1',
'type' => 'post',
'post_type' => array( 'course' ),
'field_type' => 'select_advanced',
'placeholder' => __( 'Select an Item', 'your-text-domain'),
'query_args' => array(
MB_Relationships_API::get_connected( [
'id' => 'event-to-course-relationship',
'from' => get_the_ID(),
] ),
'post_status' => 'publish',
'posts_per_page' => - 1,
'order' => 'ASC',
'orderby' => 'name',
)
)
)
];
return $meta_boxes;
}
?>
After I figure this out, I would like to add another Advanced Select field that shows only Scholars in the relationship to the Course.
Any help would be greatly appreciated.
Thanks,
Ryan
Hi Ryan,
"Courses are connected to Events" so there is a relationship meta box shows the courses
connect to the current event
and you can add more courses
connected. Why do you need to create a new field post
which works like the relationship?
The difference is the field post
saves the value as post meta in the table wp_postmeta
while the relationship saves the value in a custom table wp_mb_relationships
.
Each event has around 20 courses, over the span of 3-5 days. Each day on the schedule contains at least 4 courses, each course with one scholar (some courses are taught by several scholars, depending on the year).
From what I can tell, there's no easy way to group the courses by day (which change per event). While a taxonomy or different relationships would work, it is much easier to do it via the Advanced Select Field. Please trust me on this - this event system is complicated and it needs to be user-friendly.
What code would I need to achieve what was mentioned in the original post?
Thank you very much for your time and any assistance,
Ryan
I still don't have the solution - this is still not working as wanted and assistance is still needed.
I was able to query courses connected to an event; but, only by manually inputting the Post ID. See below:
<?php
add_filter( 'rwmb_meta_boxes', 'qrsi_select_advanced' );
function qrsi_select_advanced( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'QRSI2', 'your-text-domain' ),
'id' => 'qrsi2',
'post_types' => ['events'],
'include' => [
'relation' => 'OR',
'event-category' => [15],
],
'fields' => array(
array(
'name' => __( 'Courses Block 1', 'your-text-domain' ),
'id' => $prefix . 'courses_block_1',
'type' => 'post',
'post_type' => 'course',
'field_type' => 'select_advanced',
'placeholder' => __( 'Select an Item', 'your-text-domain'),
'query_args' => array(
'relationship' => array(
'id' => 'event-to-course-relationship',
'from' => 60,
),
'post_status' => 'publish',
'posts_per_page' => - 1,
'order' => 'ASC',
'orderby' => 'name',
)
)
)
];
return $meta_boxes;
}
?>
Neither get_the_ID()
or get_queried_object_id()
worked. I was unable to figure out the $from
and $to
IDs mentioned in the documentation – there were no examples that showed how to find what the or what they would be. It just said to use them, which isn't a useful direction to provide. Nonetheless, I tried from_event-to-course-relationship
and event-to-course-relationship_from
as options. Neither worked.
Manually inputting the events doesn't really work for this purpose. What's the correct way to do this?
I appreciate the concern behind the reasoning of why; but, right now, I need the how more than anything. I can further expand on the reason after I get this code working as intended.
Thank you.
Hi,
If you want to get the current post ID when registering the meta box, please use this code
$post_id = null;
if ( isset( $_GET['post'] ) ) {
$post_id = intval( $_GET['post'] );
} elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = intval( $_POST['post_ID'] );
}
Refer to this topic https://support.metabox.io/topic/get-the-post-id-as-value/
function qrsi_select_advanced( $meta_boxes ) {
$prefix = '';
$post_id = null;
if ( isset( $_GET['post'] ) ) {
$post_id = intval( $_GET['post'] );
} elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = intval( $_POST['post_ID'] );
}
$meta_boxes[] = [
...
'fields' => array(
array(
'name' => __( 'Courses Block 1', 'your-text-domain' ),
'id' => $prefix . 'courses_block_1',
'type' => 'post',
'post_type' => 'course',
'field_type' => 'select_advanced',
'placeholder' => __( 'Select an Item', 'your-text-domain'),
'query_args' => array(
'relationship' => array(
'id' => 'event-to-course-relationship',
'from' => $post_id,
),
)
)
)
];
return $meta_boxes;
}
Let me know if it works.
Thank you so much – this worked perfectly. I submitted the link to the old rendition of the website if you want to take a look at the setup. I can explain further why I feel this was the best choice if you'd like.
I was also able to filter the other Advanced Select field based off scholars linked to courses using some additional code:
$course_args = array(
'post_type' => 'course',
'relationship' => array(
'id' => 'event-to-course-relationship',
'from' => $post_id,
),
);
$course_query = new WP_Query($course_args);
if ($course_query->have_posts()) {
while ($course_query->have_posts()) {
$course_query->the_post();
$course_post_id = get_the_ID();
}
}
And:
'query_args' => array(
'relationship' => array(
'id' => 'from-course-to-scholar',
'from' => $course_post_id,
),
The whole code is located here:
https://pastebin.com/4sayJwyR
Thank you again for your help. And just to make sure, is there anything wrong with the code I used? Will it cause any errors?
I spoke too soon about my solution for Scholars working – it only pulls in one scholar connected to one course, regardless of any others. What do I need to change so the Advanced Select field creates options for any/all scholars in a relationship with the courses on the event?
Thank you so much for any time and assistance.
Hi Ryan,
The parameter from
or to
accepts the array of object IDs, you can create an array of course
IDs and assign it to the relationship from-course-to-scholar
$course_post_id = array();
if ( $course_query->have_posts() ) {
while ($course_query->have_posts()) {
$course_query->the_post();
array_push( $course_post_id, get_the_ID() );
}
}
'query_args' => array(
'relationship' => array(
'id' => 'from-course-to-scholar',
'from' => $course_post_id
),
)
I can't thank you enough for your help. Everything is working as expected – loving the "multiple" option as well. Everything is solved now, and I've learned so much from your responses and from tackling this project.
Thank you again and have a wonderful day!
You are welcome 🙂
Hi,
I know the Meta Box team is on vacation, so I don't want to bother them. They deserve some time off and I'm posting this here in case anyone has encountered this or knows what to do.
I tried writing my issue out a few times with no luck, so I made a quick video trying to show the problem I'm facing.
The video: https://www.loom.com/share/144db4819f284c7eb41535bc1a9b016c
The code: https://pastebin.com/aYjmCN0q
Does anyone have suggestions or ideas on what could be going wrong?