Support

Account

Home Forums General Issues Querying Posts by User Field Type

Solving

Querying Posts by User Field Type

  • I have a ‘User’ field type (called ‘participants’) set up to associate one or more WordPress users with a given post. On author.php I have the user meta data stored using:

    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

    and assumed I could use $curauth->ID as the meta_value in the below query to get the posts that are associated with the user/participant.

    This appears to work fine on my local server, but doesn’t on the live site.

    Any pointer would be much appreciated. Thanks, Alastair

    NB: This is the only loop on author.php ; the only additional code outputs author meta data such as website, biog description

    $posts = get_posts(array(
    'post_type' => 'post',
    'meta_key' => 'participants', // 'User' field type
    'meta_value' => $curauth->ID // 'User' field type value
    ));
    if($posts)
    {
    foreach($posts as $post)
    {
    get_template_part( 'partials/loop', 'resources' );
    }
    }
    wp_reset_query();
  • OK, replacing the original query with

     
    
    $args = array(
            'post_type' => 'post',
            'meta_query' => array(
    		array(
    			'key' => 'participants',
    			'value' => '"' . $curauth->id,
    			'compare' => 'LIKE'
    		)
    	)
        );
        $wp_query = new WP_Query();
        $wp_query->query( $args );
    
        while ($wp_query->have_posts()) : $wp_query->the_post();
    
     get_template_part( 'partials/loop', 'resources' ); 
    
    endwhile; 

    Appears to do the trick. Hope this helps anyone else in a similar bind. I will update this if anything breaks.

  • Tweaked the meta_query to add . '"' after $curauth->id to prevent some incorrect matches

    $args = array(
            'post_type' => 'post',
            'meta_query' => array(
    		array(
    			'key' => 'participants',
    			'value' => '"' . $curauth->id . '"',
    			'compare' => 'LIKE'
    		)
    	)
        );
        $wp_query = new WP_Query();
        $wp_query->query( $args );
    
        while ($wp_query->have_posts()) : $wp_query->the_post();
    
     get_template_part( 'partials/loop', 'resources' ); 
    
    endwhile; 
  • alastair38 how where you able to return the values from your relational field?

    Im having similar issues retrieving the user first name from the multi select field.

    Im attempting to do this outside the query loop. any chance in sharing your partials/loop?

  • I did it from within the loop so, while not totally straightforward, allowed me to use get_field('participants'), and then foreach( $participants as $participant ) to access each user/participant associated with a given post as an array. From there I could get the user details.

    However, this is not going to work outside of the loop. You could try http://codex.wordpress.org/Class_Reference/wpdb to get specific meta_key values

    I think the following would return an array of all users (where the meta_key is ‘participants’). As users information is itself stored as an serialized array, you’d then need to dig into this.

    $metakey = 'participants';
    $participants = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );

    I’m pretty new to PHP so don’t bet your house on anything here, although I’ve successfully tried all the above. Best of luck

  • Como seria en el caso contrario, si quisiera consultar con get_users y obtener los usuarios de un tipo de contenido con un campo tipo user?

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

The topic ‘Querying Posts by User Field Type’ is closed to new replies.