save Text List field using update post meta

Support General save Text List field using update post metaResolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #11722
    clientservicesclientservices
    Participant

    Dear Support,

    i have a text_list in my admin custom post type and i am trying to set the values using update_post_meta.

    this is my field:

    array(
                        'id' => $prefix . 'address',
                        'type' => 'text_list',
                        'name' => esc_html__( 'Postal Address', 'nw' ),
                        'options' => array(
                            'address_1' => 'Address Line 1',
                            'address_2' => 'Address Line 2',
                        ),
                    ),

    $prefix = 'pph-';
    update_post_meta($applicantID, $prefix . "address", 'one');

    i tried the above and it sets the value for both fields. address_1 and address_2

    i also tried below with no avail
    update_post_meta($applicantID, $prefix . "address[]", 'one');
    update_post_meta($applicantID, $prefix . "address[address_1]", 'one');

    thank you.

    #11748
    Anh TranAnh Tran
    Keymaster

    Hello,

    text_list save data in multiple rows in the database. So, using update_post_meta might removes all existing values.

    I'd suggest using add_post_meta instead:

    add_post_meta($applicantID, $prefix . "address", 'one', false);
    add_post_meta($applicantID, $prefix . "address", 'two', false);

    Notice the last parameter, it must be set to false. For more info, please see the Codex.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.