我有一个名为“产品”的自定义帖子类型。我的目标是使我在主页上的自定义查询以及搜索查询首先显示具有名为“id\\u number”的自定义字段的帖子不为空,然后显示具有“id\\u number”的其他帖子。所有应按标题排序。
这是迄今为止我的代码,仅用于自定义查询。我还需要它用于搜索页面查询,但我没有做到这一点。
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$wp_query = new WP_Query( array(
\'post_type\' => \'products\',
\'posts_per_page\' => 20,
\'post_status\' => \'publish\',
\'pagination\' => true,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'id_number\',
\'compare\' => \'EXISTS\',
),
array(
\'key\' => \'id_number\',
\'compare\' => \'NOT EXISTS\'
)
),
\'meta_key\' => \'id_number\',
\'orderby\' => \'title\',
\'paged\'=>$paged
) );
任何帮助都将不胜感激。非常感谢。
SO网友:Sumeet Shroff
Try this..
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$wp_query = new WP_Query( array(
\'post_type\' => \'products\',
\'posts_per_page\' => 20,
\'post_status\' => \'publish\',
\'pagination\' => true,
\'meta_query\' => array(
\'relation\' => \'OR\',
\'id_number\' => array(
\'key\' => \'id_number\',
\'compare\' => \'EXISTS\',
),
\'id2_number\' => array(
\'key\' => \'id_number\',
\'compare\' => \'NOT EXISTS\'
)
),
\'meta_key\' => \'id_number\',
\'orderby\' => \'id_number\',
\'paged\' => $paged
) );