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

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

问题描述:

我的代码是在我的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; 

} 
+0

看看这个:http://stackoverflow.com/questions/11100473/ordering-wordpress-posts-with-meta-values –

+0

谢谢你的答案,如何将我的代码中的实现?即:'$ wp_query-> order ='ASC';'不是这样做的...... – karlosuccess

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

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

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