Support Forum
Hi, I have a few custom number fields, created in Advanced Custom Fields and assigned to my custom post type.
I want to populate a Meta Box select field on the same custom post type from these values.
So, I have the following number / text fields, each with a single value, ex: 40
field_1
field_2
field_3
field_4
I want to use these as the value options / select options for my meta box select field.
How might I achieve this?
By the way, I used the meta box builder to create the select field.
Here is the PHP to show my custom fields:
<?php the_field( 'w' ); ?>
<?php the_field( 'ru' ); ?>
<?php the_field( 't5s' ); ?>
<?php the_field( 't10q' ); ?>
<?php the_field( 't1516' ); ?>
<?php the_field( 't20' ); ?>
<?php the_field( 't3032' ); ?>
<?php the_field( 't40' ); ?>
This is what I want to populate my select options with.
Hi Pete,
The Meta Box Builder doesn't allow you to use dynamic values from other fields at the moment. You need to create the meta box and field by code.
Here is an example:
add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
$post_id = null;
if ( isset( $_GET['post'] ) ) {
$post_id = intval( $_GET['post'] );
} elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = intval( $_POST['post_ID'] );
}
$options = array(
'w' => get_post_meta( $post_id, 'w', true ),
'ru' => get_post_meta( $post_id, 'ru', true ),
// and so on.
);
$meta_boxes[] = [
'title' => 'Your title',
'fields' => [
[
'type' => 'select',
'id' => 's',
'name' => 'Select',
'options' => $options,
],
],
];
} );
Thank you very much for getting back to me.
Okay, so I can't use the builder at all? Can I convert what I have built to code and modify that?
Oh, okay, I found the code tab. Thank you!
So, I'm assuming I can use the builder, copy the code, edit it, paste the code into functions, and deactivate the builder? Is that a correct flow?
Yes, that's the correct flow. It's useful whenever you want to do advanced things with code.
Okay, great, so I have this set up - I rebuild the fields I want to use for my dropdown options in Meta Box instead of ACF.
So, the fields I want to use for the Points dropdown are:
W
RU
T5
T10
T15
T20
T30
T40
My code is below:
https://ghostbin.com/paste/9gg73
Is it still basically the same to populate the points dropdown options...
I tried to modify the snippet you sent to this:
$options = array(
'w' => get_r_points_table( $post_id, 'points_w', true ),
'ru' => get_r_points_table( $post_id, 'points_ru', true ),
// and so on.
);
array (
'id' => 'ppoints',
'name' => 'Points',
'type' => 'select',
'placeholder' => 'Select an Item',
'options' => $options,
'columns' => 1,
),
But it came up empty...
Thank you for your assistance!
This plugin is so great. I realize the limits to what I can build in WordPress are much broader now.
Thank you.
TO show you the back-end, post edit screen.... I'm trying to get the tournament points to populate the points select dropdown:
Oh, you use group! So the code to get the point should be different a little bit. I've fixed your code to: https://ghostbin.com/paste/9p34h. Please take a look.
Anh Tran, you are awesome my friend. I send a lot of gratitude and karma your way! You deserve it. Thank you for helping me on this. Happy Holiday.
Hi Anh,
Quick question. I've set up individual groups for multiple years (2006 - 2019), but it is very slow:
Here is my working code for the multi years, wondering if there is a better way to write it so it is quicker. Like I said, this is slow. It's taking forever to load the edit Tournament (edit post).
https://ghostbin.com/paste/ejjhy
Thought maybe the multi year functions could be grouped... just wasn't sure. Thought I'd send you a message. Thank you!
I have optimized your code to make it much shorter here: https://ghostbin.com/paste/uk3zq. As all the year has a same meta box, it can be wrapped in a function.
However, the speed is slow maybe because of the post
field. Each post
field make a query to the database to get all posts. As you have 14 post
fields for 14 years, there are 14 queries. That might be the cause.
Update: I have updated the plugin on Github https://github.com/wpmetabox/meta-box/ that has an improvement for the post
field. Now it runs only 1 query instead of 14. Please download from Github and try it.
Hi Anh Tran,
I am trying to solve a very similar problem (as above) using Metabox Group fields to populate select options. I noticed you had a code example for this in reply #12666 that links to https://ghostbin.com/paste/9p34h. Unfortunately, this example is no longer available there - do you have it somewhere else? Think it will help my problem.
Cheers 🙂