我尝试获取没有自定义字段集的帖子,或者if set的值与给定的参数不同。
这是我的代码:
$args_included = array(
\'numberposts\' => 1,
\'post_type\' => \'post\',
\'post_status\' => \'published\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'requested_by\',
\'value\' => \'%\' . $SANITIZED_PARAM . \'%\',
\'compare\' => \'NOT LIKE\'
),
array(
\'key\' => \'requested_by\',
\'compare\' => \'NOT EXISTS\'
)
)
);
get_posts($args_included); // empty array
自定义字段
requested_by
应为空或用如下字符串值填充:
foo;bar;gamma;
.
如果我删除这部分代码:
\'relation\' => \'OR\',
array(
\'key\' => \'requested_by\',
\'value\' => \'%\' . $SANITIZED_PARAM . \'%\',
\'compare\' => \'NOT LIKE\'
),
然后查询就可以工作了。
我错过了什么?
WP 3.5
UPDATE以下是我的最终代码:
$args_included = array(
\'numberposts\' => 1,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => \'requested_by\',
\'value\' => $PARAM,
\'compare\' => \'NOT LIKE\'
)
)
);
get_posts($args_included); // Just ONE post
错误:
状态为publish
不published
自定义字段不存在,因此找不到结果
下面是针对批量设置自定义字段的SQL查询:
INSERT INTO wp_postmeta(post_id,meta_key,meta_value)
SELECT ID , \'requested_by\', \';\'
FROM wp_posts
WHERE post_status=\'publish\'
AND post_type=\'post\'
最后的调试提示。您可以将最终SQL查询发送到MySQL,如下所示:
$args = array(
\'numberposts\' => 1,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => \'requested_by\',
\'value\' => $PARAM,
\'compare\' => \'NOT LIKE\'
)
)
);
$q = new WP_Query($args);
echo $q->request;