如何将自动完成添加到CPT的自定义分类

时间:2015-12-17 作者:thatryan

我在学习这个教程,

http://gabrieleromanato.name/adding-jquery-ui-autocomplete-to-the-wordpress-search-form/

让它像那样工作很好。然而,我正在尝试根据我为CPT创建的自定义分类法中的列表自动完成这项工作。我的搜索功能如下所示,

function hbgr_search() {
        $term = strtolower( $_GET[\'term\'] );
        $suggestions = array();

        $input_args = array(
            \'post_type\' => \'dealer-locator\',
            \'tax_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'taxonomy\' => \'zip_code\',
                    \'field\'    => \'slug\',
                    \'terms\'    => array( $term ),
                ),
                array(
                    \'taxonomy\' => \'city_served\',
                    \'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_query();

            $response = wp_json_encode( $suggestions );
            echo $response;
            exit();

}
JS代码:

(function( $ ) {
    $(function() {
        var url = MyAutocomplete.url + "?action=hbgr_search";
        $( "#search-dealer" ).autocomplete({
            source: url,
            delay: 300,
            minLength: 3
        });
    });

})( jQuery );
如果我注释掉我的PHP行$loop=。。。。并替换为此

$loop = new WP_Query( \'s=\' . $term );
然后ajax自动完成工作正常(如果我搜索帖子或页面),所以我知道它加载正确,只是我的税务查询有点奇怪?

我使用了WP\\u查询部分,并在其自己的文件中对其进行了测试,以确保其工作正常,而且确实如此。

编辑:如果我将我的函数更改为此,它也可以工作,方法是返回一个列表,其中包含我在搜索框中键入的任何内容。。。

$suggestions = array();
$suggestion[\'label\'] = $_GET[\'term\'];
$suggestion[\'link\'] = \'http://google.com\';
$suggestions[] = $suggestion;

$response = wp_json_encode( $suggestions );
echo $response;
exit();
谢谢你。

1 个回复
SO网友:cybmeta

税务查询中的逻辑不太可能验证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_codecity_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>

相关推荐

Tags as autocomplete values

如何将常规post标记作为自动完成字段的值加载?我现在得到的是这样的预设值:var data = {items: [ {value: \"1\", name: \"Siemens\"}, {value: \"2\", name: \"Phillips\"}, {value: \"3\", name: \"Whirlpool\"}, {value: \"4\", name: \"LG\"} }; $(\"#input_1_3\").autoSuggest(da