Support

Account

Home Forums Front-end Issues acf/fields/post_object/query filter not working on the frontend?

Solving

acf/fields/post_object/query filter not working on the frontend?

  • I’m trying to filter the results returned from get_field(‘my_field’) on the post object field type in order to sort them by menu order. I found acf/fields/post_object/query, but that doesn’t seem to work on the frontend, only when I’m displaying the fields for selection in the WordPress admin. Is there another filter I should be using?

    Filter I implemented:

    function sort_people( $args ) {
        $args['orderby'] = 'menu_order';
        $args['order'] = 'DESC';
        return $args;
    }
    
    add_filter('acf/fields/post_object/query', 'sort_people');

    Frontend display:

    $post_objects = get_field('people_involved');
    
    if( $post_objects ): ?>
        <section class="profiles">
            <?php
            foreach( $post_objects as $post):
                setup_postdata($post);
                //do stuff
            endforeach;
            ?>
    
            <?php wp_reset_postdata();?>
        </section>
    <?php endif;
  • Noticed this line is missing the s in sort

    add_filter('acf/fields/post_object/query', 'ort_people');

  • Oh. Oops. That was just a remnant of the cutting and pasting I did. It’s there in the code that I’m working with.

    Damn. As stupid as that would have been, I was really hoping that was it…

  • I tried to do a similar set up and was unable to get the query to work as well.

  • Have you tried specifying a field by name or key? Also, the two numbers that are at the end are missing from your code line. Not sure if they’re optional:

    add_filter('acf/fields/post_object/query', 'sort_people');
    
    // filter for a specific field based on it's name
    //add_filter('acf/fields/post_object/query/name=my_post_object', 'my_post_object_query', 10, 3);
    
    // filter for a specific field based on it's key
    //add_filter('acf/fields/post_object/query/key=field_508a263b40457', 'my_post_object_query', 10, 3);
  • Yep, tried it with the field’s key (the actual key, not just the name) and adding the priority parameters to the end. Still nothing…

    For reference, here’s my current code, where key is the key of the post object field I’m using:

    function sort_people( $args ) {
        $args['orderby'] = 'menu_order';
        $args['order'] = 'ASC';
        return $args;
    }
    
    add_filter('acf/fields/post_object/query/key=field_5a9f6ea45bfa1', 'sort_people', 10, 3);
    
  • I also tried using the name:

    add_filter('acf/fields/post_object/query/name=people_involved', 'sort_people', 10, 3);

  • I have a work around that seems to work for me. I use a select type field and load values into it using the acf/load field filter. It uses a key/value set up. I can give you an example tomorrow as it is past midnight here in Maine.

  • On second thought, this probably won’t work for you,since I’m trying to limit my posts from thousands to lest than 50 and there’s no auto complete to narrow the selections.

  • Does the the post object populate without the filter on the front end?

  • Yeah, everything pulls in fine on the front end. They’re just sorted by date and nothing I do with this filter seems to have an effect.

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

You must be logged in to reply to this topic.