Support

Account

Home Forums Front-end Issues Query posts by user relationship field Reply To: Query posts by user relationship field

  • @marcusw I don’t see anything wrong with your query that should cause it to not function. The value of the field should have quotes around it, otherwise a value like this would also return posts

    
    a:3:{i:0;s:1:"5";i:1;s:1:"1";i:2;s:1:"3";}
    

    if the user id you are querying for is “2”, without the quotes the array index 2 would return this post.

    The only thing I can think of by looking at your query args is that either $post_type or $current_user_id is incorrect.

    Another cause of your issue would be if you originally saved values to the field when it was set to only allow a single user and then you changed the field to allow multiple users. In this case the field value will be holding an integer instead of an array.

    This change to the meta query might correct for that

    
    
    'meta_query'     => array(
      'relation' => 'OR',
      array(
        'key'     => 'shift_volunteers',
        'value'   => '"' . $current_user_id . '"',
        'compare' => 'LIKE'
      ),
      array(
        'key'     => 'shift_volunteers',
        'value'   => $current_user_id,
        'compare' => '='
      ),
    ),