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