patrick
patrick

Reputation: 657

Wordpress - Multiple Post Queries

I am using the following code to query posts for categories:

<?php query_posts("cat=8"); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
            <article>
                <h4><?php the_title(); ?> </h4>
                 <p><?php the_content(); ?></p>
             </article>
    <?php endwhile; ?>

It seems to work fine, until I did it a third time(three instances of the code above) on a single page. Now the page seems to load forever and it breaks as if it is compiling more then 1 page template. I should mention that all works fine unless I publish a post to the third category

Has anyone had a problem like this, or know why it happens? Is this bad practise for querying posts?

Upvotes: 0

Views: 1114

Answers (1)

randyttp
randyttp

Reputation: 98

Use WP_query instead so you can make use of the wp_reset_postdata which should clear up the issue.

<?php
$the_query = new WP_Query( 'cat=8' );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
    <article>
        <h4><?php the_title(); ?> </h4>
        <p><?php the_content(); ?></p>
    </article>
<?php
endwhile;
wp_reset_postdata();
?>

Upvotes: 2

Related Questions