Custom Fields
Re-Envisioned
Support MB Relationships Relationship field performance

  • Creator
    Topic
  • #15874
    Resolved Content Pilot
    Participant

    Hi Anh,

    We have been using the MB Relationships extension on the platform we have built and everything was going well, but on our last site with more data than I have handled so far we have encountered performance problems in the loading of the relationship fields.
    What I have seen and please correct me if I am wrong, is that the selects on the relationship fields are builded with Post fields and in each of them query all posts of that post type and this is killing the database.

    My thought is to set a pagination here and then get a lazy loading going so that when a person opens a select field and scrolls to the bottom of the list or types in a name into the search field, a query is then made to populate the next 50 or 100 posts or the proper search result. This will probably happen via Ajax.

    Some of our posts have 700 relationships and more than 4,000 possible options within individual relationship fields.

    Do you think my analysis is correct? And do you think that implement paging and lazy loading to solve it is a good approach?

    This is very important for the site I am building, and time sensitive because the admin remains unusable until it is fixed. Thank you for your time.

Viewing 10 replies - 1 through 10 (of 18 total)
  • Author
    Replies
  • #15875
    Anh Tran
    Keymaster

    Hi,

    You’re right about the query for the post field. Currently, it queries all posts, which causes the performance issue. I’ll work on making it works with ajax to reduce the load on websites that have many posts.

    FYI: the number of relationships doesn’t affect the performance, since the plugin queries only once.

    #15887
    Content Pilot
    Participant

    Thank you for your reply Anh and for working on this.
    You’re right about the number of relationships, now I’ve seen it.
    I will stay expectant

    #15897
    Content Pilot
    Participant

    Anh, do you have any ETA when you can have this? We really appreciate your help.

    #15907
    Anh Tran
    Keymaster

    Hi, I’ll start working on this from tomorrow. I’ve been busy updating the extensions. Hopefully can get it done next week.

    #15968
    Content Pilot
    Participant

    Hi Anh,

    We found that recently Advanced Custom Fields is using Ajax to populate the posts on the relationship field as the field comes into the browser.

    https://www.advancedcustomfields.com/blog/acf-5-8-4-release/

    I share this in case it helps. Thanks

    #15991
    Anh Tran
    Keymaster

    I read the news, too. Thanks for sharing.

    #16043
    Content Pilot
    Participant

    Hi Anh, just to know if you think you’ll get it soon or if it can get complicated. I appreciate your time on this. Thanks

    #16050
    Anh Tran
    Keymaster

    Hi,

    I almost finish it. You can track the progress here: https://github.com/wpmetabox/meta-box/commits/ajax-post.

    What I’ve done so far:

    • Make it possible to use ajax for post field (which is used in relationships plugin) by adding 'ajax' => true to the field.
    • Make pagination works in case you don’t want to request all posts at once (via posts_per_page param in the query_args)
    • Cache the ajax requests

    I’m working on clone. If everything goes well, I can give you an example by the end of today.

    #16055
    Content Pilot
    Participant

    That would be great Anh, thanks

    #16058
    Anh Tran
    Keymaster

    Hi again,

    I’ve done the relationships for post. Please follow this guide to use it:

    • Download the latest dev version of MB Relationships on Github.
    • Download the latest dev version of Meta Box for ajax post on Github

    Now try it again and you’ll see the ajax requests are sent when open the dropdown.

    If you want to customize the ajax request, in your code to register relationships, use this code:

    add_action( 'mb_relationships_init', function () {
        MB_Relationships_API::register( array(
            'id'   => 'users_to_posts',
            'from' => array(
                'object_type' => 'post',
                'meta_box'    => array(
                    'title' => 'Manages',
                ),
                'field' => array(
                    'ajax' => true, // Note this
                    'query_args' => array(
                        'posts_per_page' => 10, // This is for pagination in Ajax
                    ),
                    'js_options' => array(
                        'minimumInputLength' => 1, // Send ajax requests when users enter at least 1 character
                    ),
                ),
            ),
            'to'   => array(
                'object_type' => 'post',
                'post_type'   => 'page',
                'meta_box'    => array(
                    'title'         => 'Managed By',
                    'context'       => 'side',
                    'empty_message' => 'No users',
                ),
                'field' => array(
                    'ajax' => true, // Note this
                    'query_args' => array(
                        'posts_per_page' => 10, // This is for pagination in Ajax
                    ),
                ),
            ),
        ) );
    } );

    Some notes:

    • Now in the relationship, you’ll have an extra field settings, which you can pass full settings array for the field. It’s similar to post, taxonomy or user field.
    • Enable ajax for the dropdown by adding 'ajax' => true to the field
    • If you just want to query only some posts via ajax (not all of posts at once), you can set posts_per_page parameter.

    Currently, it works for posts only. I’ll make it works for terms and users by tomorrow.

Viewing 10 replies - 1 through 10 (of 18 total)
  • You must be logged in to reply to this topic.