Compare custom taxonomies of updated post (or new post) [Updated with progress]

I am using the Transients API to cache category loops for a custom taxonomy. All works fine, but if I add a new post or update an existing one, I woud like to compare the taxonomy values and then clear the transients based on the difference in IDs and then clear the others. Transients have the following format, _transient_mouldings_cat_loop_141 where 141 is the ID.

Does anyone have any suggestions on grabbing the tax values before the post is saved (existing terms) (which I thought would be category_save_pre(), but that only references post categories, and then on-save, use save_post() to grab the new values, find the difference and then delete the needed transients?

Read More

Thanks!

Update

I was able to delete category transients with save_post():

function mouldings_delete_transient_teaser($post_id) {
    switch (get_post_type()) {
        case 'moulding_profiles':
            // delete teaser transient
            delete_transient('mouldings_moulding_profiles_teaser_'.$post_id);

            // delete profile category transient
            $post_terms = get_the_terms($post_id, 'profile_categories');
            foreach ($post_terms as $post_term) {
                delete_transient('mouldings_cat_loop_'.$post_term->term_id);
            }
            break;
    }

}
add_action('save_post','mouldings_delete_transient_teaser');

but I still need to be able to plug into the initial load. I was thinking load_(page) might do the trick, an I could possibly create a function in there that grabs the posts before an item is saved, but then calling that function in save_post() would probably just give me the new categories (which I already have). Any suggestions are welcome

Thoughts

I’m wondering if this is more along the lines of storing the category IDs in a session variable – then on save_post() compare those with get_terms() (which will have the new ones) and then the unique IDs would be run through the delete_transient() function — as I have still yet to come across anything…

Thoughts continued:

It sounds like sessions are not used in WordPress and while you can enable them via wp-config.php, I’m wondering if there is a simpler way that I’m missing.

Update #2

After more than a few back ‘n forths with Tom, I believe this is the correct way to go:

function mouldings_delete_transients ($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {

    if (get_post_type() == 'moulding_profiles' || get_post_type() == 'moulding_combination') {
        $changed_cats = array_diff($tt_ids, $old_tt_ids);
        foreach ($changed_cats as $changed_cat) {
            delete_transient('mouldings_cat_loop_'.$changed_cat);
        }
    }
}
add_action('set_object_terms', 'mouldings_delete_transients', 10, 6);

The only problem I’m having with this is $old_tt_ids are not being called when a post is saved. So, what I’m trying to do is compare the current categories, $tt_ids with the old categories, $old_tt_ids and detect the changes between the two. So, if a new category has been chosen, that would be part of the diff. And, if a category was removed (unchecked), that would also be chosen. It seems to grab the new ones, but not the old ones. Anyone have any ideas? Thanks!

Related posts

Leave a Reply

1 comment

  1. Use the set_object_terms hook,

    http://adambrown.info/p/wp_hooks/hook/set_object_terms?version=3.4&file=wp-includes/taxonomy.php

    It should be fired when a post or page is modified.

    Here is the code that fires it:

    do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids);
    

    You can thus check against the $object_id and taxonomy, and flush your transient cache and regenerate as needed.

    Specifically, clear the term caches of the term IDs in $tt_ids and $old_tt_ids