Home Forums General Issues acf/fields/relationship/query



  • Im sure this is an issue of data scope.. but I can’t even get it to work with Globals.

    The schema is Listings 1:m > < 1:m events 1:m > < 1:1 bookings.

    Trying to filter events where the event ID is in the listing file. $thePostID comes up empty. It works if I set it to a fixed (listing) post number. I believe $thePostID is changing or going out of scope.

    Anyone else found a way of filtering a relationship child to several parents?

    events are IDs in the related_viewing field.

    function event_post_object_filter($args, $field, $post_id) {
    global $wp_query;
    $thePostID = $wp_query->post->ID;
    $args[‘post_type’] = ‘events’ ;
    $args[‘post__in’] = get_field(‘related_viewing’,$thePostID, false);
    return $args ;

    add_filter(‘acf/fields/post_object/query/key=field_61e8d26c9f773’, ‘event_post_object_filter’,10,3) ;

  • You are trying to use the global $wp_query, this will not have any value during the AJAX request done to get the results.

    Not really sure what you are trying to do here. Are you trying to limit the posts returned based on the posts selected in another field?

  • Hi John

    Thankyou for getting back to me. I have read tons of your ACF work and use a couple of your plugins. I’m very grateful to you for posting a reply.

    Yes that is exactly what I’m trying to do; and are well in over my head! I’ve attached a JPG which probably explains it better. The area in blue is the parent “listing” post. In green are related viewing appointment times: “events.” In orange is an ACF form with a “booking” post. Within the booking post in purple is a related “events” field. Purple is supposed to allow the user to choose from the available event times (as seen in green). Purple currently lists all booking times posted for all events.

    At the top you can see a shortcode return: 207 is the ID of the listing (Blue) and the array 3515,4562 are the events times (green). I cannot seem to get the array loaded in the ACF [acf/fields/post_object/query/] filter. The post ID (207) comes back empty when in called inside the filter.

    I’ve tried using a global var to save the ID but this also seems to zero out.

    I’m beyond the limits of my present knowledge on how to fix this.

    Some other files also attached which you can take credit for and use as you see fit 🙂

  • .. and again with the correct zip file attached 🙂

  • Still not sure I completely understand. Are you trying to filter the post object field by a custom field on the same post where the field is shown or is it a custom field associated with the related post?

  • Hi John

    I think what I trying to do is something along the lines of a SQL innner join.

    I have a custom field in the ‘listing’ post from which I can pull an array of event IDs (all of whom related to that listing.)

    I’m trying to use that array to filter out unrelated events when the user fills out a booking form on the front end (where the user is choosing an event to book for.

    The upshot is that user’s list of choices for selecting an event to book for, are limited to the events related to the listing the user is looking at.

    One way of expressing it is that I’m trying to filter the child to choose a parent from multiple parents, all of whom have a single grandparent. So my filter has to omit all choices of parents who are not the children of a particular grandparent.

    Doe that make sense?

  • You need to set a meta query to filter the posts by the relationship field on the events. See 3. Multiple custom field values (array based values)

    Note that since the field stores ID values you also need to have your values in quotes. The value you are looking for is the current post ID.

    $args['meta_query'] = array(
        'key' => 'field_name', // name of relationship field on events that relates this post to the events
        'value' => '"'.$post_id.'"',
        'compare' => 'LIKE'
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.