0

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.

  • Who is your host? some managed WP hosts (ie. WPEngine) disable rand by default – mrben522 Jan 9 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 Jan 9 at 15:45
  • Do you (or your hosting company) use any cache on that site? – Krzysiek Dróżdż Jan 9 at 16:14
  • contact WPEngine support via their live chat. They'll turn it back on if you ask nicely – mrben522 Jan 9 at 19:02
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

0

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();
0

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

  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Jan 9 at 19:42
  • OK, why? Why do you want to reset both query and postdata? Why do you want to reset query at all, if OP is using custom query in his code...? – Krzysiek Dróżdż Jan 12 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.