在AJAX循环中使用分类复选框筛选自定义帖子总是会给服务器500带来错误

时间:2019-09-25 作者:Captain Dando

我已经为此工作了几个小时,找不到解决方案。

我有一个网站,它使用一个名为“Brands”的自定义帖子类型,该类型有一个名为“brand categories”的分类法。应我的客户要求,我创建了一个页面,您可以在其中筛选品牌,允许您搜索一个品牌和/或按品牌类别查找所有品牌。它是通过ajax完成的,因此无需重新加载页面。

不幸的是,无论我做什么,我总是会在以下方面出错

GET https://darkm.co/womenschapter/wp-admin/admin-ajax.php?action=my_ajax_filter_search&Brandcats%5B%5D=beauty 500 (Internal Server Error)

当我尝试按分类法搜索时。我已经没有主意了。我错过什么了吗?

这是我的php

add_shortcode(\'WCAlphabeticalBrands\', \'WCAlphabeticalFunc_Cleanse\');
// Shortcode: [my_ajax_filter_search]
function my_ajax_filter_search_shortcode() {
    ob_start(); 
    my_ajax_filter_search_scripts(); // Added here

    // list taxonomies
    $taxonomies = get_object_taxonomies( \'brand\', \'objects\' );




    foreach( $taxonomies as $taxonomy ){
        echo $taxonomy->name;

        $terms = get_terms(array(
            \'taxonomy\' => $taxonomy->name,
            \'hide_empty\' => false,
        ));
        echo "<ul>";
        foreach( $terms as $term ){
            echo "<li><label><input class=\'brandcatcheckboxes\' type=\'checkbox\' name=\'Brand-category-filter[]\' value=\'{$term->slug}\'/>{$term->name}</label></li>";
        }
        echo "</ul>";
    }?>
    <div id="my-ajax-filter-search">
        <form action="" method="get">
            <div class="column-wrap">
                <div class="column">
                    <label for="year">Brand Name</label>
                    <input type="text" name="title" id="BrandName">
                </div>
            </div>
            <input type="submit" id="submit" name="submit" value="Search">
        </form>
        <ul id="ajax_filter_search_results"></ul>
    </div>
    <?php
    return ob_get_clean();
}

add_shortcode (\'my_ajax_filter_search\', \'my_ajax_filter_search_shortcode\');





function my_ajax_filter_search_scripts() {
    wp_enqueue_script( \'my_ajax_filter_search\', plugin_dir_url( __FILE__ ). \'/js/ajax.js\', array(), \'1.0\', true );
    wp_localize_script( \'my_ajax_filter_search\', \'ajax_url\', admin_url(\'admin-ajax.php\') );     
}

add_action(\'wp_ajax_my_ajax_filter_search\', \'my_ajax_filter_search_callback\');
add_action(\'wp_ajax_nopriv_my_ajax_filter_search\', \'my_ajax_filter_search_callback\');

function my_ajax_filter_search_callback() {

    header("Content-Type: application/json");     
    /*
    $tax_query = array();
    if(isset($_POST[\'Brand-category-filter\'])) {
        $BCat = $_POST[\'Brand-category-filter\'];
        $tax_query[] = array(
            \'taxonomy\' => \'brand-category\',
            \'field\' => \'term_taxonomy_id\',
            \'terms\' => $BCat
        ); 
    }
    */

    $args = array(
        \'post_type\' => \'brand\',
        \'posts_per_page\' => -1,
    );

    if(isset($_GET[\'title\'])) {
        $search = sanitize_text_field( $_GET[\'title\'] );
        $BCat = sanitize_text_field($_GET[\'Brand-category-filter\']);
        $search_query = new WP_Query( array(
            \'post_type\' => \'brand\',
            \'posts_per_page\' => -1,
            \'tax_query\' => array(
                \'relation\' => \'AND\',
                array(
                    \'taxonomy\' => \'brand-categories\',
                    \'terms\' => $BCat[0],
                    \'field\' => \'slug\'
                )

            ),\'s\' => $search
        ));
    } 
    else {
        $search_query = $args;
    }

    if ( $search_query->have_posts() ) {
        $result = array();
        while ( $search_query->have_posts() ) {
            $search_query->the_post();
            $result[] = array(
                "id" => get_the_ID(),
                "title" => get_the_title(),
                "content" => get_the_content(),
                "taxonomies" => get_the_terms(get_the_ID(), \'brand-categories\'),
                "permalink" => get_permalink(),
            );
        }
        wp_reset_query();

        echo json_encode($result);

    } else {
        // no posts found
    }
    wp_die();
}
这是我的javascript:

jQuery( document ).ready(function() {
    var mafs = jQuery("#my-ajax-filter-search"); 
    var mafsForm = mafs.find("form"); 
    var Brandcats = [];

    mafsForm.submit(function(e){
        e.preventDefault(); 
        //console.log("form submitted");

        //console.log(mafsForm.find("#title").val());
        /*if(mafsForm.find("#year").val().length !== 0) {
            var BrandName = mafsForm.find("#year").val();
        }*/

        jQuery(\'.brandcatcheckboxes:checkbox:checked\').each(function() {
            Brandcats.push($(this).val());
        });
        console.log(Brandcats);
        if(mafsForm.find("#BrandName").val().length !== 0) {
            var BrandName = mafsForm.find("#BrandName").val();
        }
        var data = {
            action : "my_ajax_filter_search",
            title : BrandName,
            Brandcats : Brandcats,
        }

        $.ajax({
        url : ajax_url,
        data : data,
        success : function(response) {
            console.log(response);
            mafs.find("ul").empty();
            if(response) {
                for(var i = 0 ;  i < response.length ; i++) {
                    //var html  = "<div>Result found</div>";
                     var html  = "<div>" + response[i].title + "</div>";
                     mafs.find("ul").append(html);
                }
            } else {
                var html  = "<li class=\'no-result\'>No matching brands found. Try a different filter or search keyword</li>";
                mafs.find("ul").append(html);
            }
        } 
        });
});





});

1 个回复
SO网友:Milan Hirpara

代码中没有任何错误,只需从代码中删除一行即可。

请从代码中删除第一行:

add_shortcode(\'WCAlphabeticalBrands\', \'WCAlphabeticalFunc_Cleanse\');