Dynamic options from posts
- This topic has 10 replies, 3 voices, and was last updated 2 years, 8 months ago by
Zack.
-
AuthorPosts
-
February 17, 2022 at 3:27 PM #33959
closemarketing
ParticipantHello,
We are using a query inside the filter rwmb_meta_boxes to dynamic populate $posts_options in a select advanced, but the problem is that is loading in every page, admin and public.$posts_options = array(); $args_query = array( 'post_type' => 'page', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', ); // The Query. $the_query = new WP_Query( $args_query ); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); $posts_options[ get_the_ID() ] = get_the_title(); } wp_reset_postdata(); }
How could we use this feature and not making queries every where?
I don't see any documentation about callbacks or something similiar to populate values in a field.Thanks in advance.
February 18, 2022 at 9:44 AM #33974Long Nguyen
ModeratorHi,
I think you can create a condition to check the area that you want to show the options, like admin area
is_admin()
or something else.$posts_options = array(); if( is_admin() ) { $args_query = array( //... ); // The Query. }
February 18, 2022 at 4:32 PM #33980closemarketing
ParticipantNice suggestion Long. That filters the query in only admin.
Could we be more specific? in Edit page?I tried with pagenow global variable but in AJAX is not saving correctly.
February 19, 2022 at 9:51 AM #33997Long Nguyen
ModeratorHi,
You can try to use the WP function
get_current_screen()
to check the current screen https://developer.wordpress.org/reference/functions/get_current_screen/or use this code to get the current post ID and check some cases based on the post ID
$post_id = null; if ( isset( $_GET['post'] ) ) { $post_id = intval( $_GET['post'] ); } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = intval( $_POST['post_ID'] ); }
February 19, 2022 at 6:36 PM #34011closemarketing
ParticipantHello Long,
I'm afraid None of your solutions works.
get_current_screen function gives you undefined when it loads the filter:
Fatal error: Uncaught Error: Call to undefined function get_current_screen()
And $_GET is not working in AJAX.
I think that the best would be to have a callback solution to load the values.
March 31, 2022 at 2:40 PM #35387closemarketing
ParticipantLong, are you working in a callback so we can load dinamically the options? We need. We collapse the admin in WordPress having some blocks.
Please work in a solution.
April 1, 2022 at 11:02 AM #35394Long Nguyen
ModeratorHi,
Sorry, not yet. This case is beyond the scope support of Meta Box, it needs to create more custom code to do. You can create a service request via this contact form. I will forward it to the development team to check it and get back to you with a quote.
https://metabox.io/contact/April 4, 2022 at 9:24 PM #35456closemarketing
ParticipantIt's a suggestion to add dynamic options to your plugin. Not interested in a quote.
June 28, 2022 at 6:32 PM #36699closemarketing
ParticipantI see that is a general problem.. you can see it here:
https://wordpress.stackexchange.com/questions/379135/custom-metabox-data-slow-query-on-admin-initJune 30, 2022 at 4:54 PM #36742closemarketing
ParticipantWhy metabox is loading so many times?
[29-Jun-2022 12:16:12 UTC] load meta [29-Jun-2022 12:16:13 UTC] load meta [29-Jun-2022 12:16:15 UTC] load meta [29-Jun-2022 12:16:16 UTC] load meta [29-Jun-2022 12:16:18 UTC] load meta [29-Jun-2022 12:16:18 UTC] load meta [29-Jun-2022 12:16:21 UTC] load meta [29-Jun-2022 12:16:24 UTC] load meta [29-Jun-2022 12:16:24 UTC] load meta [29-Jun-2022 12:16:27 UTC] load meta [29-Jun-2022 12:16:29 UTC] load native [29-Jun-2022 12:16:30 UTC] load meta [29-Jun-2022 12:16:34 UTC] load meta [29-Jun-2022 12:16:45 UTC] load meta [29-Jun-2022 12:16:45 UTC] load meta [29-Jun-2022 12:16:48 UTC] load meta [29-Jun-2022 12:16:48 UTC] load meta [29-Jun-2022 12:16:51 UTC] load meta [29-Jun-2022 12:16:51 UTC] load meta [29-Jun-2022 12:16:54 UTC] load meta [29-Jun-2022 12:16:54 UTC] load meta [29-Jun-2022 12:16:57 UTC] load meta [29-Jun-2022 12:16:57 UTC] load meta [29-Jun-2022 12:17:00 UTC] load meta [29-Jun-2022 12:17:01 UTC] load meta [29-Jun-2022 12:17:03 UTC] load meta
August 18, 2022 at 8:13 PM #37568Zack
ParticipantDynamic fields really important, great to have feature to populate fields prior to submission in the case of MB Front-End Submission.
-
AuthorPosts
- You must be logged in to reply to this topic.