0

I have a Publications Post Type, a Writers Taxonomy and a Publications Custom 'Meta Box' Value; parent_id.

In my archive-publictions-post.php template I am trying to filter the WP_Query Class, with the tax_query and meta_query Parameters.

With the code below I can get all Publications to show on page load and I can get filtered results when I select both a Term and a Parent together but do not when selected individually?

I am not sure about how the meta_query & tax_query relationship parameter should be used in this case?

<?php
  // The code from: archive-publictions-post.php

  // Set Var Filter
  $publications_filter = $_POST['publications'];

  // Set Terms Arguments
  $term_args = array(
    'orderby' => 'name',    
    'order' => 'ASC',     
    'hide_empty' => false,    
    'parent' => 0,      
  );
  // Get Publications Terms
  $terms = get_terms('publications-tax', $term_args);

  $writers_args = array( 
    'post_type' => 'writers-post', 
    'posts_per_page' => -1,
    'post_status' => 'any',
    'order' => 'DESC',
  ); 
  // Get Writers Posts
  $writers = get_posts($writers_args);

    echo '<select name="publications[]" multiple>';
      // Set var as an array
      $all_writers = array();
      foreach($writers as $writer){
        // Set all terms to our array
        $all_writers[] = $writer->ID;
        echo '<option value="'.$writer->ID.'">'.$writer->post_name.'</option>';
      }
      foreach($terms as $term){
        // Set all terms to our array
        $all_terms[] = $term->name;
        echo '<option value="'.$term->name.'">'.$term->name.'</option>';
      }          
    echo '</select>';         
    echo '<input type="submit" value="go">';
  ?>
  </form>

WP_QUERY $args:

   //Start Args

    $publications_results = isset($_POST['publications']) ? $publications_filter : $all_terms;

    $publication_values=array();
    $writers=array();
    foreach ($publications_results as $key => $value) {
      if (is_numeric($value)) {
        $writers_values[] = $value;
      }
      else{
        $publications_values[] = $value;
      }
    }
    $args = array(   
      'post_type' => array('publications-post','writers-post'),
      'order' => 'DESC',
      'post_status' => 'any',
      'orderby' => 'date',
      'tax_query' => array(array(
        'taxonomy' => 'publications-tax',
        'field' => 'slug',
        'terms' => $publications_values
      ))
    ); 

    if($_POST['publications']){
      $args['meta_query'][] = 
      array(
        'key' => 'parent_id',
        'value' => $writers_values,
        'type' => 'numeric',
      );
    }

Thanks

1 Answer 1

2

My initial code did not provide default WP_query argument parameters if either Select option was not set - the empty Parameter would filter everything out.

To the best of my ability, I applied default parameter values from the initial Post and Terms queries and now the select filter is working.

I would be pleased to know if my code can be improved upon :)

  /*
  *Get Publications Filters 
  */

  $writers_filter = array();
  $publications_filter = array();

  if (isset($_POST['publications'])) {

    $publications_filter = $_POST['publications'];
    // Separate Numeric from String - i.e. Post ID and Term Name
    foreach ($publications_filter as $key => $value) {
      if (is_numeric($value)) {
        $writers_filter[] = $value;
      }
      else{
        $terms_filter[] = $value;
      }
    }
  }      

  // If Either Select Option is Empty, 
  // Assign Final Value With All Queried Results - Avoid Empty Value 

  $final_writers = array();
  $final_terms = array();
  $final_writers = (!empty($writers_filter)) ? $writers_filter : $all_writers;
  $final_terms = (!empty($terms_filter)) ? $terms_filter : $all_terms;

  /*
  *Set WP_Query Argument Parameters
  */

  $args = array(   
    'post_type' => array('publications-post','writers-post'),
    'order' => 'DESC',
    'post_status' => 'any',
    'orderby' => 'date',
    'tax_query' => array(array(
      'taxonomy' => 'publications-tax',
      'field' => 'slug',
      'terms' => $final_terms
    ))
  ); 

  if($_POST['publications']){
    $args['meta_query'][] = 
    array(
      'key' => 'parent_id',
      'value' => $final_writers,
      'type' => 'numeric',
    );
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.