Support » Themes and Templates » Get random post from specific category

  • Resolved MaddTechWF


    I have a specific category, lets call it ‘testimonials’. I came across this link. After reading it, I’m still a little confused.

    I want to pull a random post from ‘testimonials’ and display an excerpt of it. I would also like to show a Custom Fields from the post that has the individuals name and lets call it ‘WritersName’. How to do I reference it?

Viewing 1 replies (of 1 total)
  • Assuming testimonials is category 14

    //get one random post from category 14, display post title, and excerpt.
    //display WritersName custom field if it exists.
      'cat' => 14,
      'orderby' => rand,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        $name = get_post_meta($my_query->post->ID, 'WritersName', true);
        if ($name){
          echo 'Writers name: ' .$name;
    wp_reset_query();  // Restore global post data stomped by the_post().
Viewing 1 replies (of 1 total)
  • The topic ‘Get random post from specific category’ is closed to new replies.