Support

Account

Home Forums General Issues Relationship query filter not working

Solving

Relationship query filter not working

  • Hi there,

    I’m trying to filter de results of a relationship field by modifying the query with he filter ‘acf/fields/relationship/query’. Even when I add the example filter to all relationship fields, nothing is happening.

    add_filter('acf/fields/relationship/query', 'my_acf_fields_relationship_query', 10, 3);
    function my_acf_fields_relationship_query( $args, $field, $post_id ) {
    
        $args['posts_per_page'] = 1;
    
        return $args;
    }

    The relationship field I’m using is a sub field inside a “Group” field, but if I test it with a new relationship field outside the group, it isn’t working either.

    Is this filter bugged in the current ACF PRO?

    Thanks for your help!

  • We have the same problem. The filter does not seem to have any effect. Can you help us out here?

  • Going to need more information. As far as I can tell the hook is working as expected and I am able to alter the query.

  • Hi John, thanks for the quick reply!
    So, we have a relationship field with the name news and the following query filter:

    add_filter('acf/fields/relationship/query/name=news', 'my_acf_fields_relationship_query', 10, 3);
    function my_acf_fields_relationship_query( $args, $field, $post_id ) {
        $args['posts_per_page'] = 3;
        return $args;
    }

    In the backend mask, this actually works and limits the result in the selector to 3 results.
    We would expect the filter to behave the same way in the frontend, limiting the result of a call to get_field(‘news’,$post_id) in a template to 3 results.
    But here the filter doesn’t seem to get applied.

  • No, this filter is only for the admin and has no effect on front end functionality. There is not filter on the front end that will limit what is returned.

    The field settings allow setting the number of posts that can be selected so that more than this number cannot be added.

    If you want to not limit the number that can be selected but only show 3 then you must do this with your own code where the values are shown in PHP.

  • Hi John,

    ok, thanks for the information!

    Although it is bad news, as in our use case, this means in some cases loading quite a lot of data. Filtering it afterwards with PHP just to get 3 rows is of course possible, but not very elegant and also a performance issue.
    Especially if you consider that we would have multiple filters, like post_status, date, etc., loading all data vs loading only the required data really makes a difference to the page speed.
    Therefore, a way to filter the frontend query would be a highly appreciated future feature 😉

  • My question would be why are you letting users choose an unlimited number of post when you will only show 3? Is there some case where you’re going to show more than 3?

    You can get the unformatted value of the field

    
    $posts = get_field('relationship_field_name', false, false);
    

    This will return just an array of post IDs and not have the overhead of getting many posts. Then you can use this array in your own WP_Query() to get the number of posts that you want using the “post__in” argument to only get posts that are included in the field and limit the query (posts_per_page) to 3. This is what ACF does when it formats the value except for the limit.

  • Well, there are pages on which we will display all the news for a project. But on other pages, we want to display only the latest three.

    But I agree, since the filter in question isn’t working the way i thought, what you proposed is probably the best way to go, yes. Thanks!

  • That is the way I would go if you have a need to show them all in other places.

    To be honest, I have rarely had ACF return post objects from relationship or post object fields because there is too much overhead. For example, if all I am doing is getting custom fields from the posts and I will not be showing any of the WP content for the posts then there is really not point in doing a query to get the posts at all. And since I never I don’t build sites that use the default content editing I almost never need the posts.

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

You must be logged in to reply to this topic.