Support

Account

Home Forums General Issues Query Users from Tax Field (and children)

Solved

Query Users from Tax Field (and children)

  • Hi, this is probably an uncommon query.

    But i’m using a tax field to group my users.
    and I can query it fine using this

    
    $args = array (
        'numberposts'   => -1,
        'order'         => 'ASC',
        'orderby'       => 'display_name',
        'meta_query'    => array(
            //'relation'    => 'OR',
            array(
                'key'       => 'department',
                'compare'   => 'LIKE',
                'value'     => '"' . $queriedTaxTerm->term_id . '"',
            )
        )
    );
    $wp_user_query = new WP_User_Query($args);
    

    But I realize now I also need to grab the Users with a Tax-parent of the same id.
    Luckily the Custom Field is set to “Term Object” and should theoretically be able to query for the parent key in the Object.
    But I have no idea how I can do this though.

    Basically: How can I query for “parent” in a Term Object Custom Field?

  • No, setting the field to return the term object does not store the term object in the database. The only thing that ACF stores in the DB is the term ID, no matter what you want to return.

    You would need to get the parents of the term and then add them all to your query

    
    $args = array (
      'numberposts'   => -1,
      'order'     => 'ASC',
      'orderby'     => 'display_name'
    );
    $meta_query = array(
      'relation'  => 'OR', // this is required, default is AND
      array(
        'key'     => 'department',
        'compare'   => 'LIKE',
        'value'   => '"' . $queriedTaxTerm->term_id . '"',
      )
    );
    $ancestors = get_ancestors($queriedTaxTerm->term_id, 'your-taxonomy-slug-here', 'taxonomy');
    if ($ancestors) {
      foreach ($ancestors as $ancestor) {
        $meta_query[] = array(
          'key'     => 'department',
          'compare'   => 'LIKE',
          'value'   => '"' . $ancestor . '"',
        );
      }
    }
    $args['meta_query'] = $meta_query;
    $wp_user_query = new WP_User_Query($args);
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Query Users from Tax Field (and children)’ is closed to new replies.