How do you make word press custom post show randomly without having to be logged in? It doesn't change when I am not logged in

So I am looping through some custom post type called reviews on my Controller. Basically I am looping through a custom post type using ‘rand’ so every time I load it shows different reviews every time. Well it does work but ONLY when I am logged in into Wordpress, but when I act like any other user (logged out) it stays the same.

So far this is what I have in Controller:

 public function reviews()
{
    $args =[
        'post_per_page' => -1,
        'offset' => 0,
        'orderby' => 'rand',
        'order' => 'ASC',
        'posts_per_page' => '4',
        'post_type' => 'reviews'
    ];

$the_query = new \WP_Query($args);
$reviews = [];
if($the_query->post_count > 0)
{
    $reviews = array_map(function ($reviews){
        $post = get_post($reviews);
        $name = get_the_title($reviews);
        $content = $post -> post_content;
        return (object)[
            'name' => $name,
            'content' => $content
        ];

    }, $the_query->posts);

    wp_reset_postdata();
}
return $reviews;

}

Nothing about this code looks like it would cause the behavior you’re describing. It sounds like a server-side caching issue: Something is caching your pages/queries, but forces cache invalidation on ever load when you’re logged in. That, or there’s some other 3rd party thing at work here. A good first step would be to deactivate all plugins and see if this issue persists.

This doesn’t really seem like it’s related to the Roots stack, though. We try to keep these forums focused on our stack, instead of just general WordPress support.

This topic was automatically closed after 42 days. New replies are no longer allowed.