0

In a WP_List_Table example I'm using, I am seeing how to make a general query to the wp_options table in the Wordpress database.

For example, $query = "SELECT * FROM $wpdb->options";

I also see that we typically create a foreach loop to display all the records in this table on separate rows. Sounds easy enough.

But I would like to filter this query to reference an "options array" instead. This is a single value in my table. For example, I want to query a single option called testimonial_settings and then retrieve all the array values from this single field. I can't wrap my head around how I would do this using a foreach loop.

Maybe a simple example showing the proper syntax would help clear things up....?

1 Answer 1

3

The table wp_options can normally be queried using get_option()

So you can use something like

$testimonial_settings = get_option('testimonial_settings');

to retrieve the values of testimonial_settings. If the stored value is serialized, it will be retrieved as an array.

2
  • So WP_List_Table does not care "how" we access the options, right? That's great! None of the WP_List_Table examples out there are using get_option('blah') to access settings. I have only seen database query methods.
    – Rob Myrick
    Commented Dec 17, 2012 at 15:00
  • It just depends what you want to do. If you're having a plugin having lots of settings, it is recommended to use a single entry in the wp_options table and store your data as an array. Storing each settings in its own field can result in more queries to fetch your data. It gets worse if autoload is set to true.
    – RRikesh
    Commented Dec 18, 2012 at 4:34

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.