I have a custom taxonomy "q_client" - each time a new term is added to this tax the ID of the user is stored in user_meta.
So each user has an array of id of the terms they have added - a quick hack to get around the lack of a taxonomy_meta table.
So - I'd like to use pre_get_posts to only include the terms that this user has added on ALL views - admin and front-end.
Here is what I've got so far:
Add the action:
add_action( 'pre_get_posts', array ( $this, 'tax_query' ), 2 );
The method
public function tax_query( $query )
{
// get our existing user meta - or an empty array ##
$q_client_terms = $this->get_q_client_terms();
wp_die(var_dump($query));
if ( $query->is_tax( 'q_client' ) ) {
wp_die(var_dump($q_client_terms));
$taxquery = array(
array(
'taxonomy' => 'q_client',
'field' => 'id',
'terms' => $q_client_terms,
'operator' => 'IN'
)
);
$query->set( 'tax_query', $taxquery );
return $query;
}
}
The main problem is I can't get is_tax or any other query variable to catch correctly - var_dump($query) gives a boolean false on is_tax() - it does not even know which post type this is on when editing the taxonomy via the admin.
Any help would be much appreciated - thanks!
EDIT
Thanks to @Milo - here is the code I now use:
function get_terms_args( $args, $taxonomies ) {
global $pagenow; // required if you want to only filter on certain pages ##
if(
//is_admin()
//&& 'edit-tags.php' == $pagenow
//&&
'q_client' == $taxonomies[0]
) {
// get our existing user meta - or array ( -1 ) to exclude all terms ##
$q_client_terms = $this->get_q_client_terms();
// add array of terms to the include key ##
$args['include'] = $q_client_terms;
}
// kick back args Array ##
return $args;
}
One thing to note is that if each user has not added any terms and your return an empty array or an array( 0 ) - they will be shown all the terms - so use array( -1 ) to ensure they are all hidden
I've used this filter on all requests to this taxonomy as I want to filter everywhere - not just the admin