自定义张贴随机Wordpress


Custom Post Random Wordpress

你好,我在随机显示帖子时遇到问题。帖子列得几乎正确,我正在做一个条件meta_quey来从自定义帖子中捕获2个数据,并且确实可以提取内容,但rand属性的orderby只更改了位置的前2个帖子,我想随机生成所有帖子。有人对此有解决方案吗?

感谢您的帮助!

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

?>

尝试从参数数组$args2中删除('order' => 'ASC'),因为此参数将重新排列ASC结尾中的帖子

简单地('orderby' => 'rand')就能完成任务。

$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',
);

我希望它对你有效,谢谢