MB Relationships display only post name

Support MB Relationships MB Relationships display only post nameResolved

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #14727
    eazyeazy
    Participant

    Hello, i'm creating relations programatically, there is a way to display only the values stored from db instead of all possible options inside the select?

    So user can only view and sort order the relations from backend but cant create/view new one.

    Actually i'm using MB_Relationships_API::add but this api can't sort/update the relations

    I need some help to understand which part of code i need to change.

    #14739
    eazyeazy
    Participant

    Any help is appreciated

    #14741
    Anh TranAnh Tran
    Keymaster

    Hi,

    I think a way to get this done is add disabled attribute to the select dropdown. You can do that with this code:

    add_action( 'init', function() {
        $mb = rwmb_get_registry( 'meta_box' )->get( 'posts_to_pages_relationships_to' );
        $mb->meta_box['fields'][0]['attributes']['disabled'] = true;
    }, 99 );

    And maybe add some CSS to hide the "+ Add more" button and the delete clone button.

    #14757
    eazyeazy
    Participant

    Thanks a lot, this helps the UI, but options rendering in html can be disabled?

    If we have, 100k posts, and have relations post to post, every select will have 100k options and page load speed will be slow, some browsers will crash, i want to disable other options rendering, and render only the "selected" option. So user can order only the related items.

    #14759
    Anh TranAnh Tran
    Keymaster

    You can set query_args parameter for from and to to limit the query. If you know the IDs of selected items, you can do this:

    MB_Relationships_API::register( [
        'id'   => 'posts_to_pages',
        'from' => [
            'post_types' => 'post',
            'query_args' => [ 'post__in' => [1,2,3], 'posts_per_page' => 10 ],
        ],
        'to'   => 'page',
    ] );
    #14765
    eazyeazy
    Participant

    Hi,
    thanks for fast reply.

    Sadly i can't use the ID's because all 100k posts are related to each other and i can't limit the query.

    I need to set this limit to query that populates the select2, instead of showing all options, i need to show/render only the selected option, so the page will load fast because html output will be a lot less.

    #14776
    eazyeazy
    Participant

    Hi,
    the first solution will cause a big problem, when you save the post, all relations will be lost when you save again the post.

    Then i found a bug, if you try to order the connected posts, when you reload a page, it will go back to its original order, in database all seems fine, but on frontend no.

    To replicate this issue, create some related posts

    Test1
    Test2
    Test3

    -> save -> reload page -> move Test3 to first position -> save -> reload page
    you will see the order is resetted

    #14778
    Anh TranAnh Tran
    Keymaster

    Hmm, looks like the disabled elements won't be submitted when submitting the form.

    I think both issues (disable selection and limit the query) can be done like this:

    • Make an extra query to get the current selected options (do it by making a SQL query directly to the database). With this, you get the IDs of the connected posts.
    • Set the post__in for query_args in the relationship meta box. So the query only get the selected items.
    #14786
    eazyeazy
    Participant

    Hello, so i tried but it's not working, here the code i put into functions.php:

    add_action('mb_relationships_init', function () {
    
        $currentPostId = 86323; //how do i get this?
        global $wpdb;
        $filteredIds = [];
        $results = $wpdb->get_results($wpdb->prepare('SELECT * FROM wp_mb_relationships WHERE wp_mb_relationships.to = %s', $currentPostId));
        foreach($results as $result){
            $filteredIds[] = $result->from;
        }
    
        MB_Relationships_API::register(array(
            'id' => 'episodes_to_series',
            'from' => array(
                'type' => 'post',
                'post_type' => 'episode',
                'admin_column' => true,
                'meta_box' => [
                    'context' => 'advanced',
                    'title' => 'Serie'
                ]
            ),
            'to' => array(
                'type' => 'post',
                'post_type' => 'serie',
                'meta_box' => [
                    'context' => 'advanced',
                    'title' => 'Episodes'
                ],
                'query_args' => [ 'post__in' => $filteredIds], //not working
            ),
        ));
    });

    how do i get $currentPostId ? i tried with $post but without success
    why $filteredIds is not working?

    I also tried with hard-coded array instead of filteredIds, but nothing

    Also, the bug i mentioned on #14776? Some fix is coming soon?

    #14807
    eazyeazy
    Participant

    Hi, any help on how i get this done?

    #14826
    Anh TranAnh Tran
    Keymaster

    Hi @easy,

    The working code here: https://pastebin.com/qKA2uYWL

    There are 2 notes:

    • I added the code to get the current post ID
    • Query args are set for the current post type, so the param should be in the from part instead of to

    I also optimize the query a little bit.

    I'm checking the bug with reordering. Thanks for your feedback.

    Updated: I've checked the reorder bug and it seems to be the browser cache. Pressing Ctrl-F5 to reload the page without cache will fix this.

    #14895
    eazyeazy
    Participant

    Thanks for the support, actually this works for now. Optimal choise would be ajax for both selects, inside "to" and "from" posts. But for now i'm ok with this solution.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.