WP_query – Filter by tax_query and meta_query using multiple select

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)