Support

Account

Home Forums General Issues User query based on taxonomy field

Solving

User query based on taxonomy field

  • I am trying to build a user-query based on an acf taxonomy field (“fruits”):

    <?php
    $args = array(
    	'key' => 'fruits', 
    	'value' => 'apple'
    );
    
    // The Query
    $user_query = new WP_User_Query( $args );
    
    // User Loop
    if ( ! empty( $user_query->results ) ) {
    	foreach ( $user_query->results as $user ) {
    		echo $user->display_name;
    	}
    } else {
    	echo 'No users found.';
    }
    ?>

    but nothing happens. Any idea? Or is the a better way to do it?

  • Hi,

    What do you want to do ? Search users with specific values on ACF fields ?

    Because otherwise, WP_User_Query is a WordPress class, not ACF.

  • Yes i want to search users with a specific value. get it to work with normal custom fields but not with the taxonomy field used on users pages.

  • Ok. For this, I do as follow (excerpt from my code, so probably not perfect at all) :

    foreach( $args as $key => $value ) {
    
            $meta_query = array ( // logical operator between search field
                              'relation'  => 'AND',
                          );
    
            $meta_query[] = array(
                    'key'       => $key,
                    'value'     => $value,
                    'compare'   => 'IN',    // 'IN' if $value is an array, '=' for string
    
                );
    
    }
    $wp_query = new WP_User_Query( array( 'meta_query' => $meta_query ) );
    $resultats = $wp_query->get_results();
  • Hmmm, i have no search field i just want to show the users with the checked taxonomy field “bananas” on the page “bananas”. For this i show a taxonomy field (ACF) on every user page where the users see all categories listet. i have posts who are in the category “bananas” and i have a page with the title “bananas”. Andon this page i want to list all users who have checked the tyxonomy (category) “bananas” on their profile page.

  • I make it with a post-type query now, this works.

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

You must be logged in to reply to this topic.