1

Is is possible to combine a tax_query with a date_query in the most recent Wordpress?

Here's my attempt to do so:

$args = array(
 'post_type' => 'job',
'tax_query' => array(  
       array(  
        'taxonomy' => 'location',
        'terms' => 'dallas',
        'field' => 'slug',
       )  
 ),
 'date_query' => array(
    array(
        'year' => '2014',
        'month' => '12',
    ),
  )
);
$jobs = new WP_Query($args);

However, it is ignoring the date query and only the taxonomy query works. When I remove the taxonomy query portion, the date query suddenly works.

Any ideas on this?

0

Take a closer look at the date parameters; you'll need to enter an integer (number), not a string (text). Simply get rid of the single quotes to make it work:

$args = array(
  'post_type' => 'job',
  'tax_query' => array(  
    array(  
      'taxonomy' => 'location',
      'terms' => 'dallas',
      'field' => 'slug'
    )  
  ),
  'date_query' => array(
    array(
      'year' => 2014,
      'month' => 12
    ),
  )
);

$jobs = new WP_Query($args);
9
  • Actually, those are coming through as variables, so in my working code there are no quotes. It looks like this: 'year' => $year, – f8xmulder Jun 4 '14 at 23:45
  • I've just tested with real integers, no quotes, and it's still coming up empty. – f8xmulder Jun 4 '14 at 23:47
  • @f8xmulder Are you sure about the output of $year? Maybe this will help: (int)$year – Sven Jun 4 '14 at 23:51
  • Just tried 'year' => intval(2014) and 'year' => 2014 Still nothing... – f8xmulder Jun 4 '14 at 23:58
  • @f8xmulder Well, 'year' => 2014, 'month' => 12 is in the future; are you sure that there are posts? ;) – Sven Jun 5 '14 at 0:12
0

A bit more digging. I was able to do a conditional based on the querystring, and run a separate query.

Here's my final solution:

if($locationq) {
$qu = 'tax_query';
$typequery = array(  
    array(  
        'taxonomy' => 'location',
        'terms' => $locationq,
        'field' => 'slug',
        'operator' => 'IN',
        'include_children' => false
    )
);
} else if($monthq || $yearq) {
$qu = 'date_query';
$typequery = array(
    array(
        'year' => intval($yearq),
        'month' => intval($monthq),
        'compare' => '='
    )
);
} 
$jobargs = array(
'post_type' => 'career',
$qu => $typequery
);
0

Here is the full code I ended up using, since there doesn't seem to be a way of introducing "OR" as a relation in the query. This just checks which query is occuring from the query string, then applies the correct args to the WP_Query. Hope all this makes sense!

<?php
$locationq = $_REQUEST["location"]; 
$monthq = $_REQUEST['month'];
$yearq = $_REQUEST['year'];
$searchq = $_REQUEST['keyword'];

//list terms in a given taxonomy
$taxonomy = 'careerlocation';
$tax_terms = get_terms($taxonomy);
echo '<div class="careerbox locationpicker">';
echo '<select class="locationlinks">';
echo '<option value="" disabled="disabled" selected="selected">Select a region</option>';
echo '<option value="">View All</a></li>';
foreach ($tax_terms as $tax_term) {
    echo '<option value="?location='.$tax_term->slug.'">' . $tax_term->name . '</option>'."\n";
}
echo '</select>';
echo '</div>';

//list month and year of listed positions
echo '<div class="careerbox datepicker">';
echo '  <select class="datelinks">';
echo '    <option value="" disabled="disabled" selected="selected">Date Posted</option>';
$current_date = "";
$count_posts = wp_count_posts();
$nextpost = 0;
$published_posts = $count_posts->publish;
$myposts = get_posts(array(
  'post_type' => 'career',
  'posts_per_page' => $published_posts
)); 
foreach($myposts as $post) :
    $nextpost++;
    setup_postdata($post);
    $date = get_the_date("F Y");   
    if($current_date!=$date): 
        //echo '<option value="">'. $date .'</option>';
        $dt = strtotime($date);
        $month=date('m',$dt);
        $year=date('Y',$dt);
        echo '<option value="?month='.$month.'&year='.$year.'">'. $date .'</option>';
        $current_date = $date;
    endif;
endforeach; 
wp_reset_postdata();
echo '  </select>';
echo '</div>';
?>
           </div>
<?php
//now run query filter on either date or location
if($locationq) {
$qu = 'tax_query';
$typequery = array(  
    array(  
        'taxonomy' => 'careerlocation',
        'terms' => $locationq,
        'field' => 'slug',
        'operator' => 'IN',
        'include_children' => false
    )
);
} else if($monthq || $yearq) {
$qu = 'date_query';
$typequery = array(
    array(
        'year' => intval($yearq),
        'month' => intval($monthq),
        'compare' => '='
    )
);
} else if($searchq) {
$qu = 's';
$typequery = $searchq;
}
$jobargs = array(
'post_type' => 'career',
$qu => $typequery
);
$careersq = new WP_Query($jobargs);
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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