custom post types - WP_Query not resetting after wp_reset_postdata

admin2025-06-04  11

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.

Share Improve this question asked Jan 9, 2019 at 15:39 Steve RodgersSteve Rodgers 156 bronze badges 4
  • Who is your host? some managed WP hosts (ie. WPEngine) disable rand by default – mrben522 Commented Jan 9, 2019 at 15:41
  • I see, and my host is ofc WPEngine... Do you know a workaround? Or is there a better way to get rand? @mrben522 – Steve Rodgers Commented Jan 9, 2019 at 15:45
  • Do you (or your hosting company) use any cache on that site? – Krzysiek Dróżdż Commented Jan 9, 2019 at 16:14
  • contact WPEngine support via their live chat. They'll turn it back on if you ask nicely – mrben522 Commented Jan 9, 2019 at 19:02
Add a comment  | 

3 Answers 3

Reset to default 1

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749026201a315732.html

最新回复(0)