I am using this hook --
add_action( 'delete_term_taxonomy', array( $this, 'before_product_category_deleted' ) );
-- to do something before a product category is deleted.

In that function, I am trying to get all the products in that category right before its deletion, using the following code:

$args = array(
    'fields' => 'ids',
    'posts_per_page' => -1,
    'post_type' => 'product',
    'tax_query' => array(
            'terms' => $category_ids,
            'taxonomy' => 'product_cat',
            'operator' => 'IN',

$results = new WP_Query( $args );

Unfortunately, I am not getting anything. It appears that by the time the delete_term_taxonomy action fires, the category has already been removed from the related products.

How can I get the products in that category before the category is deleted?

1 Answer 1


The hook delete_term_taxonomy fires before the category is deleted. If you look at the original code:

 * Fires immediately before a term taxonomy ID is deleted.
 * @since 2.9.0
 * @param int $tt_id Term taxonomy ID.
do_action( 'delete_term_taxonomy', $tt_id );
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );

 * Fires immediately after a term taxonomy ID is deleted.
 * @since 2.9.0
 * @param int $tt_id Term taxonomy ID.
do_action( 'deleted_term_taxonomy', $tt_id );

You are using the right hook for the intended purpose. Either your query has a problem or your hook is not being triggered at all. Are you getting some sort of answer like Array(0) ?

You should first try to see if you can manage to trigger your hook when deleting a category. I use the logging functions to test hooks and other functions that are difficult to trace such as Crons or forms submissions etc. It helps making sure something happens and logging some events and outputs.

You have a useful tutorial here on how to achieve this

If you managed to log anything into your log file using that trigger it means that you should check the query. That means the hook triggered.

However, looking at your code, I can see that you are using a class for this. It makes me believe that the issue comes from the instantiation of your class, when are you calling this class? Is your hook 'add_action' function placed in the constructor ?

To avoid having problems with hooks in OOP plugins, I advise using something like Devin Vinson's loader This has saved me so many headaches and using this sort of loading class helps getting the hooks registered at the very beginning and handling hooks in one single place.

If however you actually managed to have an evidence that your hook is working, then you should checkout the query.

Is $category_ids an array such as $category_ids = array('1', '2', '3'); ?

Have you tried running that query somewhere else than within your hook ? You could try running that query somewhere else safe and make sure you are getting the results you expect.

  • Hi, thank you for your response. You have put a lot of effort into it. However, I have already found the answer, and there is nothing wrong with my code. It is the hook delete_term_relationships, which runs even before delete_term_taxonomy. The product category is removed from related products at this stage. Therefore, by the time we arrive at delete_term_taxonomy, there are no more products in the affected product category. Thanks again. :)
    – l_nxy
    Sep 13, 2017 at 15:03
  • Oh well, I tried to give you my best shot at it :) not bad I would have never thought term_relationships are separate from term_taxonomy but good to know
    – db306
    Sep 14, 2017 at 7:31

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.