Adding data to key value field via PHP

Support Meta Box AIO Adding data to key value field via PHPResolved

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #37378

    Hi,
    I'm trying to create a tracking history for the vehicle delivery service, everytime the booking post is updated I will take the current date and time along with other values and store it in a key value field.

    Update 1 (key) : Date, Time, Location, Status ( value )

    The issue is this is stored in a custom table and I cannot get my code to create key and value fields on the booking which then I will return to be used in a custom HTML field to output a tracking table.

    How can I create key value field entries via PHP to be stored in a custom table?

    Note: I've created the fields using Metabox GUI.

    #37388
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use public APIs of MB Custom Table to add/update values of custom fields in the custom table, please read more on the documentation https://docs.metabox.io/extensions/mb-custom-table/#api

    #37398

    Hi,
    I wrote this code but doesn't add anything to the key value field or the database:

    add_action( 'init', function($post_id) {
    $data = [ 'tracking_history_data' => 'keyTest', 'valueTest' ];
    \MetaBox\CustomTable\API::add( $post_id, "aaypmz_recoveroo_tracking_history", $data );
    }, 99 );

    #37400
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The init hook does not support passing the post ID to the callback function, please read more here https://developer.wordpress.org/reference/hooks/init/

    You can try to use the hook save_post to use the updated post ID in the callback function.
    https://developer.wordpress.org/reference/hooks/save_post/

    #37428

    Hi,
    Still doesn't work, here is the updated code:

    add_action('save_post', function ($post_id) {
    $data2 = [ 'tracking_history_data' => 'testKey', 'testvalue' ];
    \MetaBox\CustomTable\API::add( $post_id, "aaypmz_recoveroo_tracking_history", $data2 );
    }
    });

    #37436
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You need to add the pair key and value in an array like this

    add_action( 'save_post', function( $post_id ) {
        $data2 = [ 
            'tracking_history_data' => [ 
                'testKey' => 'testvalue',
                'testKey2' => 'testvalue2' 
            ],
        ];
        \MetaBox\CustomTable\API::add( $post_id, "aaypmz_recoveroo_tracking_history", $data2 );
    } );

    If the field tracking_history_data already has a value, you need to change the function add to update.

    #37438

    Hi again,
    Here is another issue 🙂

    When I add Key Value using the above code, this is the entry in the database column:

    a:2:{s:8:"TestKey1";s:10:"TestValue1";s:8:"TestKey2";s:10:"TestValue2";}

    And when I add it through the GUI, this is the database entry:

    a:2:{i:0;a:2:{i:0;s:8:"TestKey1";i:1;s:10:"TestValue1";}i:1;a:2:{i:0;s:8:"TestKey2";i:1;s:10:"TestValue2";}}

    The issue is that the Key Value is added to the database, however it won't show on the GUI unless the entry is same as the second format above, how do I make it so that it shows up on the post type edit screen.

    #37447
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you are using the field type key_value to save value in the custom table, here is the correct code to add/update value

    add_action( 'save_post', function( $post_id ) {
        $data2 = [ 
            'tracking_history_data' => [ 
                ['key10', 'value10'],
                ['key11', 'value11'],
                ['key12', 'value12']
            ]
        ];
        \MetaBox\CustomTable\API::add( $post_id, "aaypmz_recoveroo_tracking_history", $data2 );
    } );
    #37448

    I can't believe it I missed the [ ] brackets on the values, thank you so much now it works, thanks

    #38029

    Hi,
    I've run into an issue again, how do I update the text field inside the group?

            $data3 = [ 
                'tracking_history_data_group' => [
                    ['awaiting_confirmation_thistory' => $testDate],
                    ['booking_confirmed_thistory' => 'value11']
                ]
            ];
            \MetaBox\CustomTable\API::update( $post_id, "aaypmz_recoveroo_tracking_history", $data3 );

    Tried this but doesn't work.

    #38036
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can follow this code to update the text field in a group https://support.metabox.io/topic/adding-data-to-key-value-field-via-php/#post-37436

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