Query advanced select field by relationship
Support › MB Relationships › Query advanced select field by relationshipResolved
- This topic has 10 replies, 2 voices, and was last updated 3 years, 2 months ago by
Ryan Maietta.
-
AuthorPosts
-
January 25, 2022 at 8:26 AM #33440
Ryan Maietta
ParticipantHi,
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,
RyanJanuary 25, 2022 at 10:35 PM #33452Long Nguyen
ModeratorHi Ryan,
"Courses are connected to Events" so there is a relationship meta box shows the
courses
connect to the currentevent
and you can add morecourses
connected. Why do you need to create a new fieldpost
which works like the relationship?The difference is the field
post
saves the value as post meta in the tablewp_postmeta
while the relationship saves the value in a custom tablewp_mb_relationships
.January 26, 2022 at 4:34 AM #33468Ryan Maietta
ParticipantEach 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,
RyanJanuary 26, 2022 at 5:10 PM #33480Ryan Maietta
ParticipantI 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()
orget_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 triedfrom_event-to-course-relationship
andevent-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.
January 26, 2022 at 7:16 PM #33482Long Nguyen
ModeratorHi,
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.
January 27, 2022 at 6:28 AM #33493Ryan Maietta
ParticipantThank 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/4sayJwyRThank you again for your help. And just to make sure, is there anything wrong with the code I used? Will it cause any errors?
January 27, 2022 at 7:06 AM #33494Ryan Maietta
ParticipantI 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.
January 27, 2022 at 1:24 PM #33501Long Nguyen
ModeratorHi Ryan,
The parameter
from
orto
accepts the array of object IDs, you can create an array ofcourse
IDs and assign it to the relationshipfrom-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 ), )
January 27, 2022 at 3:05 PM #33504Ryan Maietta
ParticipantI 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!
January 28, 2022 at 10:10 AM #33530Long Nguyen
ModeratorYou are welcome 🙂
January 30, 2022 at 1:47 PM #33572Ryan Maietta
ParticipantHi,
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?
-
AuthorPosts
- You must be logged in to reply to this topic.