2015-09-27 148 views
0

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

回答

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是一個很好的參考。

相關問題