wordpress 通过自定义元键订购 WP 帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6145984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:46:29  来源:igfitidea点击:

Ordering WP Posts by Custom Meta Key

wordpressunix-timestampgetdate

提问by cqde

I created a WordPress custom post type to be able to create events, select the event's date, and display the date on the frontend.

我创建了一个 WordPress 自定义帖子类型,以便能够创建事件、选择事件的日期并在前端显示日期。

I added a new meta_key in the postmeta of WP's database to store the event's date in a UNIX timestamp.

我在 WP 数据库的 postmeta 中添加了一个新的 meta_key 以将事件的日期存储在 UNIX 时间戳中。

I've had no trouble creating a new WP query to output my events on my site but I am trying to figure out how to organize the events by their UNIX timestamp in the database, not by the date that WordPress created the events.

我在创建新的 WP 查询以在我的网站上输出我的事件时没有遇到任何问题,但我试图弄清楚如何通过数据库中的 UNIX 时间戳来组织事件,而不是 WordPress 创建事件的日期。

I can't seem to wrap my head around the thing.. any advice?

我似乎无法理解这件事……有什么建议吗?

回答by yitwail

I believe your query can have

我相信您的查询可以有

'orderby' => 'meta_value_num',
'meta_key' => 'event_timestamp' //or whatever your meta_key is

you can read about it here: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

你可以在这里阅读:http: //codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

回答by Magnus Mio

Better use pre_get_posts:

更好地使用pre_get_posts

function ta_modify_main_query($query) {
   if ($query->is_main_query()) {
       $query->set('orderby', 'meta_value_num');
       $query->set('meta_key', '_liked');
       $query->set('order', 'DESC');
   }
}

add_action( 'pre_get_posts', 'ta_modify_main_query' );