1

I created a plugin that is used on every site within my Multisite setup. Within this plugin I have an admin setting page that allows each site to set the options according to the sites needs. Each site saves its data with the add_option() function.

How do I query all the custom options values from all the sites on the Multisite? I need to collect the values and then store them into an array. Once in an array I will be using it to check another value with the array to see if it exist within the array of option values from all the sites.

How to find all the values of the custom options?

1
  • Try to provide a SSCCE of your code, so people don't have to write a plugin from scratch to start testing the issue.
    – brasofilo
    Commented Oct 3, 2013 at 4:43

3 Answers 3

2

You can use get_site_option() function to get single network-wide option.

Example :

$singleoption = get_site_option( 'siteurl' );

If you want to get all options from all sites use wp_load_alloptions() function.

Example :

$alloptions = wp_load_alloptions()
1

So this is what I came up with and it works, but if there is a more efficient way of getting the same results (like a built in WP function) then please let me know. Thanks.

 global $wpdb;

        $blogs = $wpdb->get_results("SELECT blog_id FROM wp_blogs where blog_id > 1");

        foreach ($blogs as $blog) {

            $option_values = $wpdb->get_results("SELECT option_value FROM wp_" . $blog->blog_id . "_options WHERE option_name='mb-activation-options' ");

            foreach ($option_values as $value) {
                var_dump($value->option_value);
            }
        }
1
  • 1
    If the options aren't updated frequently, you might be wise to try caching them so that you're not having to query all those tables every single time. Also, if you haven't seen them, see get_option() and switch_to_blog().
    – Pat J
    Commented Oct 3, 2013 at 16:58
0

Thanks for the response, but wp_load_alloptions() is only returning all options and values from a the single site that it is loaded on and not all the options and values from all the sites on the network.

What I need is something like below where I copied from wp_load_alloptions() core and added the WHERE clause. However this returns only the site option specific to where it is executed, because the $wpdb prefix calls for the site that is ran.

$alloptions_db = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE option_name = 'NAME_OF_OPTION'");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.