1

I'm trying to set a transient on my custom query, but it does not seem to work. Every time I refresh the page it still show me different posts each time.

After reading up on the transients api, I came to believe that setting a transient would store my first query with the exact same posts it queried and display them again next time without doing the whole query again.

Am I wrong or what am I doing wrong?

Here's my complete UPDATED query:

if(isset($_COOKIE['myCookie']) && $_COOKIE['myCookie'] == $f_a){

// Check for transient. If none, then execute WP_Query
if ( false === ( $akerargs = get_transient( 'county_query' ) ) ) {

    $akerargs = array(
    'posts_per_page' => 4,
    'nopaging' => false,
    'order'    => 'ASC',
    'orderby'  => 'rand',
    'tax_query' => array(
        array(
            'taxonomy' => 'Count',
            'field' => 'slug',
            'terms' => $f_a,
        )
    )

);

// Put the results in a transient. Expire after 10 minutes.
set_transient( 'county_query', $akerargs, 10 * MINUTE_IN_SECONDS );

}

ob_start();?>

<div class="container-fluid">

<?php

$akerquery = new WP_Query( $akerargs );

?>

<div class="row">

<?php if ( $akerquery->have_posts() ) {

while ( $akerquery->have_posts() ) {
    $akerquery->the_post();
?>  

   <div class="col-6 mt-4">  

        <a href="<?php the_field('county_link'); ?> ">
                <img src="<?php echo the_field('county_img'); ?>" />
       </a>  

    </div> 

<?php

}

$akerquery->wp_reset_postdata();

} else {

echo 'Oops! Something went wrong.';

}

?>

</div>
</div>

<?php

return ob_get_clean();

UPDATE

I've tried multiple possible solutions to this now and it still does not work. According to the WordPress codex this is how it could be accomplished:

// Check for transient. If none, then execute WP_Query
if ( false === ( $featured = get_transient( 'foo_featured_posts' ) ) ) {

      $featured = new WP_Query(
       array(
        'category' => 'featured',
        'posts_per_page' => 5
       ));

    // Put the results in a transient. Expire after 12 hours.
    set_transient( 'foo_featured_posts', $featured, 12 * HOUR_IN_SECONDS );
} ?>

// Run the loop as normal

And this is what I've tried now:

if(isset($_COOKIE['myCookie']) && $_COOKIE['myCookie'] == $f_a){

    // Check for transient. If none, then execute WP_Query
if ( false === ( $akerargs = get_transient( 'county_query' ) ) ) {


    $akerargs = array(
    'posts_per_page' => 4,
    'nopaging' => false,
    'order'    => 'ASC',
    'orderby'  => 'rand',
    'tax_query' => array(
        array(
            'taxonomy' => 'Count',
            'field' => 'slug',
            'terms' => $f_a,
        )
    )
);


$akerquery = new WP_Query( $akerargs );

    // Put the results in a transient. Expire after 10 minutes.
set_transient( 'county_query', $akerargs, 10 * MINUTE_IN_SECONDS );
}
// Run the loop as normal

But it still runs the query and fetches new posts instead of saving the 4 posts that gets queried at first in the transient.

  • Hey Cap'n. try setting some indents, and show us your whole code and I think you'll see quickly what krzysiek is saying. – rudtek Jan 9 at 17:58
  • 1
    Instead of setting your transient to be the actual query make your transient the results of the query – rudtek Jan 9 at 17:59
  • @rudtek I updated the question now, with the entire code for the query and output. – Steve Rodgers Jan 9 at 18:04
  • @rudtek, so basically what you are saying is that this if ( false === ( $akerargs = get_transient( 'akershus_query' ) ) ) { $akerquery = new WP_Query( $akerargs ); set_transient( 'county_query', $akerargs, 10 * MINUTE_IN_SECONDS ); } is enough? – Steve Rodgers Jan 9 at 18:13
1

And that exactly how it will work, because that’s what you coded ;)

You set the transient correctly, but...

You put only query arguments in there. And that doesn’t make much sense - it’s just an PHP array that is very easy and quick to create.

And the query itself is executed always - it’s outside the if. So yes - the results of the query are obtained on every request. And because you use rand as orderly, then you get random posts on every request.

If you want to use transients to cache results of query and display the same posts for 10 minutes (or other period), then you’ll have to keep these results in the transient.

  • Not sure I understand your answer completely. You are saying that the transient I set is basically no good? What would i need to change to get it to work as intended? Which is to store the query for later use. – Steve Rodgers Jan 9 at 17:50
  • @SteveRodgers take a look at your code. What are you storing in transient? An array. And when are you querying posts? Always ;) So yes - you use transients and your code works exactly how it is written. You should store results of that query, not params, if you want it to work how you described. – Krzysiek Dróżdż Jan 9 at 17:53
  • Okay, I see. Will it work if I wrap the transient around $akerquery = new WP_Query( $akerargs ); also, or do I have to wrap the entire loop and html output in the transient? – Steve Rodgers Jan 9 at 17:56
  • @SteveRodgers you have to wrap it all, but you also have to store correct data in transient... But I’m starting to repeat myself, I guess. I’ve explained it pretty clear in my answer. – Krzysiek Dróżdż Jan 9 at 17:58
  • 1
    My mistake, changed from set_transient( 'county_query', $akerargs, 10 * MINUTE_IN_SECONDS ); to set_transient( 'county_query', $akerquery, 10 * MINUTE_IN_SECONDS ); now, and guess what? It works. As you already knew it would! Thank you for not throwing the answer in my face. I guess i understand it better now:) – Steve Rodgers Jan 9 at 19:31

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.