Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a transient set for a custom query. It expires in 30 days. But I also need it to expire once a new post/page is published. So that the new published post/page is available in that custom query. How to delete a transient on post/page publish?

How I set the transient:

// Get any existing copy of our transient data
   if ( false === ( $query = get_transient('d_results') ) ) {
    // It wasn't there, so regenerate the data and save the transient
    $randargs = array("post_type"=>"", "orderby"=>"", "order"=>"", "posts_per_page"=>-1);
    $query = new WP_Query($randargs);
    set_transient( 'd_results', $query, DAY_IN_SECONDS * 30);
   }
share|improve this question
    
can u also add the codes which contain how u r setting the transient? –  Maruti Mohanty May 14 at 18:23
    
Updated the post with how the code of how I set the transient. I removed the argument's values from the query, but besides that everything is the same. –  gdaniel May 14 at 18:27
    
can you add some details on why would you do this, I mean why are u adding this transient at the first place? –  Maruti Mohanty May 14 at 18:30
    
The query returns over 600 posts. These posts are rarely updated, but everynow and then there's a new post before the 30 day expiration. –  gdaniel May 14 at 18:36

1 Answer 1

up vote 0 down vote accepted

I am considering it for publication of a new post.

Add the below code in your active theme's functions.php file.

function wpse_delete_query_transient( $post ) {
    // Deletes the transient when a new post is published
    delete_transient( 'd_results' );
}
add_action( 'new_to_publish', 'wpse_delete_query_transient' );

This will delete the transient every time a new post is published.

if you want to delete the transients on differrent post status transitions, you may like to look into the codex

share|improve this answer
    
Thanks @Maruti Mohanty. I was wondering based on your questions and my answers, if this is not a good solution for my problem? Would you not use transients? –  gdaniel May 14 at 18:50
    
I guess you have used transient very smartly and efficiently. I asked the questions to know why were u adding a query result to a transient but now i get your point :) –  Maruti Mohanty May 14 at 18:54
1  
Thanks. I was not aware of Posts Status Transitions. I will be doing some good reading. –  gdaniel May 14 at 18:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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