Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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....?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
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 Dec 17 '12 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 Dec 18 '12 at 4:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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