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


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

    • B1gJ4k3

    • March 28, 2018 at 11:15 am

    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">
            foreach( $post_objects as $post):
                //do stuff
            <?php wp_reset_postdata();?>
    <?php endif;
  • Noticed this line is missing the s in sort

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

    • B1gJ4k3

    • March 29, 2018 at 1:43 am

    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);
    • B1gJ4k3

    • March 29, 2018 at 11:01 am

    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);
    • B1gJ4k3

    • March 29, 2018 at 11:03 am

    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?

    • B1gJ4k3

    • March 30, 2018 at 1:34 am

    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.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.