Monday, January 23, 2023
HomeWordPress DevelopmentQuestion customized submit kind and group by taxonomy

Question customized submit kind and group by taxonomy


I wrestle a bit with my piece of code – it is principal objective is to point out record of customized submit kind (referred to as “profession”) childs grouped by customized taxonomy.

The way it ought to work?
A consumer see default record of ALL out there posts (from that profession CPT). On prime of that there are two dropdowns every subject with phrases from customized taxonomies. First customized taxonomy is named job-category and the second job-country. As soon as a consumer selects particular worth from a dropdown and click on ‘filter’ the record of proven posts adjustments. So all in all it’s a easy filtering mechanism.

What works and what’s not?
The filtering itself works okay. So if a consumer choose any worth from job-category record and filter the outcomes it reveals correct record on this approach:

//CUSTOM TAXONOMY (job-category) TERM NAME
record of assigned posts

No different posts are suppose to be proven.

If a consumer selects worth from BOTH dropdowns (so assign parameters from each taxonomies job-country and job-category) it additionally works in that approach

//CUSTOM TAXONOMY (job-category) TERM NAME
record of assigned posts

The one factor that presently does not work is a situation when a consumer selects worth from taxonomy job-country. I want to be displayed in the identical approach as beforehand so

//CUSTOM TAXONOMY (job-category) TERM NAME
record of assigned posts

Now as a substitute it reveals simply full record of posts assembly standards for ‘job-country’. But there isn’t any grouping by taxonomy ‘job-category’. That is my present code:

<?php

$args = array(
    'orderby' => 'date', // we are going to type posts by date
    'order' => $_POST['date'], // ASC or DESC
    'post_type' => 'profession',
    'posts_per_page' => -1,
'post_status' => 'publish'
);


if( isset( $_POST['categoryfilter'] ) && isset ($_POST['taxonomyfilter'])  ) {
  $args['tax_query'][] = array(
      array(
          'taxonomy' => 'job-category',
          'subject' => 'title',
          'phrases' => $_POST['categoryfilter']
      ),
      array(
          'taxonomy' => 'job-country',
          'subject' => 'title',
          'phrases' => $_POST['taxonomyfilter']
      ),
  );

} elseif( !isset( $_POST['categoryfilter'] ) && isset ($_POST['taxonomyfilter'])  ) {
  $args['tax_query'][] = array(
      'taxonomy' => 'job-country',
      'subject' => 'title',
      'phrases' => $_POST['taxonomyfilter']
  );

} elseif( isset( $_POST['categoryfilter'] ) && !isset ($_POST['taxonomyfilter'])  ) {
  $args['tax_query'][] = array(
      'taxonomy' => 'job-category',
      'subject' => 'title',
      'phrases' => $_POST['categoryfilter']
  );
}

$question = new WP_Query( $args );

if( $query->have_posts() ) :

if(isset($_POST['categoryfilter'])) {
  echo '<h2 class="category-title">'. $_POST['categoryfilter'] .'</h2>';
}
    whereas( $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 discovered :-(</p>';
endif;

die(); 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments