$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Set Transient does nothing|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Set Transient does nothing

matteradmin9PV0评论

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.

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.

Share Improve this question edited Jan 9, 2019 at 19:06 Steve Rodgers asked Jan 9, 2019 at 17:38 Steve RodgersSteve Rodgers 156 bronze badges 4
  • 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 Commented Jan 9, 2019 at 17:58
  • 1 Instead of setting your transient to be the actual query make your transient the results of the query – rudtek Commented Jan 9, 2019 at 17:59
  • @rudtek I updated the question now, with the entire code for the query and output. – Steve Rodgers Commented Jan 9, 2019 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 Commented Jan 9, 2019 at 18:13
Add a comment  | 

1 Answer 1

Reset to default 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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far