How to add/change a value of $wpdb property/var?

There is a problem: I want to add a table ‘linkmeta’ to work like ‘postmeta’ or ‘commentmeta’.. and use functions like add_metadata / update_metadata / delete_metadata!

but, in this functions call “_get_meta_table()” to verify the name in db, this code:

Read More
function _get_meta_table($type) {
    global $wpdb;
    $table_name = $type . 'meta';
    if ( empty($wpdb->$table_name) )
        return false;
    return $wpdb->$table_name;
}

so, if doesn’t exist the property/var named ‘linkmeta’ in $wpdb, _get_meta_table will return false, then update_metadata too will not work, because this piece of code at begin of that:

if ( ! $table = _get_meta_table($meta_type) )
    return false;

but, if I can set a property/var of (class) $wpdb like $wpdb->linkmeta return the correct name of table, in this case (default) wp_linkmeta, all the rest will work NICE!

so, there is a way to change wpdb in runtime?
if I just put the commands:

$wpdb->tables = array_merge($wpdb->tables, array('linkmeta'));
$wpdb->linkmeta = $wpdb->prefix . 'linkmeta';

at plugin initialize, will it work when function _get_meta_table is called??!

rgds

Related posts

Leave a Reply

1 comment

  1. Yep, just declare global $wpdb early enough and modify its fields. Codex docs for add_metadata() say exactly that:

    Note that you will also have to register the table with the $wpdb object in order for it to work. Simply add the following to WordPress’ init hook (or at least before you attempt to use any custom metadata functions).

    global $wpdb;
    $wpdb->termmeta = $wpdb->prefix.'termmeta';