admin管理员组文章数量:1298120
I have a start date and an end date saved as post meta fields (so strings). I want to return posts between two date ranges. I thought it would be a simple string comparison - one datetime represented as a string should still be able to be compared (<= and >=) with another datetime saved as a string. But MySQL is returning unexpected results. For example, of the 13 valid records it should return for a particular search, it returns a (seemingly) random subsection, sometimes 3 records, sometimes 6 records, sometimes 8 records, sometimes a different 6 records, and so on, for queries only seconds apart.
This is the query I'm using:
$now_date = date( 'Y-m-d\TH:i', time() );
$meta_args = array(
'relation' => 'AND',
array(
'key' => 'package_startdate',
'value' => "$now_date",
'compare' => '<',
),
array(
'key' => 'package_enddate',
'value' => "$now_date",
'compare' => '>',
),
);
$args = array(
'post_type' => 'saleitems',
'posts_per_page' => 50,
'meta_query' => $meta_args,
'meta_key' => 'display_order',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$query = get_posts( $args );
A typical post meta for package_startdate or package_enddate might be 2022-04-29T10:30.
Wordpress doesn't appear to allow a simple way to utilise STR_TO_DATE or similar in these queries and I can't work out why it's not returning the correct results.
本文标签: postsReturn records between two meta datetimes saved as strings
版权声明:本文标题:posts - Return records between two meta datetimes saved as strings 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738515764a2091074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论