Support

Account

Home Forums ACF PRO Calling Users with meta_query and an ACF checkbox field

Helping

Calling Users with meta_query and an ACF checkbox field

  • I have additional ACF fields added to my users. One of those is a checkbox area where my users can check off all of the various departments that they personally belong to. Users can select as many as they want.

    The field name is “department“.
    And there are values available like:

    • Social Work
    • Criminal Justice
    • Public Affairs

    I want to show a list of all of my users that have checked the box for Social Work… even if they have also checked other box options as well.

    Below is the short code that I am trying to build, however this returns me nothing. What am I missing to be able to pull out all users with Social Work Checked?

    <?php
    add_shortcode('show_valuesofstuff', function() {
    ?>
    <!-- START CONTENT SECTION -->	
    <?php
    	$args1 = array(
        'meta_key' => 'last_name',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'exclude' => array(1,8,9),
        'meta_query' => array(
            'relation' => 'AND',
            'department' => array(
                'key' => 'department',
                'value' => 'Social Work',
    	    'compare' => 'IN',
            ),
        )
    );	
     $subscribers = get_users($args1);
    echo '<ul>';
     foreach ($subscribers as $user) {
     echo '<li>' . $user->display_name.' ['.$user->phone_number . ']</li>';
     }
    echo '</ul>';
    ?>
    <!-- END CONTENT SECTION -->	
    
    <?php	
    });// END SHORTCODE [show_valuesofstuff]?>

    If anyone could provide suggestions, or example code, that would be excellent!
    Thanks so much!

  • Checkbox fields store serialized arrays and do not store the values in standard WP meta fields that can be searched using ‘IN’.

    You need to use “LIKE”

    
    array(
      'key' => 'department',
      'value' => '"Social Work"',
      'compare' => 'LIKE',
    )
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.