admin管理员组文章数量:1278854
I have custom post type and I just want to show single post data random, here is the basic code that I have used.
$args = array(
'post_type' => 'nd_rst_cpt_1', //custom post type name
'orderby' => 'rand', //random order
'posts_per_page' => 1, //shows only single post
);
$resData = new WP_Query($args);
if ($resData->have_posts()) :
while ($resData->have_posts()) :
the_title(); //display title
endwhile;
else:
echo '<div class="no_restaurants">' . NO_RES_MSG . '</div>'; //if no post found
endif;
This is a basic sturucture of my code but it always shows recent post name instead of random.
I have custom post type and I just want to show single post data random, here is the basic code that I have used.
$args = array(
'post_type' => 'nd_rst_cpt_1', //custom post type name
'orderby' => 'rand', //random order
'posts_per_page' => 1, //shows only single post
);
$resData = new WP_Query($args);
if ($resData->have_posts()) :
while ($resData->have_posts()) :
the_title(); //display title
endwhile;
else:
echo '<div class="no_restaurants">' . NO_RES_MSG . '</div>'; //if no post found
endif;
This is a basic sturucture of my code but it always shows recent post name instead of random.
Share Improve this question asked Jan 5, 2021 at 15:58 Parth ShahParth Shah 111 bronze badge 2- Are you certain that there is more than 1 post of this CPT that is published? – kero Commented Jan 5, 2021 at 16:21
- Check this question: wordpress.stackexchange/a/31769/7968 – Q Studio Commented Jan 5, 2021 at 16:37
1 Answer
Reset to default 0Add order asc
$args = array(
'post_type' => 'nd_rst_cpt_1', //custom post type name
'orderby' => 'rand', //random order
'order' => 'ASC',
'posts_per_page' => 1, //shows only single post
);
本文标签: wp queryorderby rand is not working for a custom post types
版权声明:本文标题:wp query - orderby rand is not working for a custom post types 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741296505a2370850.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论