Support

Account

Home Forums Front-end Issues Filter by User Taxonomy?

Helping

Filter by User Taxonomy?

  • I have been using Taxonomy to set users to a certain category of business they are in. When I filter normally this works for displaying all users:

    // $search = ( get_query_var( ‘search’ ) ) ? absint( get_query_var( ‘search’ ) ) : ”;

            $args = array(
                'number' => 7,
                'orderby' => $orderby,
                'paged' => $paged,
                'search' => '*'.esc_attr( $search ).'*',
           
            );
    
            $user_query = new WP_User_Query($args);
    
            $users = (array) $user_query->results;
    
            if(! empty( $user_query->results ) ) :
                foreach ($user_query->results as $user):
                    $category = get_field('organization_category', 'user_' . $user->ID);
            ?>
    
          <li>
             <div class="col-md-12 single-company">
               <div class="container-fluid close-box">
                 <div class="col-xs-1 no-padding">
                   <div class="directoryImgBox">
                     <img src="" alt="" class="directory-image">
                  </div>
                </div>
                 <div class="col-xs-10 directoryBox">
                 <ul>
                     <li class="directory-name"><a href="<?php echo get_author_posts_url( $user->ID ); ?>"><?php echo $user->display_name ?></a></li>
                     <li class="read-more"><a href="<?php echo get_author_posts_url( $user->ID ); ?>">More Info...</a></li>
                     <li class="businessType"><?php echo $category[0]->name; ?></p></li>
                 </ul>
                </div>
               </div>
             </div>
          </li>
    
      <?php endforeach;
    
        else:
            ?>
    
            <h3>No results found with your search!</h3>
    
            <?php
      endif;
    ?>
    

    I will be able to see all of the available users and see their taxonomy at the bottom.

    Now when I am trying to filter category it will return no results back using these arguments:

    $args = array(
                'number' => 7,
                'orderby' => $orderby,
                'paged' => $paged,
                'search' => '*'.esc_attr( $search ).'*',
                'meta_query' => array(
                    array(
                        'key' => get_field('organization_category')->name,
                        'value' => 'Heating',
                        'compare' => '='
                    )
                )
            );

    I am unsure as to why it’s doing this. Any help would be great.

  • The ACF taxonomy field stores an array of term ID values, you can’t search based on the term name or use ‘=’ for the comparison.

    You need to use the term id and you need to use LIKE, something like this

    
    'search' => '*'.esc_attr( $search ).'*',
                'meta_query' => array(
                    array(
                        'key' => organization_category,
                        'value' => '"'.$term->term_id.'"',
                        'compare' => 'LIKE'
                    )
                ).......
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Filter by User Taxonomy?’ is closed to new replies.