Hooking into get_post_metadata and add_post_metadata

Support MB Custom Table Hooking into get_post_metadata and add_post_metadataResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #17847
    COMCEPTCOMCEPT
    Participant

    Hello, 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?

    #17857
    DaveDave
    Participant

    I've got the same issue.

    #17860
    Anh TranAnh Tran
    Keymaster

    Hi 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] );
    #17865
    COMCEPTCOMCEPT
    Participant

    Thank 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

    #17867
    DaveDave
    Participant

    So 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?

    #17870
    DaveDave
    Participant

    This 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));

    #17887
    Anh TranAnh Tran
    Keymaster

    @COMCEPT: currently, it's the only solution. We'll add more higher API to the plugin in future releases.

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