WordPress AJAX查询成功但没有收到数据

时间:2017-10-11 08:24:37

标签: javascript php jquery ajax wordpress

我正在WordPress上构建一个过滤器,根据他们所属的分类过滤来自自定义帖子类型的帖子。我正在使用HTML选择字段来获取分类术语。我构建了一个AJAX函数来使这个东西工作。 我的代码的问题是它似乎正在工作。 AJAX请求是成功的,但它不返回任何数据与我当前的设置(具有参数的函数使用选择字段更改的分类变量来进行AJAX查询)。我测试的是当我手动向数据对象输入变量值时,只调用函数而没有输出数据的参数。 我在这里错过了什么,为什么我的解决方案不能正常工作。

我的PHP功能:

<?php

function my_archive_filter() {

    if (!isset($_GET['afp_nonce']) || !wp_verify_nonce($_GET['afp_nonce'], 'afp_nonce')) {
        die('Permission denied');
    }

    $args = array(
        'post_type' => 'project',
        'posts_per_page' => -1,
        'orderby' => 'name',
        'order' => 'ASC'
    );

    $args['tax_query'] = array(
        'relation' => 'AND',
    );

    // Type Taxonomy
    if (isset($_GET['typefilter'])) {
        $args['tax_query'][] = array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'operator' => "IN",
            'terms' => $_GET['typefilter']
        );
    }

    // Service Taxonomy
    if (isset($_GET['servicefilter'])) {
        $args['tax_query'][] = array(
            'taxonomy' => 'service',
            'field' => 'slug',
            'operator' => "IN",
            'terms' => $_GET['servicefilter']
        );
    }

    // Location Taxonomy
    if (isset($_GET['locationfilter'])) {
        $args['tax_query'][] = array(
            'taxonomy' => 'location',
            'field' => 'slug',
            'operator' => "IN",
            'terms' => $_GET['locationfilter']
        );
    }

    // Year Taxonomy
    if (isset($_GET['yearfilter'])) {
        $args['tax_query'][] = array(
            'taxonomy' => 'project-year',
            'field' => 'slug',
            'operator' => "IN",
            'terms' => $_GET['yearfilter']
        );
    }

    ob_start();
    $filter_posts = new WP_Query($args);
    if ($filter_posts->have_posts()): while ($filter_posts->have_posts()): $filter_posts->the_post();

            echo get_the_title();

        endwhile;
    endif;
    wp_reset_postdata();
    $result = ob_get_clean();
    wp_send_json_success($result);
    wp_die();
}

add_action('wp_ajax_archive_filter', 'my_archive_filter');
add_action('wp_ajax_nopriv_archive_filter', 'my_archive_filter');
?>

我的JS:

<script>
    (function ($) {

        function getProjects(projectType, projectService, projectLocation, projectYear) {
            var data = {
                action: 'archive_filter',
                afp_nonce: SiteParameters.afp_nonce,
                typefilter: projectType,
                servicefilter: projectService,
                locationfilter: projectLocation,
                yearfilter: projectYear
            };

            console.log(data);

            $.ajax({
                type: 'GET',
                dataType: 'json',
                url: SiteParameters.admin_ajax,
                data: data,
                success: function (data, textStatus, XMLHttpRequest) {
                    // $('.project-archive').html(data);
                    console.log(data);
                },

                error: function (errorThrown, textStatus, XMLHttpRequest) {
                    console.log(errorThrown);
                }
            });
        }

        $(document).on("change", '.project-filter__container select', function (e) {

            var projectType = $('.project-filter__type').val(),
                    projectService = $('.project-filter__service').val(),
                    projectLocation = $('.project-filter__location').val(),
                    projectYear = $('.project-filter__year').val();

            new getProjects(projectType, projectService, projectLocation, projectYear);
        });

    })(jQuery);
</script>

1 个答案:

答案 0 :(得分:0)

尝试将您的PHP代码替换为:


    function my_archive_filter() {

        if (!isset($_GET['afp_nonce']) || !wp_verify_nonce($_GET['afp_nonce'], 'afp_nonce')) {
            die('Permission denied');
        }

        $args = array(
            'post_type' => 'project',
            'posts_per_page' => -1,
            'orderby' => 'name',
            'order' => 'ASC'
        );

        $args['tax_query'] = array(
            'relation' => 'AND',
        );

        // Type Taxonomy
        if (isset($_GET['typefilter'])) {
            $args['tax_query'][] = array(
                'taxonomy' => 'type',
                'field' => 'slug',
                'operator' => "IN",
                'terms' => $_GET['typefilter']
            );
        }

        // Service Taxonomy
        if (isset($_GET['servicefilter'])) {
            $args['tax_query'][] = array(
                'taxonomy' => 'service',
                'field' => 'slug',
                'operator' => "IN",
                'terms' => $_GET['servicefilter']
            );
        }

        // Location Taxonomy
        if (isset($_GET['locationfilter'])) {
            $args['tax_query'][] = array(
                'taxonomy' => 'location',
                'field' => 'slug',
                'operator' => "IN",
                'terms' => $_GET['locationfilter']
            );
        }

        // Year Taxonomy
        if (isset($_GET['yearfilter'])) {
            $args['tax_query'][] = array(
                'taxonomy' => 'project-year',
                'field' => 'slug',
                'operator' => "IN",
                'terms' => $_GET['yearfilter']
            );
        }
        $result = '';
        $filter_posts = new WP_Query($args);
        if ($filter_posts->have_posts()):
            while ($filter_posts->have_posts()): $filter_posts->the_post();

                $result .= get_the_title();

            endwhile;
        else:
            $result = 'No posts found';
        endif;


        echo $result;
        wp_die();
    }

    add_action('wp_ajax_archive_filter', 'my_archive_filter');
    add_action('wp_ajax_nopriv_archive_filter', 'my_archive_filter');