Wordpress pre_get_posts和date_query显示空白屏幕

时间:2018-02-16 03:06:08

标签: wordpress

我试图在wp_query上实施过滤器,以便按学年(9月 - 5月)显示年度档案,而不是jan-dec。当我查看年度存档页面时,例如sitename.com/2017/,我会看到死机的白屏,没有PHP错误。

我在functions.php中的代码如下所示:

add_action( 'pre_get_posts', 'school_year_filter' );

function school_year_filter($query) {
    if (! is_admin() && $query->is_main_query() && $query->is_year() && $query->is_archive()) {

        $year     = get_query_var('year');
        $year2    = $year + 1;

        $query->set(
            'date_query' => array(
                array(
                    'after'     => array(
                        'year'  => "$year",
                        'month' => '9',
                        'day'   => '1',
                    ),
                    'before'    => array(
                        'year'  => "$year2",
                        'month' => '5',
                        'day'   => '31',
                    ),
                    'inclusive' => true,
                ),
            ),
        );
    }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码。

我将此行'date_query'=>array(更改为'date_query', array(

add_action( 'pre_get_posts', 'school_year_filter' );

function school_year_filter($query) {
    if (! is_admin() && $query->is_main_query() && $query->is_year() && $query->is_archive()) {

        $year     = get_query_var('year');
        $year2    = $year + 1;

        $query->set(
            'date_query', array(
                array(
                    'after'     => array(
                        'year'  => $year,
                        'month' => '9',
                        'day'   => '1',
                    ),
                    'before'    => array(
                        'year'  => $year2,
                        'month' => '5',
                        'day'   => '31',
                    ),
                    'inclusive' => true,
                ),
            )
        );
    }
}

https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Only_Display_Search_Results_After_Specific_Date