I am struggling with a search filtering. Firstly, my script is searching for users using natural search SQL. The query returns ids of users. Now I would like to filter them by terms which are assigned to each user. I used this:
$terms_ids = array(); $terms_ids = $_GET['user_sport']; $objects = get_objects_in_term($terms_ids, 'user_sport'); $unique = array_unique($objects); print_r($unique);
But this get_objects_in_term function returns every object which has at least one term. What I want to achieve is the opposite thing. If one user has football as a term and does not have basketball he should not be displayed when I filter users with football and basketball. Only if I use football. Is there any way of solving this problem? Another approach would be second SQL query but I found out terms and taxonomies relations a bit weird just to write SQL query.
Assuming “users” you are trying to pull is a custom post type. You can use “tax_query” as below.
$user_args = array( 'post_type' => 'custom_user_post', // Custom post type, 'posts_per_page' => 10, 'paged' => ( ! empty( get_query_var( 'paged' ) )? get_query_var('paged'): 1 ), 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'user_sports', //assuming your custom taxonomy is 'user_sport' 'field' => 'slug', 'terms' => array( 'football' ) ), array( 'taxonomy' => 'user_sports', //assuming your custom taxonomy is 'user_sport' 'field' => 'slug', 'terms' => array( 'basketball' ) ) ) ); $users_list = new WP_Query( $user_args );