Adding data to key value field via PHP
Support › Meta Box AIO › Adding data to key value field via PHPResolved
- This topic has 10 replies, 2 voices, and was last updated 2 years, 7 months ago by
Long Nguyen.
-
AuthorPosts
-
August 6, 2022 at 6:50 AM #37378
Necdet Emre Ozpalamutcu
ParticipantHi,
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.
August 7, 2022 at 2:40 PM #37388Long Nguyen
ModeratorHi,
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
August 8, 2022 at 4:27 PM #37398Necdet Emre Ozpalamutcu
ParticipantHi,
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 );August 8, 2022 at 6:25 PM #37400Long Nguyen
ModeratorHi,
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/August 10, 2022 at 3:54 AM #37428Necdet Emre Ozpalamutcu
ParticipantHi,
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 );
}
});August 10, 2022 at 12:45 PM #37436Long Nguyen
ModeratorHi,
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 functionadd
toupdate
.August 10, 2022 at 4:53 PM #37438Necdet Emre Ozpalamutcu
ParticipantHi 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.
August 11, 2022 at 12:40 PM #37447Long Nguyen
ModeratorHi,
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 ); } );
August 11, 2022 at 1:07 PM #37448Necdet Emre Ozpalamutcu
ParticipantI can't believe it I missed the [ ] brackets on the values, thank you so much now it works, thanks
August 25, 2022 at 10:04 AM #38029Necdet Emre Ozpalamutcu
ParticipantHi,
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.
August 25, 2022 at 12:50 PM #38036Long Nguyen
ModeratorHi,
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
-
AuthorPosts
- You must be logged in to reply to this topic.