0

This is my code:

$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'
    ),
);

I need to get the pictures that liked by users that I follow.

This is an example of how following_users meta is stored in the database:

a:1:{i:0;s:2:"87";}

So I try to get the user meta following_user by using an array but I failed it gives me a PHP error and also try extract PHP function but also failed.

Can anyone help me?

  • Do you only want to pass the "87" value in the array to your meta_query? – socki03 Oct 5 '17 at 15:42
  • @socki03 Thank you for your reply, as I explain I want to get the pictures that liked by users that I follow so to do that we must get the users that I follow from following_users meta, so don't focus about 87 I just write an example of how following_users meta is stored in the database, I hope you understand me and really hope help me on this. – Trello Oct 5 '17 at 15:51
0

This should be better for what you're looking for, the only issue might be how the meta gets returned, as an actual array, or as a string that needs to be turned into an array.

$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;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.