我试图使用meta\\u键查询帖子列表,但返回的结果是一个空数组。
为了编写数据,我使用了以下代码:
$post_id = 2;
$user_key = \'ldp_system_follows_user_1\';
$user_data_new = array(
\'followed\' => \'nofollowed\'
);
update_post_meta($post_id, $user_key, $user_data_new);
执行时,上述代码在db中保留了以下内容
get_post_meta(post_id);
{
"ldp_system_follows_user_1": [
"a:1:{s:8:\\"followed\\";s:8:\\"followed\\";}"
]
}
我想检索与我编写的元数据匹配的所有post ID,但它返回了一个空数组。
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => $user_key,
\'value\' => $user_data_new,
\'compare\' => \'LIKE\'
))
);
return get_posts($args);
任何帮助都将不胜感激。
UPDATE 1:
正如phatskat所建议的,下面是我更改时的SQL语句
\'value\' => \'follow\'
和
\'value\' => \'nofollow\'
分别地但是,我仍在取回空数组:
//\'value\' => $user_data_new,
"query": {
"post_type": "post",
"post_status": "publish",
"meta_query": [
{
"key": "ldp_system_follows_user_1",
"value": {
"followed": "nofollowed"
},
"compare": "LIKE"
}
]
},
"request": "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( \\n ( wp_postmeta.meta_key = \'ldp_system_follows_user_1\' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE \'%followed%\' )\\n) AND wp_posts.post_type = \'post\' AND ((wp_posts.post_status = \'publish\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10",
"request": "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( \\n ( wp_postmeta.meta_key = \'ldp_system_follows_user_1\' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE \'%nofollowed%\' )\\n) AND wp_posts.post_type = \'post\' AND ((wp_posts.post_status = \'publish\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10",
SO网友:phatskat
要使用LIKE
比较,请尝试字符串:
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => $user_key,
\'value\' => \'followed\', // String here
\'compare\' => \'LIKE\',
),
),
);
return get_posts($args);
此外,您可以使用
WP_Query
要帮助您调试,请执行以下操作:
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'meta_query\' => array(
array(
\'key\' => $user_key,
\'value\' => \'followed\', // String here
\'compare\' => \'LIKE\',
),
),
);
$query = new WP_Query( $args );
echo \'<xmp>\';
print_r( $query->result );
die;
$query->result
应该包含生成的SQL,并将向您展示WordPress是如何准备元数据的。