将自定义帖子类型存档页面显示为主页

时间:2015-09-27 16:38:07

标签: php wordpress-plugin wordpress-theming wordpress

我的代码在我的functions.php文件中,并显示自定义帖子类型' events'根据需要发布帖子,但不按元值排序&coming_date'。它也没有按升序排序。此外,是否可以按2个元值排序?请帮忙。

add_action("pre_get_posts", "cpt_front_page");
function cpt_front_page( $wp_query ){

    //Ensure this filter isn't applied to the admin area
    if(is_admin()) {
        return;
    }

    if($wp_query->get('page_id') == get_option('page_on_front')):

        $wp_query->set( 'post_type', 'events' );
        $wp_query->set( 'page_id', '' ); //Empty

        //Set properties that describe the page to reflect that
        //we aren't really displaying a static page     
        $wp_query->is_page = 0;
        $wp_query->is_singular = 0;
        $wp_query->is_post_type_archive = 1;
        $wp_query->is_archive = 1;

        //sort the posts by meta value in ascending order       
        $wp_query->meta_key = 'upcoming_date'; // <= IS THIS RIGHT?
        $wp_query->orderby = 'meta_value'; // <= IS THIS RIGHT?
        $wp_query->order = 'ASC'; // <= IS THIS RIGHT?

    endif;

}

1 个答案:

答案 0 :(得分:0)

这很可能是您如何存储即将到来的日期并尝试使用它进行排序的问题。通过以yymmdd格式存储日期并将orderby设置为'meta_value_num',您可能会获得最佳成功机会,以便按数字顺序排序,而不是按文字顺序排序。

$wp_query->meta_key = 'upcoming_date';
$wp_query->orderby = 'meta_value_num';
$wp_query->order = 'ASC';

documentation on orderby是一个很好的参考。