Support

Account

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

Solving

Query posts by user relationship field

  • Hello,

    I have a custom post type with a user relationship field. The user field allows for multiple values.

    I am attempting to query all posts with that post type for which the user field contains the ID of the current user.

    As it is being saved as a serialized array, I am trying to query it with this code:

    $args      = array(
    		'meta_key'       => 'time_start',
    		'orderby'        => 'meta_value',
    		'order'          => 'ASC',
    		'post_type'      => $post_type,
    		'post_status'    => 'publish',
    		'posts_per_page' => - 1,
    		'meta_query'     => array(
    			array(
    				'key'     => 'shift_volunteers',
    				'value'   => '"' . $current_user_id . '"',
    				'compare' => 'LIKE'
    			),
    		)
    	);
    
    	$my_query = new WP_Query( $args );

    The query always returns 0 entries.

    In the database, the value is saved e.g. as:

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

    That’s a representation of…

    Array
    (
        [0] => 2
        [1] => 1
        [2] => 3
    )

    What am I doing wrong?

  • Value in meta query should contain only variable, like this

    
    		'meta_query'     => array(
    			array(
    				'key'     => 'shift_volunteers',
    				'value'   => $current_user_id,
    				'compare' => 'LIKE'
    			),
    		)
    

    Tested – work like a charm 🙂

  • @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' => '='
      ),
    ),
    
  • I think this might be a bug with ACF Pro – I ran into the same issue. Relationship fields don’t appear to be saving correctly when the fields are attached to the User object.

    An ACF relationship field on a Post saves as:
    a:1:{i:0;s:3:”777″;}

    While the same relationship field, made as a fresh field group, on a User saves as:
    a:1:{i:0;s:3:777;}

    I just spent 4 hours trying to figure out what was wrong with my meta query until I finally checked the database on a whim, and realized that the value isn’t saved the standard way to the User object (user_meta table).

    Is there a reason why it saves in a different format to the User format than it does to the Post format?

  • I just did a test of this and both user and relationship fields are saving string values in quotes in both the usermeta and postmeta table. Do you have any other plugins installed or filters created that could be interfering with or modifying the values of these relationships?

    This is actually a function of serializing the data before it is saved, so I’m a little confuse as to what could be causing it. In fact, the second value you posted a:1:{i:0;s:3:777;} will not unserialize and causes an error.

  • How to list the blog posts where id is either author id or participant id ACF user field in wordpress

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.