Using `set_transient()` when saving a custom post type

I've got multiple custom post types registered via a plugin and one of them is used as a precursor for multiple other queries. In an attempt to reduce queries throughout the site, I've attempted to include the one CPT, we'll call it post_type_x and some of it's post_meta data in a 'transient record'.

Everything is working exactly as I want it to - the data I'm placeing in a transient record is available for me when I need it, whenever I need it, etc and I've managed to reduce an entire query for some of the more complex pages on the site.

However, I'm wondering about a very 'edge' case:

When I am creating a new post_type_x post the transient record is already set and if the new post_type_x fits the requirements of the original check that determines which post_type_x posts to add via set_transient() it won't get added for users currently browsing. I'll probably set the transients to expire and reset after 12 or 24 hours.

This in itself isn't actually a problem as any newly added post_type_x won't need to be included for a typical user of the site as soon it's created. These CPT posts will usually be created weeks or even months before standard users needed to access them and by that time they'll have visited the site multiple times and the transient record will have been repeatedly updated. Basically by the time a typical user needs it, it'll be available to them.

However, the one segment of people that may need to see new post_type_x posts loaded into the transient record as soon as it has been created would be administrators, because they're setting up programs and have a lot of related details across other CPTs and custom tables to work through. The data in the transient record would, ideally, be updated as soon as new CPT post is created or an existing one is updated.

So, what I'm wondering... ...is there a reliable hook/action within WP I can use that runs immediately AFTER the add_action( 'save_post_post_type_x', 'post_type_x_save_function', 1, 2 );

That way I can be sure that the admin user is getting a the transient record set as soon as they've implemented a change to one of the post_type_x posts.

I've modified my question above to use transient records instead of wp_cache. After doing some additional codex reading and taking Tom J Nowell's comments into consideration, I believe that setting a transient record is the best option.

Topic transient actions hooks custom-post-types Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.