I have this custom query:
if (isset($_COOKIE['myCookie']) && $_COOKIE['myCookie'] == $f_r) {
$args = array(
'posts_per_page' => 4,
'nopaging' => false,
'order' => 'ASC',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
The query works, but it does not reset the data. Every time a visitor lands on the page where the query runs, it shows the exact same 4 posts as it always does.
This code run just fine in my local environment (fetching different posts each time it runs), but on the live server it seems to be "fixed" to 4 specific posts.
I don't understand why this is happening, so I could really use some advice here. Thanks!
Ps. It's not the plugins or theme, because it works fine in a local environment.
I have this custom query:
if (isset($_COOKIE['myCookie']) && $_COOKIE['myCookie'] == $f_r) {
$args = array(
'posts_per_page' => 4,
'nopaging' => false,
'order' => 'ASC',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
The query works, but it does not reset the data. Every time a visitor lands on the page where the query runs, it shows the exact same 4 posts as it always does.
This code run just fine in my local environment (fetching different posts each time it runs), but on the live server it seems to be "fixed" to 4 specific posts.
I don't understand why this is happening, so I could really use some advice here. Thanks!
Ps. It's not the plugins or theme, because it works fine in a local environment.
If order by RAND is disabled at server level, you can try doing the rand by hand by running the query and get the ids first, then running a new query from the result including x random post ids:
$get_all_args = array(
'fields' => 'ids',
'posts_per_page' => 99,
'order' => 'DESC',
'orderby' => 'modified',
'tax_query' => array(
array(
'taxonomy' => 'Count',
'field' => 'slug',
'terms' => $f_r,
)
)
);
$query_all = new WP_Query( $get_all_args );
// additional code and checks
// ...
// ...
wp_reset_postdata();
$args = array(
'post__in ' => array_rand(array_flip( $query_all ), 4)
);
$query = new WP_Query( $get_all_args );
// additional code and checks
// ...
// ...
wp_reset_postdata();
You will get 4 random posts from the latest modified posts, notice I'm not using 'posts_per_page'=> -1
since this is not a good practice and can harm your site speed
you have missing a } after while, if you check
while ( $query->have_posts() ) {
} else {
Please try with
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<a href="<?php the_field('the_link'); ?> ">
<img src="<?php echo the_field('the_img'); ?>" />
</a>
}
} else {
echo 'Oops! Something went wrong!';
}
wp_reset_postdata();
wp_reset_query();
try to use
wp_reset_query();
wp_reset_postdata();
As you can see here: documentation with wp_reset_query and wp_reset_postdata reset Query and Original Post Data