Home Forums ACF PRO Relationship Field (Post Object) filtering


Relationship Field (Post Object) filtering

  • Hey,

    I have a post object custom field which I’m trying to filter based on a custom field.

    I use the acf/fields/post_object/query hook with the following query:
    ` $args[‘meta_key’] = ‘custom_field_key’;
    $args[‘meta_value’] = ‘some_value’;
    $args[‘meta_compare’] = ‘LIKE’;
    $args[‘post_type’] = ‘post’;

    I get no results and I’m wondering if it’s even doable.
    Did anyone try something similar and succeed?


  • I’m actually trying to do the same thing and having the same problem where no results show up. Did you end up figuring this out? Not sure what I’m doing wrong.

    as you can see. I’ve tried a few different ways and none seem to work. the field is set to show “documents” post type and I’m trying to filter by the documents custom field “rfi_project” and if it matches the current project.

    // filter RFI Drawing Reference for only this project
    function tmc_rfi_drawings_for_project( $args, $field, $post_id ) {
        $meta_query = array();
        $meta_query[] = array(
            'key' => 'rfi_project',
            'value' => get_the_ID()
        $args['meta_query'] = $meta_query;
        //$args['meta_key'] = "rfi_project";
        //$args['meta_value'] = $post_id;
        return $args;
    add_filter('acf/fields/post_object/query/key=field_59d435129094f', 'tmc_rfi_drawings_for_project', 10, 3);
  • @fishnyc22 your problem here is using get_the_ID(). This filter is run during an AJAX request and whatever that function is returning it’s not the ID of the post that’s being edited. ACF passes the post ID in the function call, which I see that you’ve tried to use in the commented out version. Try using that in the attempt that you do not have commented out.

    If this still fails, the post type you’re trying alter the query on a hierarchical post type were a child post can be related to the current post and the parent of that post may not be?

    Another problem could be the way that the field ‘rfi_project’ is stored. What type of field is this? Is it a relationship field or another post object field that can have multiple values? If it is then is is stored as a serialized array and you cannot an ‘==’ meta query and you need to use

    $meta_query[] = array(
      'key' => 'rfi_project',
      'value' => '".$post_id."',
      'compare' => 'LIKE'
  • Hey John. Thanks for the reply. Unfortunately the front-end form that I have created is set for “new_post” so “new_post” is sent in that value. What I need is the post_id of the page where the form is shown on the front end. OR I need a way to manipulate the Ajax query to add that value to the string so I can access it. The only way I’ve gotten this to work is with a SESSION variable which is messy. This is a password protected project management application that I’m building so that may be an OK solution but it’s not idea. If there was any other way to get this to work I’d love to hear suggestions.

  • This is where I generally bypass the pretty fields in ACF like post object and relationship fields and instead I opt for dynamically populated select fields. 99% of the time when I use a post object or relationship field I only get the post IDs and don’t use the built in ability of WP to get the actual posts and do all the other work myself. The, with the select fields I create my own AJAX requests through ACF to get the information for other fields. I have some examples of this here I just built a similar set of fields where one select field is only parent terms in a taxonomy and the second select field is dynamically populated with only the child terms of the parent term selected. Might this be possible with ACF alone? Maybe, but it took me a whole lot less time to build it myself than it would have to figure out the hoops I’d have to jump through to use ACF taxonomy fields. I don’t know if that helps you at all.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Relationship Field (Post Object) filtering’ is closed to new replies.