Support Forum
Hello,
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.
Hi,
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.
}
Nice 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.
Hi,
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'] );
}
Hello 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.
Long, 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.
Hi,
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/
It's a suggestion to add dynamic options to your plugin. Not interested in a quote.
I see that is a general problem.. you can see it here:
https://wordpress.stackexchange.com/questions/379135/custom-metabox-data-slow-query-on-admin-init
Why 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
Dynamic fields really important, great to have feature to populate fields prior to submission in the case of MB Front-End Submission.