Connect Users and Taxonomies

I’d like to send email notifications when new posts are saved. But users can select post categories they want to subscribe to. Available plugins are no solution to me – I really need my own code base. Even though I am fairly new to WP there is no problem with all that … but I don’t know how to connect custom taxonomies to users.

Is it possible to list all categories on the user profile page (checkboxes) and save theme in the usermeta table?
Or do you mean it is better to put all the subscriptions into a new table and use a third pivot table to connect taxonomies and users?

Read More

Or another solution? Any ideas are appreciated 🙂 Many thanks!

Related posts

2 comments

  1. I came across a tutorial about ‘Custom User Taxonomies in WordPress‘ and there is a plugin based on that ‘User Taxonomies‘ but these are for creating taxonomies for Users. Have a read thought the tutorial it might help.

    I think what you’re talking about is to associating posts’ taxonomies with users. I think you need to use something like wp_set_object_terms (Codex) to create a relationship between the user and taxonomy term, so you’ll have something like:

    wp_set_object_terms( $object_id, $terms, $taxonomy, $append )
    
    • $object_id – will be your user_id
    • $terms – corresponding terms
    • $taxonomy – corresponding taxonomy
    • $append – false. Every time a user saves her/his choice of taxonomy terms to subscribe for, you’d need to go through them and add them again (in case something has been un-ticked that was ticked before).

    You would need to make sure to clean up the DB whenever a user/taxonomy/taxonomy term is deleted, so you should clean up the relation of object and taxonomy terms you created previously. Use wp_delete_object_term_relationships (Codex).

    I hope that points you in the right direction.

  2. I was/am using the user taxonomy snippet as well.

    However, I highly recommend not to mix users and posts in one taxonomy. Meaning registering a taxonomy for users AND posts.

    The wp_term_relationships table just stores an object_id. Once there’s a post and a user with the same ID, everything gets messed up because then, the term is assigned to both, user and post with that ID.

    My solution was to register two taxonomies, one for each type (post,user), and to keep the term pool synchronous by programmatically add/delete/alter terms in both taxonomies. (Using these hooks: created_term, pre_delete_term, edited_term)

Comments are closed.