有没有办法显示自定义帖子类型中最后一篇帖子(最近一篇)的创建日期?
我想让我的网站显示上一次更新产品列表的时间,我想它可以基于上一篇文章的创建或修改时间。这可能吗?
有没有办法显示自定义帖子类型中最后一篇帖子(最近一篇)的创建日期?
我想让我的网站显示上一次更新产品列表的时间,我想它可以基于上一篇文章的创建或修改时间。这可能吗?
根据你的要求和我的理解the_modified_date()
或者如果你只是想要价值get_the_modified_date()
.
如果您想要创建日期,可以使用post_date
和post_date_gmt
. 关于这方面的几个问题:
wp_query
. 我们有几个关于这个主题的问题。如果可以用代码进行编辑,我不介意尝试进一步帮助您。您需要对此进行自定义查询。基本上,只需从posts表中提取1个结果的查询,其中post类型是您的自定义post类型:
global $wpdb;
// the name you gave to your custom post type
$post_type = \'my_custom_post_type\';
// this is the query string that we\'ll use with the $wpdb class
// selecting from the posts table where the post_type is your custom post type
// ordered DESC and LIMITed to 1 to get on the most recent entry
$query_str = "SELECT * from " . $wpdb -> prefix . "posts WHERE post_type = %s ORDER BY post_date DESC LIMIT 1";
// it\'s good practice to $wpdb -> prepare queries to the database
$results = $wpdb -> get_results( $wpdb -> prepare( $query_str, $post_type ) );
我用过get_results
因此,您可以选择要使用的日期(创建日期或修改日期)我在get\\u帖子中有一个元查询,它需要花很长时间才能完成。它工作得很好,但只是时间太长了。我有一个名为event. 在每个event 发布后,有自定义元数据:post\\U sort\\U日期(事件日期YmdHis 格式,用于排序)我需要做的是获取下一个事件,该事件相对于$year 和$month 变量。所以如果$year = 2021 和$month = 10 (2021 10月)然后应该在2021 11月或之后找到第一个事件。我下面的查询很好,但很慢。执行大约需要40秒,我不知道为什么。$next