How to apply media query in table?

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

Was this solution helpful to you?

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

Find solution

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.