0

When deleting a tag, category or other term I would like to get the IDs of the posts that are related to the term before deletion. Wordpress uses wp_delete_term, which is found in taxonomy.php, to delete a term. In this function the earliest hook is "delete_term_taxonomy". However, it seems like the relationships are already deleted before this hook fires.

I would like to make this query:

SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = $id

This would normally return all the post ids related to a term (category/tag/etc). Is there any other hook I can use? Or maybe a hack?

0

In this function the earliest hook is "delete_term_taxonomy". However, it seems like the relationships are already deleted before this hook fires.

According to the source, 'delete_term_taxonomy' runs just before deletion and 'deleted_term_taxonomy' runs just after. There are other things going on before this code, like the child relationships are changed.

do_action( 'delete_term_taxonomy', $tt_id );
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
do_action( 'deleted_term_taxonomy', $tt_id );

Are you sure you were not using 'deleted_term_taxonomy' by mistake?

Is there any other hook I can use?

If the taxonomy is hierarchical, 'edit_term_taxonomies' and 'edited_term_taxonomies' run first:

do_action( 'edit_term_taxonomies', $edit_tt_ids );
$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
do_action( 'edited_term_taxonomies', $edit_tt_ids );
2
  • If the term was hierarchical, it would work. However, it should work in both cases. Wordpress is making the exact same query I want to do (on line 1805 in taxonomy.php) and then goes through them all in a foreach loop. If i try to make the query right after that loop, it doesn't work. So something is happening there. 'delete_term_taxonomy' runs before deletion of the term itself, but not the relationships it has. Those seem to be deleted before that.
    – John
    Aug 27 '13 at 15:20
  • Upon further digging, it seems like I can use the hook 'wp_get_object_terms'.
    – John
    Aug 27 '13 at 15:58

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.