我的目标是通过meta\\u键查询帖子,值介于1-100之间。拉动时,假设从1到100升序排列。
$post = array(
\'posts_per_page\' => 10,
\'post_type\' => \'post\',
\'tax_query\' => array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => \'category-1\'
),
\'meta_query\' => array(
\'meta_key\' => \'custom-key\',
\'meta_value\' => array(1,100),
\'meta_compare\' => \'BETWEEN\'
),
\'order\' => \'ASC\',
\'orderby\' => \'meta_value_num\',
);
当查看页面时,它可以获得带有meta值“1-100”的帖子,但排序不正确。它看起来是随机的(即1、21、5、20、6、7、8、3、9、19),即使在页面不断刷新的情况下也会按此顺序显示。
任何帮助都将不胜感激。
谢谢
SO网友:Roc
通过进一步研究,我得到了以下代码
$post = array(
\'posts_per_page\' => 10,
\'post_type\' => \'post\',
\'tax_query\' => array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => \'category-1\'
),
\'meta_query\' => array(
array(
\'key\' => \'custom-key\',
\'value\' => array(1,100),
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\'
)
),
\'meta_key\' => \'custom-key\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\'
);
参考号:
http://www.wphub.com/use-meta_query-query-posts-postmeta/