1

I am setting up some transients to reduce load with multiple custom WP_Query objects.

When I set the transient to expire within an hour for example, any change to a post / custom post is instantly reflected in the results within the loop utilizing the transient. According to my understanding the query should have been cached and the updated post values should only reflect after the specified time?

The transient is not set again, and works as expected in terms of the expiration time, it's just that the post changes are immediately available?

Or is my understanding of transients simply wrong?

A basic example below:

if ( false === ( $testimonial_loop = get_transient( 'testimonials' ) ) ) {

        $args = array(
            'post_type'     => 'testimonial',   
        );

        $testimonial_loop = new WP_Query( $args );        

    set_transient( 'testimonials', $testimonial_loop, 5 * MINUTE_IN_SECONDS );
}
0

rule of thumb is probably to use transients for simple objects (strings integers, array) as the result of unserialization of objects might be surprising and side effect might happen unless the object is fully static.

IIRC, when unserializing an object PHP will also run its default constructor, which in your case will do the query again as unserialization is being done when it is retrieved from the DB.

  • Thank you for the answer and I understand, but why would the Codex list WP_Query as a specific example if the query is simply run again? Would there be any benefit to caching a query such as this as numerous examples use cached WP queries specifically? – Svartbaard Feb 20 '17 at 11:23
  • I am obviously guessing, and if you can provide link to the codex I will look again, but generally php object serialization and unserialization is source for surprises (and I only understand the concept of the "why" not all the details) – Mark Kaplun Feb 20 '17 at 11:39
  • Ok cool, please see the last example below https://codex.wordpress.org/Transients_API – Svartbaard Feb 20 '17 at 11:49
  • OK, so now you also know that the codex examples can not be trusted ;). Frankly I don't see the point of caching a query, as posts might not exist anymore when you process it (assuming that code did work as expected...) it makes much more sense to compute the HTML and cache it. – Mark Kaplun Feb 20 '17 at 13:53

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.