税务查询中的逻辑不太可能验证true
.
看看它:
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'zip_code\',
\'field\' => \'slug\',
\'terms\' => array( $term ),
),
array(
\'taxonomy\' => \'city_served\',
\'field\' => \'slug\',
\'terms\' => array( $term ),
),
),
如果您阅读税务查询,它会显示:获取分配了相同值的帖子(
$term
) 在里面
zip_code
和
city_served
分类法。
请注意,您已设置AND
两种分类法之间的关系并使用相同的分类法$term
两者的变量。邮政编码和城市名称不太可能具有相同的值。
也许你想要另一种逻辑:
\'tax_query\' => array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'zip_code\',
\'field\' => \'slug\',
\'terms\' => array( $term ),
),
array(
\'taxonomy\' => \'city_served\',
\'field\' => \'slug\',
\'terms\' => array( $term ),
),
),
除此之外,我没有发现您的代码中有任何错误,尽管您没有显示完整的代码,所以我不能确定。
我使用默认的post类型、类别和标记分类法构建了一个简单的测试,它可以工作:
PHP:
add_action( \'wp_ajax_hbgr_search\', \'hbgr_search\' );
add_action( \'wp_ajax_nopriv_hbgr_search\', \'hbgr_search\' );
function hbgr_search() {
$term = strtolower( $_GET[\'term\'] );
$suggestions = array();
$input_args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => array( $term )
),
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => array( $term )
),
),
);
$loop = new WP_Query( $input_args);
while( $loop->have_posts() ) {
$loop->the_post();
$suggestion = array();
$suggestion[\'label\'] = get_the_title();
$suggestion[\'link\'] = get_permalink();
$suggestions[] = $suggestion;
}
wp_reset_postdata();
$response = wp_send_json( $suggestions );
}
add_action( \'wp_enqueue_scripts\', \'add_scripts\' );
function add_scripts() {
wp_enqueue_script( \'jquery\' );
wp_enqueue_script( \'jquery-ui-autocomplete\' );
wp_register_style( \'jquery-ui-styles\',\'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css\' );
wp_enqueue_style( \'jquery-ui-styles\' );
wp_register_script( \'my-autocomplete\', plugin_dir_url( __FILE__ ) . \'my-autocomplete.js\', array( \'jquery\', \'jquery-ui-autocomplete\' ), \'1.0\', false );
wp_localize_script( \'my-autocomplete\', \'MyAutocomplete\', array( \'url\' => admin_url( \'admin-ajax.php\' ) ) );
wp_enqueue_script( \'my-autocomplete\' );
}
JavaScript(my autocomplete.js):
(function( $ ) {
$(function() {
var url = MyAutocomplete.url + "?action=hbgr_search";
$( "#search-dealer" ).autocomplete({
source: url,
delay: 300,
minLength: 3
});
});
})( jQuery );
搜索表单:
<form method="get" action="<?php echo esc_url( home_url( \'/\' ) ); ?>" role="search">
<input id="search-dealer" type="search" placeholder="Search" name="s">
<button type="submit">Submit</button>
</form>