I struggle a bit with my piece of code - it's main goal is to show list of custom post type (called "career") childs grouped by custom taxonomy.
How it should work? A user see default list of ALL available posts (from that career CPT). On top of that there are two dropdowns each field with terms from custom taxonomies. First custom taxonomy is called job-category and the second job-country. Once a user selects specific value from a dropdown and click 'filter' the list of shown posts changes. So all in all it is a simple filtering mechanism.
What works and what's not? The filtering itself works ok. So if a user select any value from job-category list and filter the results it shows proper list in this way:
//CUSTOM TAXONOMY (job-category) TERM NAME
list of assigned posts
No other posts are suppose to be shown.
If a user selects value from BOTH dropdowns (so assign parameters from both taxonomies job-country and job-category) it also works in that way
//CUSTOM TAXONOMY (job-category) TERM NAME
list of assigned posts
The only thing that currently doesn't work is a condition when a user selects value from taxonomy job-country. I would like to be displayed in the same way as previously so
//CUSTOM TAXONOMY (job-category) TERM NAME
list of assigned posts
Now instead it shows just full list of posts meeting criteria for 'job-country'. Yet there is no grouping by taxonomy 'job-category'. This is my current code:
<?php
$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'
);
if( isset( $_POST['categoryfilter'] ) && isset ($_POST['taxonomyfilter']) ) {
$args['tax_query'][] = array(
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(
'taxonomy' => 'job-country',
'field' => 'name',
'terms' => $_POST['taxonomyfilter']
);
} elseif( isset( $_POST['categoryfilter'] ) && !isset ($_POST['taxonomyfilter']) ) {
$args['tax_query'][] = array(
'taxonomy' => 'job-category',
'field' => 'name',
'terms' => $_POST['categoryfilter']
);
}
$query = new WP_Query( $args );
if( $query->have_posts() ) :
if(isset($_POST['categoryfilter'])) {
echo '<h2 class="category-title">'. $_POST['categoryfilter'] .'</h2>';
}
while( $query->have_posts() ): $query->the_post();
?>
<div class="job d-flex align-items-center">
<h3 class="job__title col-12 col-lg"><?php echo $query->post->post_title ; ?></h3>
</div>
<?php
endwhile;
wp_reset_postdata();
else :
echo '<p class="text-center">Sorry, no openings found :-(</p>';
endif;
die();