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