按用户元数组获取元查询值

时间:2017-10-05 作者:Trello

这是我的代码:

$type = "pictures";
$args[\'orderby\'] = "post_modified";
$args[\'order\'] = "DESC";
$args[\'post_type\'] = $type;
$args[\'post_status\'] = "publish";
$args[\'meta_query\'] = array(
    array (
        \'key\' => \'picture_liked_users\',
        \'value\' => array(get_user_meta($user->ID, \'following_users\', true)),
        \'compare\' => \'LIKE\'
    ),
);
我需要获得我关注的用户喜欢的图片。

以下是如何将以下\\u users meta存储在数据库中的示例:

a:1:{i:0;s:2:"87";}
因此,我试图通过使用数组来获取用户meta following\\u user,但失败了。这给了我一个PHP错误,并且尝试提取PHP函数,但也失败了。

有人能帮我吗?

1 个回复
最合适的回答,由SO网友:socki03 整理而成

对于您正在寻找的内容来说,这应该更好,唯一的问题可能是如何将meta作为实际数组或需要转换为数组的字符串返回。

$args = array(
     \'post_type\' => \'pictures\',
     \'orderby\' => \'post_modified\',
     \'order\' => \'DESC\'
     // post_status is unnecessary because default is \'publish\'
);

$following_users = get_user_meta($user->ID, \'following_users\', true);

if ( !empty( $following_users ) ) {

    $meta_query = array();

    foreach ( $following_users as $following_user ) {

        $meta_query[] = array(
                        \'key\' => \'picture_liked_users\',
                        \'value\' => $following_user,
                        \'compare\' => \'LIKE\'
                    );

    }

    // If our count is 2+, we need to set our relation
    if ( count( $meta_query ) > 1 ) {
        $meta_query[\'relation\'] = \'OR\';
    }

    // Only set our meta_query if there is anything to set
    if ( !empty( $meta_query ) ) {
        $args[\'meta_query\'] = $meta_query;
    }

}

结束