当我选择一个或多个选项(分类法)时,如何在wordpress中使用ajax?

时间:2017-09-06 10:18:49

标签: php jquery ajax wordpress

当用户选择一个或多个分类形式html时,如何使用ajax并获取帖子?

目前,当我选择任何一个选项时,无法获得任何帖子。

但我需要选择一个或多个选择选项才能获得它。

这是我的存档页面,只需在选择选项中输出每个分类:

 <form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
    <?php


    $args = array(
      'public'   => true,
      '_builtin' => false
    );
    $output = 'objects'; // or names
    $taxonomies = get_taxonomies( $args, $output );

    foreach ($taxonomies as $taxonomy) {

       echo "<select name='".$taxonomy->name."' class='postform'>";
       echo "<option value='' selected>" . $taxonomy->labels->name . "</option>";

       $taxonomies_item = get_terms(array(
          'taxonomy' => $taxonomy->name,
          //'hide_empty' => false,
       ));

       foreach($taxonomies_item as $taxonomy_item) {
          echo "<option value='".$taxonomy_item->term_id."'>".$taxonomy_item->name."</option>";
       }
       echo "</select>";
    }
    ?>
    <input type="hidden" name="action" value="filter">
    <div class="product_list" id="response"></div>

这是目前的功能,它是关于输出形式ajax。

我有三种分类法(类型,播放器,时间),它已经注册了产品分类(帖子类型)。

当我选择两个或三个选项时,它会起作用。

我认为问题在于wordpress tax_query 选项&#39;关系&#39; =&GT; &#39;和&#39; /&#39;或&#39;

$group = array('type', 'player', 'time');

   $choices = $_POST['type'];
    // Grouping data by group
    foreach($group as $Key){
            switch ($Key) {
                // One block for each group defined in $group array
                case $group[0]:
                    $grp[0][] = array( 'taxonomy' => $Key, 'field' => 'id', 'terms' => $_POST['type'], 'compare' => 'like',);
                    break;

                case $group[1]:
                    $grp[1][] = array( 'taxonomy' => $Key, 'field' => 'id', 'terms' => $_POST['player'], 'compare' => 'like',);
                    break;

                case $group[2]:
                    $grp[2][] = array( 'taxonomy' => $Key, 'field' => 'id', 'terms' => $_POST['time'], 'compare' => 'like',);
                    break;
            }
    }

    $grp_arr = array();

    //Adding ('relation' => 'OR') to each group with a length > 1
    foreach ($grp as $key_grp => $grp_values) {
        if(count($grp_values) == 1){
            $grp_arr[$key_grp] = array('relation' => 'OR');
        }
        foreach ($grp_values as $grp_val) {
            $grp_arr[$key_grp][] = $grp_val;
        }
    }

    // Compiling it all
    $meta_query = array('relation' => 'AND');
    foreach ($grp_arr as $grp_arr_val) {
      $meta_query[] = array_merge($grp_arr_val);
    }

    // The query (compiled)
    $query = new WP_Query( array(
        'post_type'     => 'product',
        'tax_query'    => $meta_query
    ));

    // The Loop
    if( $query->have_posts() ) :
        while( $query->have_posts() ): $query->the_post();
            echo '<h3>' . $query->post->post_title . ' </h3>';
        endwhile;
         var_export($meta_query);
        wp_reset_query();
    else:
        _e('Sorry, no posts matched your criteria.');
        wp_send_json($query->posts);
    endif;

    die();

0 个答案:

没有答案