Wordpress pre_get_posts 和 date_query [英] Wordpress pre_get_posts and date_query
问题描述
我正在尝试使用 pre_get_posts 挂钩来更改年度存档结果,以便它向我显示整个学年的帖子.我使用的是 Wordpress 3.9.2 版.
I'm trying to use the pre_get_posts hook to change the yearly archive results so that it shows me posts over the academic year instead. I'm using Wordpress version 3.9.2.
function get_posts_by_academic_year($query) {
if ($query->is_main_query() && $query->is_year()) {
$query->set('date_query', array(
array(
'after' => array(
'year' => $year
, 'month' => 9
, 'day' => 1
),
'before' => array(
'year' => $year+1
, 'month' => 8
, 'day' => 31
),
'inclusive' => true
),
)
);
}
}
add_action('pre_get_posts', 'get_posts_by_academic_year');
我只得到一个 404 页面并且没有错误帮助我调试它.当谈到 Wordpress 我不经常使用它时,你必须原谅我的无知.关于我做错了什么或是否有更好的方法来做这件事的任何想法?
I get nothing but a 404 page and no errors to help me debug this. You'll have to excuse my ignorance when it comes to Wordpress I don't use it very often. Any ideas on what I'm doing wrong or whether there's a better way of doing this?
推荐答案
原因是你的 $year 变量为空...
The reason will be that your $year variable is empty...
从传递给函数的 $query 变量中填充它...
Populate it from the $query variable passed to the function...
$year = get_query_var('年');
$year = get_query_var( 'year' );
出于某种原因,当查询返回零结果时,我收到了 404 页面错误......而不是查询有问题.
For some reason i got a 404 page error when the query return zero results... rather than there being a problem with the query.
这篇关于Wordpress pre_get_posts 和 date_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!