Search code examples
phpwordpressnested-loopscustom-taxonomy

Wordpress get_terms & wp_query shows wrong results


I do have subpage where I want to present & filter job postings for my clients company.

The filtering is based on two taxonomies:

  • job-category (examples: logistics, hr, marketing etc); filtering done by using select field called categoryfilter
  • job-country (examples: germany, France etc); filtering done by using select field called taxonomyfilter

What works:

  • filtering by countries
  • filtering by categories
  • filtering by both taxonomies

What doesn't work:

  • while filtering ONLY by countries the expected behaviour is to show only taxonomies and its posts that belong to specific country;
  • if a taxonomy job-country element do not have any post belonging, it should not be presented

In the current version of my code when I filter the results by country it shows the results in such a manner:

JOB CATEGORY 1
post
post

JOB CATEGORY 2
Sorry, no openings found

JOB CATEGORY 3
post
post
post

So simply I want to get rid of the entire JOB CATEGORY 2 (and not showing info about lack of openings). I do have an impression that the issue with my code is related to the part with taxquery if statements. Yet I am unable to diagonze whats wrong there :/

CODE:

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');

Solution

  • As suggested by CBroe in the comments. IMO, its the most viable solution.

    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');