Wordpress get_terms 和 wp_query 显示错误结果

问题描述 投票:0回答:1

我确实有一个子页面,我想在其中展示和过滤我客户公司的职位发布。

过滤基于两个分类法:

  • 工作类别(例如:物流、人力资源、营销等);使用名为“categoryfilter”的选择字段完成过滤
  • 工作国家(例如:德国、法国等);使用称为分类过滤器的选择字段完成过滤

什么有效:

  • 按国家过滤
  • 按类别过滤
  • 按两种分类法过滤

什么不起作用:

  • 仅按国家/地区过滤时,预期行为是仅显示属于特定国家/地区的分类法及其帖子;
  • 如果分类工作国家元素没有任何职位所属,则不应呈现

在我的代码的当前版本中,当我按国家/地区过滤结果时,它会以这种方式显示结果:

JOB CATEGORY 1
post
post

JOB CATEGORY 2
Sorry, no openings found

JOB CATEGORY 3
post
post
post

所以我只想删除整个职位类别 2(并且不显示有关缺乏空缺职位的信息)。我确实有这样的印象:我的代码的问题与taxquery if 语句的部分有关。但我无法诊断那里出了什么问题:/

代码:

function multiple_filter_function()
{

  $list_custom_taxonomy = get_terms(array(
    'taxonomy' => 'job-category',
    'name' => $_POST['categoryfilter'],
    'hide_empty' => true
  ));

  foreach ($list_custom_taxonomy as $custom_single_term) {

    echo '<h2 class="category-title">' . $custom_single_term->name . '</h2>';

    $args = array(
      'orderby' => 'date', // we will sort posts by date
      'order'  => $_POST['date'], // ASC or DESC
      'post_type' => 'career',
      'posts_per_page' => -1,
      'post_status' => 'publish'
    );
    /* taxquery if statements */
    if (isset($_POST['categoryfilter']) && isset($_POST['taxonomyfilter'])) {
      $args['tax_query'][] = array(
        'relation' => 'AND',

        array(
          'taxonomy' => 'job-category',
          'field' => 'name',
          'terms' => $_POST['categoryfilter']
        ),
        array(
          'taxonomy' => 'job-country',
          'field' => 'name',
          'terms' => $_POST['taxonomyfilter']
        ),
      );
    } elseif (!isset($_POST['categoryfilter']) && isset($_POST['taxonomyfilter'])) {
      $args['tax_query'][] = array(
        'relation' => 'AND',
        array(
          'taxonomy' => 'job-category',
          'field' => 'name',
          'terms' => $custom_single_term->name,
        ),
        array(
          'taxonomy' => 'job-country',
          'field' => 'name',
          'terms' => $_POST['taxonomyfilter']
        )
      );
    } elseif (isset($_POST['categoryfilter']) && !isset($_POST['taxonomyfilter'])) {
      $args['tax_query'][] = array(
        array(
          'taxonomy' => 'job-category',
          'field' => 'name',
          'terms' => $_POST['categoryfilter']
        )
      );
    }
    /* END of taxquery if statements */

    $query = new WP_Query($args);

    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
      echo $query->post->post_title;
    endwhile;
    wp_reset_postdata();
    else :
      echo '<p class="text-center">Sorry, no openings found</p>';
    endif;
  }

  die();
}
add_action('wp_ajax_myfilter_2', 'multiple_filter_function');
add_action('wp_ajax_nopriv_myfilter_2', 'multiple_filter_function');
php wordpress nested-loops custom-taxonomy
1个回答
0
投票

正如 CBroe 在评论中所建议的那样。 IMO,这是最可行的解决方案。

function multiple_filter_function()
{

  $list_custom_taxonomy = get_terms(array(
    'taxonomy' => 'job-category',
    'name' => $_POST['categoryfilter'],
    'hide_empty' => true
  ));

  foreach ($list_custom_taxonomy as $custom_single_term) {

    $args = array(
      'orderby' => 'date', // we will sort posts by date
      'order'  => $_POST['date'], // ASC or DESC
      'post_type' => 'career',
      'posts_per_page' => -1,
      'post_status' => 'publish'
    );
    /* taxquery if statements */
    if (isset($_POST['categoryfilter']) && isset($_POST['taxonomyfilter'])) {
      $args['tax_query'][] = array(
        'relation' => 'AND',

        array(
          'taxonomy' => 'job-category',
          'field' => 'name',
          'terms' => $_POST['categoryfilter']
        ),
        array(
          'taxonomy' => 'job-country',
          'field' => 'name',
          'terms' => $_POST['taxonomyfilter']
        ),
      );
    } elseif (!isset($_POST['categoryfilter']) && isset($_POST['taxonomyfilter'])) {
      $args['tax_query'][] = array(
        'relation' => 'AND',
        array(
          'taxonomy' => 'job-category',
          'field' => 'name',
          'terms' => $custom_single_term->name,
        ),
        array(
          'taxonomy' => 'job-country',
          'field' => 'name',
          'terms' => $_POST['taxonomyfilter']
        )
      );
    } elseif (isset($_POST['categoryfilter']) && !isset($_POST['taxonomyfilter'])) {
      $args['tax_query'][] = array(
        array(
          'taxonomy' => 'job-category',
          'field' => 'name',
          'terms' => $_POST['categoryfilter']
        )
      );
    }
    /* END of taxquery if statements */

    $query = new WP_Query($args);

    if ($query->have_posts()) : 
      echo '<h2 class="category-title">' . $custom_single_term->name . '</h2>';
      while ($query->have_posts()) : 
         $query->the_post();
         echo $query->post->post_title;
      endwhile;
      wp_reset_postdata();
    endif;
  }

  die();
}
add_action('wp_ajax_myfilter_2', 'multiple_filter_function');
add_action('wp_ajax_nopriv_myfilter_2', 'multiple_filter_function');
© www.soinside.com 2019 - 2024. All rights reserved.