0

I have a social site that contains follow users and like posts system.

I need to get the posts that my following users are liked.

I try something like this but it's not working:

if(get_query_var( 'page' )){
    $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1;
} else {
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
}

$get_following_users = get_user_meta($current_user->ID, 'the_following_users', true);

$likes_args = array(
    'paged'          => $paged,
    'orderby'        => 'post_modified',
    'order'          => 'DESC',
    'post_type'      => 'post_type',
    'post_status'    => 'publish',
);

$meta_query = array();
foreach ( $get_following_users as $following_user ) {
    $meta_query[] = array(
                    'key' => 'liked_users',
                    'value' => $get_following_users,
                    'compare' => 'LIKE'
                );
}

if ( count( $meta_query ) > 1 ) {
    $meta_query['relation'] = 'OR';
}

if ( !empty( $meta_query ) ) {
    $likes_args['meta_query'] = $meta_query;
}

$wp_query = new WP_Query($likes_args);

if($wp_query-> have_posts()){
    while ( $wp_query->have_posts() ) : $wp_query->the_post();
        get_template_part( 'template/post', 'loop' );
    endwhile;
}
wp_reset_query();

NOTES:

  • [the_following_users] user meta is stored in the database like this: a:1:{i:0;s:1:"1";}

  • [liked_users] post meta is store on the database like this: a:8:{i:9;i:9;i:57;i:57;i:3;i:3;i:71;i:71;i:85;i:85;i:89;i:89;i:152;i:152;i:2;i:2;}

Here's how i store the likes into the database:

// Update Post
$post_user_likes = post_user_likes( $user_id, $post_id );
if ( $post_user_likes ) {
    update_post_meta( $post_id, "liked_users", $post_user_likes );
}

function post_user_likes( $user_id, $post_id ) {
    $post_user_likes = '';
    $the_meta_users = get_post_meta( $post_id, "liked_users" );
    if ( count( $the_meta_users ) != 0 ) {
        $post_user_likes = $the_meta_users[0];
    }
    if ( !is_array( $post_user_likes ) ) {
        $post_user_likes = array();
    }
    if ( !in_array( $user_id, $post_user_likes ) ) {
        $post_user_likes[$user_id] = $user_id;
    }
    return $post_user_likes;
}
  • 1
    You can't. That data is a 'serialized array'. You can't do database queries on the values they contain. You'll need to store the data differently. Did you write the like system? – Jacob Peattie Dec 19 '17 at 3:38
  • @JacobPeattie Yes I write this system and all my systems are stored in the database as a 'serialized array' and when I use wp-query or wp-user-query in the value I add serialize(strval($value)), I edited the question and add my codes. – Michel Dec 19 '17 at 10:01
  • Jacob is right, I think you have to change your system with a new relationship like table (user_id / post_id) – Elex Dec 19 '17 at 15:20
  • @Jean-RémiLARCELET-PROST Are you sure there's no solution to do this without tables? – Michel Dec 19 '17 at 15:33
  • @JacobPeattie can you help me on this? – Michel Dec 21 '17 at 12:13

Your Answer

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

Browse other questions tagged or ask your own question.