I have a custom post type called 'seasons' and in there i have 3 categories (summer,winter and spring.)

I want to show 10 random post from the winter category on a page.

Please help.


WP_Query is the solution to your problem.

Go through the available parameters. Just add a query and a loop in your page template and you are good to go.


$args = array(
'category_name' => 'winter',
'orderby' => 'rand',
'posts_per_page' => 10

$query = new WP_Query( $args );

The loop starts here:

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

and ends here:

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

You need to add the custom post type in the $args array in the code above by Rohit:

$args = array(
    'orderby' => 'rand',
    'posts_per_page' => 10,
    'post_type' => 'seasons', //ADD THIS
    'category_name' => 'winter'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.