Support Forum
Support › Meta Box AIO › Adding data to key value field via PHPResolved
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.
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
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 );
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/
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 );
}
});
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
.
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.
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 );
} );
I can't believe it I missed the [ ] brackets on the values, thank you so much now it works, thanks
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.
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