我正在使用内置的wordpress建议插件调用terms表中的条目。它使用以下sql查询吐出所有术语:
$query = \'SELECT term_id,name FROM \' . $wpdb->terms . \'
WHERE name LIKE \\\'\' . $search . \'%\\\'
ORDER BY name ASC\';
我希望将它吐出的术语限制为一种分类法,例如“categories”或“post_标签”。我尝试了下面的sql代码,但没有结果。
function test() {
global $wpdb;
$search = esc_sql( $wpdb -> esc_like($_REQUEST[\'q\']));
$query = \'Select term_taxonomy_id, term_id, taxonomy, name FROM \' . $wpdb->term_taxonomy . \'
JOIN \' . $wpdb->terms . \' ON term_taxonomy.term_id = terms.term_id
WHERE taxonomy = \\\'catagory\\\'
AND name LIKE \\\'\' . $search . \'%\\\'
ORDER BY name ASC\';
foreach ($wpdb->get_results($query) as $row) {
$name = $row->name;
$id = $row-> term_id;
echo $name, "\\n";
}
die();
}
add_action(\'wp_ajax_test\', \'test\');
add_action(\'wp_ajax_nopriv_test\', \'test\');
最合适的回答,由SO网友:Sumit 整理而成
加入后term_id
存在两次。您应该指定term_id
您可以通过在全表名(或别名)前加前缀进行查询。
还有一个错误term_taxonomy.term_id = terms.term_id
缺少表前缀。
因此,更新后的查询如下所示
$query = \'SELECT term_taxonomy_id, \' . $wpdb->prefix . \'terms.term_id, taxonomy, name FROM \' . $wpdb->term_taxonomy . \'
JOIN \' . $wpdb->terms . \' ON \' . $wpdb->prefix . \'term_taxonomy.term_id = \' . $wpdb->prefix . \'terms.term_id
WHERE taxonomy = \\\'category\\\'
AND name LIKE \\\'\' . $search . \'%\\\'
ORDER BY name ASC\';