Support

Account

Home Forums General Issues How to return all user are selected field

Helping

How to return all user are selected field

  • Hello,

    How to return all users in foreach are selected game A.

    Mr Bean have selected game A (field acf checkbox A)
    Mlle Andrew have selecte game A and B (field acf checkbox A/B)

    How to return a foreach with same :

    foreach only game A loop
    Mr Bean and Mlle andrew

    Thanks for help

  • Hi @shepxbfr

    You would need to use the WP_User_Query

    If you check the ACF docs on checkboxes, you can see how to query them here

    So something like:

    <?php
    $args = array(
        'role' => 'subscriber',
    	'meta_query' => array(
            array(
                'key'     => 'game',
                'value'   => '"A"',
                'compare' => 'LIKE'
            )
        )
    );
    
    // The Query
    $user_query = new WP_User_Query( $args );
    
    // User Loop
    if ( ! empty( $user_query->get_results() ) ) {
    	foreach ( $user_query->get_results() as $user ) {
    		echo '<p>' . $user->display_name . '</p>';
    	}
    } else {
    	echo 'No users found.';
    }
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.