$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'); ?>custom post types - WP_Query not resetting after wp_reset_postdata|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)

custom post types - WP_Query not resetting after wp_reset_postdata

matteradmin10PV0评论

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

Post a comment

comment list (0)

  1. No comments so far