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