Retrieve posts from meta key

时间:2015-11-02 作者:Loc Pham

我试图使用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",

1 个回复
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是如何准备元数据的。

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在