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
Add a comment  | 

1 Answer 1

Reset to default 0

Add 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