Support

Account

Home Forums Front-end Issues WP_Query with user array field

Solved

WP_Query with user array field

  • Hello,

    I’m using ACF to set post access to selected users. In a single post template I can limit the accessibility of a post to the users that have been selected in a multi select custom field created by ACF. (see screenshot, ‘player’ is a custom user role).

    On another page template I’d like to query all posts that are available to the currently logged in user. This should output all posts that are made accessible with that previously explained user select ACF field (this field is called ‘myswing_player_access’).

    This is where I’ve got so far. I can’t figure out how to modify the meta_query in my WP_Query that it displays all posts where the selected user is equal to the currently logged in user…

    <?php						
    $client_id = get_current_user_id();
    $selected_players = get_field('myswing_player_access');
    
    if ( !empty($selected_players) ) : 
    foreach ($selected_players as $selected_player) {
    $myselectedplayers[] = $selected_player['ID'];
    }
    endif;
    ?>
    <?php
    $args = array (
    'post_type' => 'post',
    'posts_per_page' => '6',
    'author' => -$client_id,
    'meta_query' => array(
    array(	'key' 	=> 'myswing_public',
    'value' => '1',
    'compare' => 'NOT IN'),
    array(	'key' 	=> 'myswing_player_access',
    'value' => $client_id,
    'compare' => '='
    )) 
    );
      	
    $loop = new WP_Query( $args );
    if ($loop->have_posts()) : ?>
    <?php while($loop->have_posts()) : $loop->the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class('col-xs-4'); ?> >
       	post content
    </article>
    <?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
    
  • Hi @Exelmans Graphics
    Thanks for the info.

    because your field is a multi-select, the data will be saved into the DB as a serialized array of user ID’s.

    The WP_Query does not contain any feature to search for serialized data, this is why we need to use a LIKE hack.

    This is all documented on the reverse relationship documentation here:
    http://www.advancedcustomfields.com/resources/field-types/relationship/

    Note: The relationship field saves it’s data as a serialized array of post_ids. It is exactly the same as what you have, so the technique will work the same too

    Thanks
    E

  • Thanks! Works perfectly now!

  • I don’t recommend LIKE operator if you have values inside other values, for example, 23 and 123, 230, 2300. See http://www.simonbattersby.com/blog/2013/03/querying-wordpress-serialized-custom-post-data/
    I recommend split field into several fields.

    Regards,
    Josep

  • Hi! I just found this and it is EXACTLY what I need. Let me explain the scenario:

    I’m creating a shop where customers have access to a page where they can see some products that are assigned to them.

    So, naturally, I created a “User” field for the “product” post type, with the possibility (this is required) to assign multiple users, and to filter them by the “client” role. I assigned my test user to some of my test products and proceeded to create my loop. Here it is:

    <?php
        // The Query
        $args = array(
            'post_type'  => 'product',
            'meta_key'   => 'assign_client',
            'meta_value' => '2'
        );
        $the_query = new WP_Query( $args );
    
        // The Loop
        if ( $the_query->have_posts() ) {
            echo '<ul>';
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
                echo '<li>' . get_the_title() . '</li>';
            }
            echo '</ul>';
        } else {
            // no posts found
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    ?>

    As you can see, the name of my field is “assign_client” and my user’s ID is 2 (I was planning on using get_current_user_id() so it would filter by the logged in user). Evidently, it failed.

    That’s when I found this entry. Now, I understand (I think) the overall idea of the “Relationship” field, what I don’t get is how to assign a user to a post type through it. I changed my “assign_client” field to “Relationship”, but on the product page, all you can select through are post types, not users.

    Can you give me a hint on this? Thank you in advance!
    Francisco

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

The topic ‘WP_Query with user array field’ is closed to new replies.