black friday sale

Custom Fields
Re-Envisioned
Support MB Custom Table Hooking into get_post_metadata and add_post_metadata

  • Creator
    Topic
  • #17847
    Resolved COMCEPT
    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?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #17857
    Dave
    Participant

    I’ve got the same issue.

    #17860
    Anh 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
    COMCEPT
    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
    Dave
    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
    Dave
    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 Tran
    Keymaster

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

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