WordPress Plugin Development: How can I set a MySQL query to specify the options table using a user defined prefix?
-
I have the following line in my plugin to remove transients from the database option table. $wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient%_feed_%')" ); However, it won't work with sites which set a prefix to the database name in wp-config.php. For example, if the prefix is set to wp_something as follows, $table_prefix = 'wp_something'; The option table name becomes wp_somethingoptions. Then the above MySQL query does not work because it uses the table name wp_options. So how can I make it so that it accepts a prefixed table name set by the user?
-
Answer:
Use the global wordpress variable $table_prefix in your query: $wpdb->query( "DELETE FROM `" . $table_prefix . "options` WHERE `option_name` LIKE ('_transient%_feed_%')" );
Nicholas Pickering at Quora Visit the source
Other answers
The $wpdb object has a member called prefix, so you could use that, like this: global $wpdb; $wpdb->query( "DELETE FROM `{$wpdb->prefix}options` WHERE `option_name` LIKE ('_transient%_feed_%')" ); But, the best way would be to use the member of $wpdb that actually represents the fully qualified and proper options table: global $wpdb; $wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient%_feed_%')" );
Trevor Mills
Related Q & A:
- Can I estimate the row count of a large mysql table using the disk space?Best solution by Database Administrators
- How can I find a Yahoo user ID if I only have the name?Best solution by Yahoo! Answers
- How can I remove a casset tape from my car player? It is a 2003 Hyundai Sonota. Does anyone else this problem?Best solution by Yahoo! Answers
- How can I burn a .rmvb file onto a DVD for viewing on a regular DVD player?Best solution by Yahoo! Answers
- How can I set a wireless camera to record on my computer?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.