是否在同一查询中包含分类和日期?

时间:2014-06-04 作者:f8xmulder

在最新的Wordpress中,是否可以将tax\\u查询与date\\u查询相结合?

以下是我的尝试:

$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);
但是,它忽略了日期查询,而只有分类查询起作用。当我删除分类查询部分时,日期查询突然起作用。

有什么想法吗?

3 个回复
最合适的回答,由SO网友:f8xmulder 整理而成

这是我最终使用的完整代码,因为似乎没有办法在查询中引入“OR”作为关系。这只是检查查询字符串中出现的查询,然后将正确的参数应用于WP\\U查询。希望这一切都有意义!

<?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);
?>

SO网友:Sven

仔细查看date parameters; 您需要输入一个整数(数字),而不是字符串(文本)。只需去掉单引号即可工作:

$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);

SO网友:f8xmulder

再挖一点。我能够基于querystring执行条件查询,并运行单独的查询。

这是我的最终解决方案:

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
);

结束