Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Or another solution? Any ideas are appreciated :-) Many thanks!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Agree this is the best way. Note the second argument to register_taxonomy can be an array, so you can put post and user together and they will both refer to an identical taxonomy. Also note that this will cause your Post Count column on the tax page to be inaccurate, though you could quite easily fix it by adapting the tutorial code. –  djb Feb 26 at 21:35
    
That is exactly what I was looking for! Many thanks. I just had a brief look on the code and it will help definitely. –  suntrop Feb 27 at 18:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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