简体   繁体   中英

Query User and get terms from a custom Taxonomy

Wordpress newbie here stuck a little bit.

I have created a custom taxonomy called 'Retailer Category' for Users as per http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress ... when adding a new user I can successfully select terms from this custom taxonomy that are then saved.

Using the tutorial above I can get all Users with a particular term however I'd actually like to get all terms from the custom Taxonomy (Retailer Category) for a logged in User ie to create a custom profile page that outputs the standard User info plus a list of the Retailer Categories the user selected.

Searching Google results in heaps of tutorials/code etc that mention Posts and Custom Post Types but nothing that I can see that mentions Users and a Custom Taxonomy.

Any help/pointer would be much appreciated. Thanks.

You should use the get_the_terms method and supply the user id and taxonomy and it will return an array of term objects associated to the user on success. False if no terms are found in the given taxonomy and a wp_error object if an invalid taxonomy is entered.

$terms = get_the_terms( $user_id, $taxonomy);

documentation: http://codex.wordpress.org/Function_Reference/get_the_terms

When I tried get_the_terms it didn't work. Looking into the code, it is specific to posts. The more general wp_get_object_terms works better. http://codex.wordpress.org/Function_Reference/wp_get_object_terms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM