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.

I need to call the WordPress options table and pull some data. However, the table name may be different (typically wp_options) depending on whether the default table prefix was used on installation.

How can I obtain a reference to the site's table prefix in order to properly access the site's options table via $wpdb?

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';
share|improve this question

2 Answers 2

up vote 4 down vote accepted
global $wpdb;
$prefix = $wpdb->prefix;
share|improve this answer
    
+1, I believe this is correct. I've since found that I need to call the options table with $wpdb->options rather than wp_options, which resolves my issue without needing the prefix. –  N2Mystic Feb 13 '12 at 21:11

While you can get the prefix with $wpdb->prefix, as suggested by Pippin, you're much better off getting the table you need directly with $wpdb->options (in your case). $wpdb includes, by default, the table names for every table, accessible by $wpdb->{table_name}. For example: the posts table is $wpdb->posts...pretty straightforward stuff really.

share|improve this answer

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.