Hooking into get_post_metadata and add_post_metadata
Support › MB Custom Table › Hooking into get_post_metadata and add_post_metadataResolved
- This topic has 6 replies, 3 voices, and was last updated 5 years, 9 months ago by
Anh Tran.
-
AuthorPosts
-
January 15, 2020 at 3:56 PM #17847
COMCEPT
ParticipantHello, I'm trying to hook into the get_post_metadata to read from the custom table for specific post types. Also, for said post types, I'd like to hook into the add_post_metadata so I can save into the custom table, possibly using MB functions.
What MB functions should I use to do such operations inside those hooks?
Specifically I'm trying to get Polylang to read/save metadata from/to custom tables when adding a translation. I've read the code and it uses those hooks so I thought I'd hook into those, using MB functions.
Any suggestion on how to do that?
January 16, 2020 at 3:26 AM #17857Dave
ParticipantI've got the same issue.
January 16, 2020 at 2:25 PM #17860Anh Tran
KeymasterHi Daniele & Dave,
You can use raw SQL code to read and write data to your custom table. It's simple like this:
// Read global $wpdb; $value = $wpdb->get_var( 'SELECT field_id FROM your_table WHERE ID=123' ); // Write global $wpdb; $wpdb->update( 'your_table', ['field_id' => $value], ['ID' => 123] );January 16, 2020 at 5:02 PM #17865COMCEPT
ParticipantThank you Anh,
I was hoping for some sort of higher level approach; an API responsible for retrieving/storing metadata from/to custom tables (for a specific post id) with some kind of validation, sanitization, serialization and so on. Maybe a set of functions/wrappers to take into account the way Meta Box stores the fields. Is this low-level approach the only possible one?
Thank you
January 16, 2020 at 8:15 PM #17867Dave
ParticipantSo this will work but is difficult. I’m trying to programmatically create a new taxonomy term, of it does not already exist, and save the term in a taxonomy_advanced field in a custom table. I’ve been digging into the plugin files to figure out how this is done by default. I’m thinking I can call the ‘value’ method on the advanced taxonomy class?
January 17, 2020 at 1:55 AM #17870Dave
ParticipantThis is what I got working
if (empty(rwmb_get_value('idea_source', null, $post_id ))) { $term = get_term_by('slug', strtolower($tweet_info['source']), 'idea-source'); if (!$term) { $term = wp_insert_term( $tweet_info['source'], 'idea-source', null ); } $term = isset( $term['term_id'] ) ? $term['term_id'] : null; $post_data['source_tax'] = $term; }Later on I did this
$wpdb->update('TABLE_NAME', $post_data, array( 'ID' => $post_id));January 18, 2020 at 8:59 AM #17887Anh Tran
Keymaster@COMCEPT: currently, it's the only solution. We'll add more higher API to the plugin in future releases.
-
AuthorPosts
- You must be logged in to reply to this topic.