I've got this inside my functions.php file.

    function add_product_category_automatically($post_ID) {
global $wpdb;
    $cat = get_the_title($post_ID);
    wp_set_object_terms($post_ID, $cat, 'product_relation');
add_action('publish_product', 'add_product_category_automatically');

It creates terms in the 'product_relation' taxonomy when a product is published. Is it possible to also have the terms automatically deleted when the product is moved to the trash?


  • There's only one custom taxonomy, called 'product_relation', which is registered somewhere else using register_taxonomy(). What this code is creating is custom terms in that taxonomy.
    – scribu
    Commented Dec 2, 2011 at 15:47

2 Answers 2


There was some confusion in the original question with the terminology i used. I didn't find a way to do what i was after but it's to late for me to do it now.


Instead of deleting the taxonomy, wouldn't you rather simply hide it from queries (if empty)?

Whenever you run a query with get_terms, the 'hide_empty' argument is, by default, set to true. That way, let's say you have a page where you list/display the taxonomy's products; whenever the user removes or simply does not have any products within that taxonomy, the taxonomy does not appear or echoes a 'Sorry, there are no available products.' message.

EDIT: In case you would like to have the argument visible, this is how it should look like:

<?php get_terms('product_relation', array('hide_empty'=>1)); ?>
  • Thanks, but perhaps i should have explain more. What i want is, there's a custom taxonomy created for every product, so i can relate them to each other. However when a product is deleted, the custom taxonomy still shows in the admin. Over time if a lot of products are deleted the list of custom taxonomies that can be selected will keep growing. The taxonomy is only used to related products, if a product is deleted the taxonomy shouldn't appear any more.
    – Corbula
    Commented Dec 2, 2011 at 15:41
  • Please update your answer, using the correct terminology (see edited question). Thanks.
    – scribu
    Commented Dec 2, 2011 at 15:51
  • @Corbula Oh sorry, I actually thought there was a taxonomy holding all of your products. Then you might want to look into this core.trac.wordpress.org/ticket/12629 - and hook that function to your code.
    – cr0z3r
    Commented Dec 2, 2011 at 16:01
  • @cr0z3r thanks but that seems to be removing custom taxonomies. See the updated question as edited by scribu. I want the terms removed not the taxonomy.
    – Corbula
    Commented Dec 2, 2011 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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