您必须将日期更改为@Krzysiek Dródżsaid或usemeta_type 选项。我已经用字符串版本进行了测试。
meta\\u类型示例$args
如下所列。另请参见MySQL time variants
/*
Plugin Name: Meta Filter
Plugin URI: https://wordpress.stackexchange.com/questions/304333
*/
// ?XDEBUG_SESSION_START=304333
function wpse_304333() {
echo \'<p>wpse_304333</p>\';
$args = array(
\'post_type\' => \'my-post-type\',
\'meta_key\' => \'my-date-as-string-yyyy-mm-dd\',
\'meta_value\' => \'2018-03-00\', // before march 2018
\'meta_compare\' => \'>\',
);
$query = new WP_Query( $args );
$titles = \'\';
while ( $query->have_posts() ) : $query->the_post();
$titles .= \'<br/>\' . get_the_title();
endwhile;
echo $titles;
wp_reset_postdata();
}
add_action( \'admin_notices\', \'wpse_304333\' );
使用包含以下内容的SQL
AND (
( wp_postmeta.meta_key = \'my-date-as-string-yyyy-mm-dd\'
AND wp_postmeta.meta_value < \'2018-03-00\' )
)
或使用meta\\u类型,如
$args = array(
\'post_type\' => \'my-post-type\',
\'meta_key\' => \'my-date-key\',
\'meta_value\' => $date_value
\'meta_compare\' => \'>\',
\'meta_type\' => \'DATETIME\'
);
它是使用CAST转换的SQL。您可以运行下面的SQL来检查哪些值是正确“强制转换”的。
SELECT meta_value, CAST(meta_value AS DATETIME)
FROM wp_postmeta
WHERE CAST(meta_value AS DATETIME) IS NOT NULL