我已经安装了wordpress和question2answer作为单一登录。wordpress以wp\\uuux和question2answer为前缀创建的数据库是qa_
现在,wordpress表不需要用$wpdb->定义前缀,但如果没有前缀为的查询,我如何才能得到question2answer表,我更喜欢下面的内容
$titles = $wpdb->get_results("
SELECT title
FROM qa_posts // with qa_ prefix
");
它给出了标题的输出,但我想要下面这样的代码,我不必像wordpress默认表一样定义qa\\uuPrefix
$titles = $wpdb->get_results("
SELECT title
FROM posts // without prefix
");
最合适的回答,由SO网友:marfarma 整理而成
编写一个使用$wpdb->前缀作为回退的函数。类似这样:
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
");