Using WordPress ‘get_site_meta()’ PHP function

The get_site_meta() WordPress PHP function retrieves metadata for a specific site.

Usage

get_site_meta($site_id, $key = '', $single = false);

Parameters

  • $site_id (int) – The Site ID you want to retrieve metadata for.
  • $key (string) – Optional. The specific meta key to retrieve. By default, it returns data for all keys. Default: ”.
  • $single (bool) – Optional. Whether to return a single value. This parameter has no effect if $key is not specified. Default: false.

More information

See WordPress Developer Resources: get_site_meta()

This function is only available on multisite blogs.

Examples

Retrieve all metadata for a site

Retrieve all metadata associated with the site with ID 2.

$site_meta = get_site_meta(2);

Retrieve a single meta value for a site

Retrieve the value of the meta key ‘custom_key’ for the site with ID 2.

$meta_value = get_site_meta(2, 'custom_key', true);

Retrieve multiple meta values for a site

Retrieve all values of the meta key ‘custom_key’ for the site with ID 2.

$meta_values = get_site_meta(2, 'custom_key');

Check if a meta key exists for a site

Determine if the meta key ‘custom_key’ exists for the site with ID 2.

$meta_exists = (get_site_meta(2, 'custom_key', true) !== '') ? true : false;

Retrieve all meta keys and values for a site and display them

Retrieve and display all meta keys and their corresponding values for the site with ID 2.

$site_meta = get_site_meta(2);

foreach ($site_meta as $key => $value) {
    echo 'Key: ' . $key . ', Value: ' . implode(', ', $value);
}