Skip to main content
deleted 3 characters in body
Source Link
user1575949
  • 153
  • 1
  • 11

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'];
    // FilterSeparate Numeric andfrom String Results- 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_filter$final_terms = array();
  $final_writers_filter$final_writers = (!empty($writers_filter)) ? $writers_filter : $all_writers;
  $final_terms_filter$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_filter$final_terms
    ))
  ); 

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

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'];
    // Filter Numeric and String Results 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_filter = array();
  $final_writers_filter = (!empty($writers_filter)) ? $writers_filter : $all_writers;
  $final_terms_filter = (!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_filter
    ))
  ); 

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

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',
    );
  }
Source Link
user1575949
  • 153
  • 1
  • 11

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'];
    // Filter Numeric and String Results 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_filter = array();
  $final_writers_filter = (!empty($writers_filter)) ? $writers_filter : $all_writers;
  $final_terms_filter = (!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_filter
    ))
  ); 

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