Support

Account

Home Forums Backend Issues (wp-admin) ACF Relationship field loading very slowly

Solving

ACF Relationship field loading very slowly

  • I’m developing a web site which has a lot of posts and pages. I’m using the ACF relationship functionality to show the option for the user to choose posts/pages related the the post/page that he is creating or updating. The problem is: as I have a lot of posts, the application is a little slow to load the posts list, and when I scroll down through the posts list, a very ugly and strange loading simbol appears in the middle of the component. So I was wondering if it would be possible to improve this loading, but I’m afraid the way is not customizing the ACF plugin files, since it can be overridden when I update the plugin later. Does anyone have any suggestion of how can I work on this? Thanks a lot!

  • Hi @rafaella_souza

    The slow issue occurs because ACF needs to load a lot of posts to list it in the relationship field. What you can do is limiting the returned posts so it won’t need a lot of time to load. You can do it by using the “acf/fields/relationship/query” hook to modify the query. It should be something like this:

    function my_relationship_query( $args, $field, $post_id ) {
    	
        // only show children of the current post being edited
        $args['posts_per_page'] = 3;
    	// return
        return $args;
        
    }
    // filter for every field
    add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);

    This page should give you more idea about it: http://www.advancedcustomfields.com/resources/acf-fields-relationship-query/.

    You can also check the core code for any hook that allows you to customize the relationship field.

    I hope this helps.

  • Hello James
    Thanks a lot for your answer. I really like this filter and use it sometimes. But what solved my problem was another thing: I had a css class with the same name of the ACF relationship class used to show the loading posts. So there was a conflict and the loading freezed the screen. I just changed my css class name and everything works fine now.
    Regards!

  • Thanks James & Rafaella for this discussion…

    I also have this problem…

    I require the user to pick posts from a selection of 12,000 posts
    And also, this relationship field is part of a “layout” field – so, feasibly a page can have 3 relationship fields, each selecting from these 12000 posts.

    As you can imagine the page load time is slow – 30s+ when I have the field group enabled and 3s when it is disabled.

    Obviously I am my own worst enemy here we’re such requirements! but I’d really love to find a way to use the relationship fields on such a big site. Or I will have to resort to the user picking post IDs manually…

    Can anyone think of a different way to do this or a filter that could improve the relationship field further?

    I tried
    $args[‘posts_per_page’] = 0;

    Which did reduce the number of posts on view in each relationship field but hardly affected the page load time.

    Many thanks for any input

  • Hi @osmith

    Could you please check it with $args[‘posts_per_page’] = 1?

    The load time should be the same as when you search posts using the searchbox. If you have a lot of posts, sometimes your server can’t handle it and need a lot of time to load the data from the database. You can open the developer tools in your browser to check the time each request takes to load the data.

    Maybe upgrading your server can help you fixes this issue. Please consult this issue with your hosting.

    I hope this makes sense. Thanks!

  • Hi James

    Thanks for your swift reply – very much appreciated.

    Unfortunately I had tried $args[‘posts_per_page’] = 1 already with no noticeable difference between that or, say 3 posts, or 10.

    But, having a hit a brick wall it caused me to investigate a few other things and I hit upon the answer…

    I realized that for these relationship fields (and it could be up to 4 fields, depending on the layout the user had chosen for the page) I had selected in the “filter” option for the field “search”, “post type” AND “taxonomy” and it crossed my mind that these 12,000 posts would have quite a few terms added – 30,000 in fact! So by turning off the taxonomy filter my problem has been all but eradicated.

    There is effectively no performance hit for having 12,000 posts it was the taxonomy filter causing the bottleneck.

    Thanks for your help and in this case just making me look harder myself. Hopefully could be useful information for someone else.

  • Hi @osmith

    Nice finding! Yeah, the taxonomy filter loads all the taxonomy you have, so it’s better to remove it if you don’t use it. Hope your finding can help anyone else out there 🙂

    Thanks!

  • Hi @James
    I have have only Three Programs to show in relationship filter but they are not displaying at all, instead a search symbol is circling for a long time. Help me please.

  • hi! I am certain that there is a better way than delivering all of the WP tags in html every time someone hits the page. WordPress itself queries the server once you begin typing in the Tags field.

    Can you please implement something like that? it’s cray cray to have (in our case) 9,119 tags loading every admin page view 🙂 our editors are complaining about the speed.

    thanks,
    CC

  • any chance the taxonomies could be dynamically loaded, as in WP core? @acf-support

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

The topic ‘ACF Relationship field loading very slowly’ is closed to new replies.