Custom Fields
Re-Envisioned
Support General Add Select Values from Other Custom Fields

This topic contains 11 replies, has 2 voices, and was last updated by  Pete Wlodkowski 1 day, 11 hours ago.

  • Creator
    Topic
  • #12512
    Resolved
    Pete Wlodkowski
    Participant

    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?

Viewing 10 replies - 1 through 10 (of 11 total)
  • Author
    Replies
  • #12513

    Pete Wlodkowski
    Participant

    By the way, I used the meta box builder to create the select field.

    #12514

    Pete Wlodkowski
    Participant

    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.

    #12520

    Anh Tran
    Keymaster

    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,
                ],
            ],
        ];
    } );
    #12538

    Pete Wlodkowski
    Participant

    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?

    Builder fields

    #12539

    Pete Wlodkowski
    Participant

    Oh, okay, I found the code tab. Thank you!

    #12540

    Pete Wlodkowski
    Participant

    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?

    #12543

    Anh Tran
    Keymaster

    Yes, that’s the correct flow. It’s useful whenever you want to do advanced things with code.

    #12659

    Pete Wlodkowski
    Participant

    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.

    fields

    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.

    #12661

    Pete Wlodkowski
    Participant

    TO show you the back-end, post edit screen…. I’m trying to get the tournament points to populate the points select dropdown:

    CPT Edit Screen

    #12666

    Anh Tran
    Keymaster

    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.

Viewing 10 replies - 1 through 10 (of 11 total)

You must be logged in to reply to this topic.