Can anyone spot anything wrong with this code(see below). No content is showing up on my page. Many thanks in advance.

    $args = array(
    'posts_per_page' => 3,
    'orderby' => 'rand',
    'category_name' => 'testimonials'

    $query = new WP_Query( $args );
        // The Loop
        if ( $query->have_posts() ) {
            echo '<ul class="testimonials">';
        while ( $query->have_posts() ) {
            echo '<li class="clear">';
            echo '<figure class="testimonial-thumb">';
            echo '</figure>';
            echo '<aside class="testimonial-text">';
            echo '<h3 class="testimonial-name">' . get_the_title() . '</h3>';
            echo '<div class="testimonial-excerpt">';
            echo '</div>';
            echo '</aside>';
            echo '</li>';
        echo '</ul>';

2 Answers 2


There aren't any errors in your code. I'm able to copy / paste it into functions.php and return 3 posts categorized under "testimonials" without issue.

By default this WP_Query will retrieve only 'post' matches so if you are using pages or a custom post type you'll need to specify that in your query args as:

$args['post_type'] = 'page';


$args['post_type'] = 'custom_post_type_name';

The only other thing to check is to ensure you've added posts to the "testimonials" category in the WordPress dashboard.

I hope this helps.


I have done the same with get_posts() method. Here is the code i used to get posts on my page. I was filtering my Posts according to their categories, so i gave the category slug in category_name, you can leave it blank if you want all your posts to be shown.

<?php $args = array(
    'posts_per_page'   => 5,
    'offset'           => 0,
    'category'         => '',
    'category_name'    => '{category slug}',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'author'       => '',
    'post_status'      => 'publish',
    'suppress_filters' => true 
$lastposts = get_posts( $args );
        foreach($lastposts as $post) :
        setup_postdata($post); ?>

        <li<?php if ( $post->ID == $wp_query->post->ID )
            echo ' class="current"'; 

         } else {} ?>>

            <a href="<?php the_permalink() ?>"><?php 



    <?php endforeach; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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