Add Select Values from Other Custom Fields
- This topic has 14 replies, 3 voices, and was last updated 2 years, 2 months ago by
Codog.
-
AuthorPosts
-
December 6, 2018 at 8:33 AM #12512
Pete Wlodkowski
ParticipantHi, 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_4I want to use these as the value options / select options for my meta box select field.
How might I achieve this?
December 6, 2018 at 8:33 AM #12513Pete Wlodkowski
ParticipantBy the way, I used the meta box builder to create the select field.
December 6, 2018 at 9:43 AM #12514Pete Wlodkowski
ParticipantHere 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.
December 6, 2018 at 2:44 PM #12520Anh Tran
KeymasterHi Pete,
The MB 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, ], ], ]; } );
December 7, 2018 at 5:51 AM #12538Pete Wlodkowski
ParticipantThank 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?
December 7, 2018 at 6:00 AM #12539Pete Wlodkowski
ParticipantOh, okay, I found the code tab. Thank you!
December 7, 2018 at 6:21 AM #12540Pete Wlodkowski
ParticipantSo, 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?
December 7, 2018 at 10:50 AM #12543Anh Tran
KeymasterYes, that's the correct flow. It's useful whenever you want to do advanced things with code.
December 14, 2018 at 12:57 AM #12659Pete Wlodkowski
ParticipantOkay, 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
T40My 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.
December 14, 2018 at 3:08 AM #12661Pete Wlodkowski
ParticipantTO show you the back-end, post edit screen.... I'm trying to get the tournament points to populate the points select dropdown:
December 14, 2018 at 10:48 AM #12666Anh Tran
KeymasterOh, 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.
December 15, 2018 at 2:08 AM #12689Pete Wlodkowski
ParticipantAnh 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.
December 18, 2018 at 1:54 AM #12711Pete Wlodkowski
ParticipantHi 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!
December 19, 2018 at 4:43 PM #12737Anh Tran
KeymasterI 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. Eachpost
field make a query to the database to get all posts. As you have 14post
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.January 25, 2023 at 10:18 PM #40306Codog
ParticipantHi 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 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.