Dionysus
Dionysus

Reputation: 49

Adding data to wp_sitemeta table

Is there a way to add a row to the wp_sitemeta table in Wordpress Multisite? I'm thinking just like for wp_usermeta, there are functions such as add_user_meta( $user_ID, $meta_key, $meta_value), add_user_meta( $user_ID, $meta_key, $meta_value) and similar, there might be a way to do the same for wp_sitemeta? I have searched, but can't seem to find information about adding to this table anywhere. Can it be done?

Upvotes: 0

Views: 1444

Answers (2)

PHPer
PHPer

Reputation: 664

It depends on what you are trying to add. You can use $wpdb

<?php 

global $wpdb;
$main_blog_prefix = $wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);

$meta_table  = $main_blog_prefix."sitemeta";

$insertedRow = $wpdb->insert( $wpdb->prepare(
    $meta_table , 
    array( 
        'site_id' => BLOG_ID_CURRENT_SITE, 
        'meta_key' => 'some_meta', 
        'meta_value' => "asd asd asda sda sda sd"
    )
  )
);


?>

Upvotes: 1

Martin_W
Martin_W

Reputation: 1797

WordPress provides the function update_metadata($meta_type, $object_id, $meta_key, $meta_value, [$prev_value]) (see the codex) that can (and probably should) be used for this. It will update the key value if it exists, and add it if it does not exist.

To update wp_sitemeta, set $meta_type = 'site'. The $object_id should be the network ID, which is almost certainly "1". (WordPress only supports a single network with ID "1" via its UI, although the WP DB has schema support for multiple networks.) The $meta_key and $meta_value parameters are for the metadata key and value, respectively.

Note too that wp_sitemeta is for network-wide options, not for blog-specific (child site) options.

If you're able to work at the shell command prompt and have access to the WP-CLI command line client, you can also use the wp network meta update <id> <key> [<value>] [--format=<format>] command. (I have not tested whether this adds or updates as needed, but I expect it does.)

Upvotes: 3

Related Questions