Custom Post Random WordPress

Hello I have a problem when displaying posts randomly .
The posts are listed almost correctly , I’m doing a condition meta_quey to catch 2 data from the custom post and is really sure pulling content , but the orderby the rand attribute is only changing the first 2 posts of position and I would like to make random all posts does anyone have a solution for this?

Thanks for Help!

$args2 = array (
'post_type' => 'garotas',
'meta_query' => array(
                 'relation' => 'AND',
                 array(
                   'key' => 'cidade',
                   'value' => 'SL',
                   // 'compare' => 'LIKE'
                 ),
                 array(
                   'key' => 'garotastop',
                   'value' => 1,
                   // 'compare' => 'LIKE'
                 )
             ),
'posts_per_page' => -1,
'orderby' => 'rand',
'order'    => 'ASC',
);

$event_query2 = new WP_Query( $args2 );


// The Query
$the_query2 = new WP_Query( $args2 );

// The Loop
if ( $the_query2->have_posts() ) {


  while ( $the_query2->have_posts() ) {
$the_query2->the_post();

$title = get_the_title();
$separator = "-";
$title2 = str_replace(" ", $separator, $title);

$width = 300;
$height = 700;


echo '<li class="col-md-4"><h1 class="title5 conteudoTitulo cor1 title-list"><a href="http://garotasjp.com.br/garotas/'.$title2.'">' . get_the_title() . '</a></h1>';

echo "<div class='thumbnail3'>";

$garotastop1 = get_post_meta( $post->ID,'garotastop1', true );

$imageUrl = wp_get_attachment_image_src($garotastop1, full);
echo '<a href="http://garotasjp.com.br/garotas/'.tirarAcentos($title2).'"><img class="img-responsive" width="'.$width.'" height="'.$height.'" src="'.$imageUrl[0].'"/></a>';

echo "</div>";
echo "</li>";

}


} 
/* Restore original Post Data */
wp_reset_postdata();


?>

Related posts

1 comment

  1. Try removing ( 'order' => 'ASC' ) from arguments array $args2, because this argument will reorder posts in ASCending.

    Simply ( 'orderby' => 'rand' ) will do the job.

    $args2 = array (
        'post_type'  => 'garotas',
        'meta_query' => array(
                             'relation' => 'AND',
                        array(
                             'key' => 'cidade',
                             'value' => 'SL',
                          // 'compare' => 'LIKE'
                        ),
                        array(
                             'key' => 'garotastop',
                             'value' => 1,
                          // 'compare' => 'LIKE'
                        )
                   ),
    'posts_per_page' => -1,
    'orderby' => 'rand',
    
    );
    

    I hope it works for you, thanks !

Comments are closed.