MB Relationships display only post name
Support › MB Relationships › MB Relationships display only post nameResolved
- This topic has 11 replies, 2 voices, and was last updated 5 years, 10 months ago by
eazy.
-
AuthorPosts
-
May 28, 2019 at 3:52 AM #14727
eazy
ParticipantHello, 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 relationsI need some help to understand which part of code i need to change.
May 28, 2019 at 11:46 PM #14739eazy
ParticipantAny help is appreciated
May 29, 2019 at 11:05 AM #14741Anh Tran
KeymasterHi,
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.
May 29, 2019 at 2:33 PM #14757eazy
ParticipantThanks 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.
May 29, 2019 at 2:44 PM #14759Anh Tran
KeymasterYou can set
query_args
parameter forfrom
andto
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', ] );
May 29, 2019 at 3:46 PM #14765eazy
ParticipantHi,
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.
May 30, 2019 at 1:46 AM #14776eazy
ParticipantHi,
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 resettedMay 30, 2019 at 2:43 PM #14778Anh Tran
KeymasterHmm, 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
forquery_args
in the relationship meta box. So the query only get the selected items.
May 31, 2019 at 1:24 AM #14786eazy
ParticipantHello, 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?
June 3, 2019 at 2:52 AM #14807eazy
ParticipantHi, any help on how i get this done?
June 4, 2019 at 10:50 AM #14826Anh Tran
KeymasterHi @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 ofto
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.
June 10, 2019 at 2:49 AM #14895eazy
ParticipantThanks 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.
-
AuthorPosts
- You must be logged in to reply to this topic.