Support

Account

Home Forums General Issues WordPress Query orderby ACF Relationsship Reply To: WordPress Query orderby ACF Relationsship

  • 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.