WP不会“查看”您拥有的数据类型,它只是有一个默认值CHAR。因此,如果您转储当前查询,您将看到相应的类型转换:CAST(wp_postmeta.meta_value AS CHAR)
.
要使其工作,需要显式指定类型参数\'type\' => \'NUMERIC\'
演员阵容将相应改变:CAST(wp_postmeta.meta_value AS SIGNED)
.
看见Custom Field Parameters 有关支持的类型的更多信息,请参阅Codex。
这里的记录是如何转储生成的SQL,而不涉及太多的移动部件:
$meta_query_args = array(
\'relation\' => \'OR\',
array(
\'key\' => \'price\',
\'value\' => array( 80, 100 ),
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\',
),
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$sql = $meta_query->get_sql( \'post\', $wpdb->posts, \'ID\' );
/*
array (2) [
\'join\' => string (64) " INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )"
\'where\' => string (112) " AND (
( wp_postmeta.meta_key = \'price\' AND CAST(wp_postmeta.meta_value AS SIGNED) BETWEEN \'80\' AND \'100\' )
)"
]
*/