Php 将自定义帖子类型存档页面显示为主页
我的代码在functions.php文件中,根据需要显示自定义的post类型“events”posts,但不按meta值“comming_date”排序。它也不是按升序排序。此外,是否可以按2个元值排序?请帮忙Php 将自定义帖子类型存档页面显示为主页,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我的代码在functions.php文件中,根据需要显示自定义的post类型“events”posts,但不按meta值“comming_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
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;
}
add_action(“pre_get_posts”,“cpt_首页”);
功能cpt_首页($wp_查询){
//确保此筛选器未应用于管理区域
if(is_admin()){
返回;
}
如果($wp\u query->get('page\u id')==get\u选项('page\u on\u front')):
$wp_query->set('post_type','events');
$wp_query->set('page_id','');//空
//设置描述页面的属性以反映该属性
//我们并没有真正显示一个静态页面
$wp\u query->is\u page=0;
$wp\u query->is\u singular=0;
$wp\u query->is\u post\u type\u archive=1;
$wp\u query->is\u archive=1;
//按元值按升序对帖子排序
$wp_query->meta_key='comming_date';//orderby='meta_value';//order='ASC';//这很可能是关于如何存储即将到来的日期并尝试对其排序的问题。以yymmdd
格式存储日期,并将orderby
设置为'meta_value\n,您可能会获得最大的成功机会嗯‘
,因此它按数字顺序而不是文本顺序排序
$wp_query->meta_key = 'upcoming_date';
$wp_query->orderby = 'meta_value_num';
$wp_query->order = 'ASC';
这是一个很好的参考资料。检查一下:谢谢你的回答,我的代码中的实现如何?即:$wp\u query->order='ASC';
没有这样做。。。