Home › Forums › General Issues › WordPress Query orderby ACF Relationsship
I have 2 custom post types:
I am using an ACF Relationship field that works 2 way on the course date. On the sites home page there is a section that shows 4 most current course offerings. I am trying to order these by an ACF date field. This field is on the course_dates cpt, but the query is on course cpt and I am having trouble finding the solution to sort by the meta value.
So courses cpt has an acf relationship field course_course_dates that connects to the course_dates cpt.
course_dates cpt has the ACF date field course_date_start_date, when I run the query below it does not return any posts, any suggestions pointing me n the correct path would be greatly appreciated
$today = date( 'Ymd' ); $args =array( 'posts_per_page' => 4, 'post_type' => 'courses', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'course_course_dates', 'value' => '"' . get_the_ID() . '"', 'compare' => 'LIKE' ), array( 'key' => 'course_date_start_date', 'type' => 'DATE', 'value' => $today, 'compare' => '>' ), ), 'meta_key' => 'course_date_start_date', 'orderby' => 'meta_value', 'order' => 'ASC', ); $query = new WP_Query( $args );
It is not possible to do what you want to do with what you have. You cannot order posts by values of other posts, whether this be different post types or not.
post => related post => related post value
This is a WP thing and this type of query is not doable using WP_Query().
In order to do what you want you need to have the dates as meta values of courses.
This is doable, but has it’s own problems. For example the dates on courses will not match up if the dates on course dates are updated after the courses post is updated. To elaborate on this, if you create a “course date” post, then you create a “course” post related to this course date and then you go back an edit the “course date” post, then the date for course will not match the date on course date.
So this is what you need to to.
First you need to create an acf/save_post filter. In this filter you test to see if the post type is “course”. If it is then you get the related “course_date” and then get the date field from that post. You then save this date in another _postmeta field using standard WP add_post_meta(). I outlined doing this with repeater values here and it’s the same principle https://acfextras.com/dont-query-repeaters/
With the above in place you can query courses by these dates.
If you want to attempt to solve the non matching dates issue then what you would do is add to the filter you created above. If the post type is “course_date” then you get the list of all of the related courses and you update the _postmeta field you added in the first step to the new date.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.