我试图返回特定查询的帖子。使用以下代码:
$meta_query = array(
\'relation\' => \'OR\',
array(
\'key\' => \'tic_assignment\',
\'value\' => $current_user -> ID,
\'compare\' => \'=\',
),
array(
\'key\' => \'tic_department\',
\'value\' => $_depts,
\'compare\' => \'LIKE\',
),
);
$_args = array ( \'post_type\' => \'tickets\',
\'meta_query\' => $meta_query,
\'posts_per_page\' => 10,
\'orderby\' => \'date\',
\'order\' => \'DESC\', );
$the_query = new WP_Query( $_args );
现在,它不再根据添加的查询返回我的帖子,而是返回所有帖子。
我有var_dump
教育部$the_query
返回的内容如下:https://pastebin.com/PPYaJpSu
(它太大了,不能全部粘贴在这里)
即使我取出第二个数组,它仍然返回所有内容。。。当它不应返回任何基于tic_assignment
当我将参数修改为:
$_args = array ( \'post_type\' => \'tickets\',
\'meta_key\' => \'tic_assignment\',
\'meta_value\' => $current_user -> ID,
\'meta_compare\' => \'=\',
\'posts_per_page\' => 10,
\'orderby\' => \'date\',
\'order\' => \'DESC\', );
$the_query = new WP_Query( $_args );
构造正确的查询,并返回正确的结果。
有什么好处?
最合适的回答,由SO网友:Elex 整理而成
你忘了meta_query
输入您的$args
.
$meta_query = array(
\'relation\' => \'OR\',
array(
\'key\' => \'tic_assignment\',
\'value\' => $current_user -> ID,
\'compare\' => \'=\',
),
array(
\'key\' => \'tic_department\',
\'value\' => $_depts,
\'compare\' => \'LIKE\',
),
);
$_args = array ( \'post_type\' => \'tickets\',
\'meta_query\' => $meta_query,
\'posts_per_page\' => 10,
\'orderby\' => \'date\',
\'order\' => \'DESC\', );
$the_query = new WP_Query( $_args );