2

I am new with WordPress plugin development. I have to Synchronize custom post type tags with WordPress default post type tags. So when user will create, delete or update any custom post type tag it should be updated in Default tags. I do not have enough knowledge with WordPress hooks so if someone guide me about this that will be appreciated. This is urgent task for me to do so i have to find out the solution for this. Thank you!

2

You need to register the post_tag taxonomy for the post type.

For a quick, after the fact, way to do this (meaning the CPT has already been registered, etc.), you can use register_taxonomy_for_object_type().

A workable example is below. Edit the value for $object_type, and place the example below in the functions.php of your child theme:

  add_action( 'init', 'my_cpt_has_tags_now' );
  function my_cpt_has_tags_now() {
       $taxonomy    = "post_tag";
       $object_type = "name_of_your_cpt";

       register_taxonomy_for_object_type( $taxonomy, $object_type );
  }  
| improve this answer | |
  • I really appreciate your answer. But i am asking some thing different. I have already tags for my my custom post type but now i have to synchronize custom post types taxonomy (tags) with WordPress default Posts tags. So now if i will create update or delete custom post type tags then these should be updated in default posts tags. If you will guide me about this i will really appreciate it because it is important and urgent task me. Thank you!! – wplearner May 29 '17 at 22:53
  • ok, so you have a cpt taxonomy named tags and you want it to be synchronized with the wp taxonomy post_tag. Can you explain what you mean by synchronized? If they are the same terms in both, why not have them use one shared taxonomy? In any event there is a do_action() hook used by wp_insert_term() you may be able to use to do what you want. developer.wordpress.org/reference/hooks/create_taxonomy – hwl May 29 '17 at 23:08
  • I appreciate your suggestion but i have two separate taxonomies. I could not share one taxonomy because both these are have different uses. This is my client requirement and he want that when he will create any tag for custom post type it should automatically added in default wp tags. Same should happens on update and on delete. – wplearner May 30 '17 at 0:38
  • ah, ok. perhaps doing an add_action hooked to create_{$taxonomy} with a callback function that uses wp_insert_term() to add the term being created to the post_tag tax after you've checked it's a relevant scenario. developer.wordpress.org/reference/functions/wp_insert_term – hwl May 30 '17 at 13:59
  • several other update/delete hooks listed here: developer.wordpress.org/reference/files/wp-includes/… – hwl May 30 '17 at 14:03

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.