1

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

  • To add some context - I'm trying to get this to work on the taxonomy edit screen for now - domain.com/wp-admin/edit-tags.php?taxonomy=q_client&post_type=q_post_type – Q Studio Dec 27 '13 at 0:44
1

The issue with a taxonomy screen on the admin side is that it isn't a taxonomy archive. An archive is a collection of posts, a taxonomy admin screen is a collection of terms. The terms on those screens are loaded via get_terms, and the filter to modify those arguments is get_terms_args. Something like this should be able to catch those queries, where you can set the include argument:

function wpa_filter_term_args( $args, $taxonomies ) {
    global $pagenow;
    if( is_admin()
        && 'edit-tags.php' == $pagenow
        && 'q_client' == $taxonomies[0] ){
            // assuming $q_client_terms is an array
            $args['include'] = $q_client_terms; 
    }
    return $args;
}
add_filter( 'get_terms_args', 'wpa_filter_term_args', 10, 2 );
  • Excellent suggestion - I've added the code I'm using to clarify - thanks! – Q Studio Dec 27 '13 at 9:11
  • I have one strange issue now - if User A has a term "New Client" and User B add the same term "New Client" - they are saved correctly into the DB - with slug new-client-2 and the same name - but the filtering breaks.. but surely this is all done by the ID.. hmmm? I'll look further into this. – Q Studio Dec 27 '13 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.