I have installed wordpress with question2answer as a single signon. The database created by wordpress with wp_ prefix and question2answer prefix is qa_
Now wordpress tables doesnt required to define prefix with $wpdb-> but how can I get question2answer table without query with prefix like I prefer something like as below
$titles = $wpdb->get_results("
SELECT title
FROM qa_posts // with qa_ prefix
");
Its giving output of the title but i want something like below code where I dont have to define qa_ prefix same like wordpress default tables
$titles = $wpdb->get_results("
SELECT title
FROM posts // without prefix
");
1 Answer
Write a function that uses $wpdb->prefix as a fallback. Something like this:
function wpse65880_table_with_prefix($table) {
// should define array in config file, rather than hard coding
$my_tables = array("table1", "table2", "table3", "table4");
if (in_array($table, $my_tables)) {
// "qa_" should also be a config file setting
return "qa_" . $table;
}
else return $wpdb->prefix . $table;
}
$table_name = wpse65880_table_with_prefix('post');
$titles = $wpdb->get_results("
SELECT title
FROM $table_name // reflects current prefix
");