0

I have this code that picks a random testimonial post.

    <?php
        $quote_args = array(
            'post_type' => 'post',
            'category' => 'quote',
            'orderby' => 'rand',
            'posts_per_page' => 1
        );

        $quote  = new WP_Query($quote_args);

        if($quote->have_posts()):
            while($quote->have_posts()):
                $quote->the_post();
    ?>

    <p id="quote">
        <?php the_content_rss(); ?>
        <span><?php echo the_title(); ?></span>
    </p>

        <?php
            endwhile;
            endif;

            wp_reset_postdata();
        ?>

The testimonials are posts with a category of 'quote', the title of the post is the name of the person.

I have other posts in the site that have other categories but only the testimonial posts have a category of 'quote'.

The code works but I'm getting a random post picked from all the posts and not just the post's with a quote category.

Can anyone see a reason why I getting this, or any other reason this is happening

2

There's no such parameter as category for WP_Query, so WP is ignoring it. You should use category_name, where the value is the slug of the category.

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.