0

I'm trying to only show the tags that are associated to the currently logged in author; I've got this 99% working, but the author can still choose tags associated to other users via the "Choose from the most used tags" link.

How can I edit this to exclude any tags whose meta tag "author" does not match the currently logged in user's ID?

I'm using this solution to limit everything else.

1

So that tag cloud seems to be a regular tag cloud generated by an AJAX request. This box appears to be the only place a tagcloud is generated via AJAX, so we can use that knowledge to use the get_terms filter to filter any term queries performed in that AJAX request.

function wpse_277075_filter_tag_cloud( $terms, $taxonomies, $args, $term_query ) {
    if ( is_admin() && isset( $_POST['action'] ) && $_POST['action'] === 'get-tagcloud' ) {
        $args['meta_query'] = array(
            array(
                'key'   => 'author',
                'value' => get_current_user_id(),
            )
        );

        $terms = $term_query->query( $args );

        return $terms;
    }

    return $terms;
}
add_filter( 'get_terms', 'wpse_277075_filter_tag_cloud', 10, 4 );

Inside the get_terms filter callback, I've checked for the $_POST['action'] variable because this will tell us if we're in the AJAX request. Then we re-perform the tag query, but this time with a meta_query filtering the results.

Note: This code assumes that the author is set via a meta_key of author which is set to a user ID.

  • Thanks, that works perfectly! I was overthinking it, trying to find a hook specific to the editor :) – JacobTheDev Aug 16 '17 at 15:29

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.