admin管理员组

文章数量:1291609

Users can choose a value from a multiple choice checkbox on sign up. In this example the choices are Cat or Dog.

I have a WP User Query and want to count the amount of users that have each meta value, and display something like '45 Cat People, 15 Dog People'.

Below are two separate queries but I'd like to loop through the values and display using one foreach query but not sure how to access the meta value to do this. Also the ability to output the value name rather than hard coding it would be useful...

    <?php

    // Meta value 'Cats'

    $user_query = new WP_User_Query(array(
        'role' => 'Subscriber', 
        'meta_query' => array(
        array(
            'key' => 'favourite_animal',
            'value' => 'Cat',
        ),
      )
    ));

    $users = $user_query->get_results();
    $total = $user_query->get_total(); 

    echo $total . 'Cat people';


    // Meta value 'Dogs'

    $user_query = new WP_User_Query(array(
        'role' => 'Subscriber', 
        'meta_query' => array(
        array(
            'key' => 'favourite_animal',
            'value' => 'Dog',
        ),
      )
    ));
    
    $users = $user_query->get_results();
    $total = $user_query->get_total(); 

    echo $total . 'Dog people'; 
            
    ?>

本文标签: loopQuery users and count totals based on meta values