• Resolved Pendulum

    (@pendulum)


    I’m currently using query_posts to select posts with a specific tag to display. I have to do this a total of 6 times on one page, with a different tag being pulled each time.

    I know there’s got to be a better way to do this… I’m getting some page load delays from the code below. Any help is greatly appreciated!

    <?php query_posts('tag=home_1&category=1&posts_per_page=1'); ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    
    		<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'home-thumbnails' ); ?>
    	    	<header class="article-header">
    
    		    	<h1><?php the_title(); ?><img src="/wp-content/themes/az***/library/images/arrows.png" class="arrow" alt="Post Arrows" title="post arrows" /></a></h1>
    
    	    	</header><!-- end article header -->
    
       		 </article><!-- end article -->
    
    	<?php endwhile; ?>
    
    	<?php rewind_posts; ?>
    
    	<?php query_posts('tag=home_2&category=1&posts_per_page=1'); ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    
    		<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'home-thumbnails' ); ?>
    	    	<header class="article-header">
    
    		    	<h1><?php the_title(); ?><img src="/wp-content/themes/az***/library/images/arrows.png" class="arrow" alt="Post Arrows" title="post arrows" /></a></h1>
    
    	    	</header><!-- end article header -->
    
       		 </article><!-- end article -->
    
    	<?php endwhile; ?>
    
    	<?php rewind_posts; ?>
    
    	<?php query_posts('tag=home_3&category=1&posts_per_page=1'); ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    
    		<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'home-thumbnails' ); ?>
    	    	<header class="article-header">
    
    		    	<h1><?php the_title(); ?><img src="/wp-content/themes/az***/library/images/arrows.png" class="arrow" alt="Post Arrows" title="post arrows" /></a></h1>
    
    	    	</header><!-- end article header -->
    
       		 </article><!-- end article -->
    
    	<?php endwhile; ?>
    
    	<?php rewind_posts; ?>
    
    	<?php query_posts('tag=home_4&category=1&posts_per_page=1'); ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    
    		<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'home-thumbnails' ); ?>
    	    	<header class="article-header">
    
    		    	<h1><?php the_title(); ?><img src="/wp-content/themes/az***/library/images/arrows.png" class="arrow" alt="Post Arrows" title="post arrows" /></a></h1>
    
    	    	</header><!-- end article header -->
    
       		 </article><!-- end article -->
    
    	<?php endwhile; ?>
    
    	<?php rewind_posts; ?>
    
    	<?php query_posts('tag=home_5&category=1&posts_per_page=1'); ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    
    		<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'home-thumbnails' ); ?>
    	    	<header class="article-header">
    
    		    	<h1><?php the_title(); ?><img src="/wp-content/themes/az***/library/images/arrows.png" class="arrow" alt="Post Arrows" title="post arrows" /></a></h1>
    
    	    	</header><!-- end article header -->
    
       		 </article><!-- end article -->
    
    	<?php endwhile; ?>
    
    	<?php rewind_posts; ?>
    
    	<?php query_posts('tag=home_6&category=1&posts_per_page=1'); ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    
    		<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'home-thumbnails' ); ?>
    	    	<header class="article-header">
    
    		    	<h1><?php the_title(); ?><img src="/wp-content/themes/az***/library/images/arrows.png" class="arrow" alt="Post Arrows" title="post arrows" /></a></h1>
    
    	    	</header><!-- end article header -->
    
       		 </article><!-- end article -->
    
    	<?php endwhile; ?>
    
    	<?php rewind_posts; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • You shouldn’t be using query_posts() for multiple Loops like that. It’s for customising the main (ie singular) query only. Try using WP_Query() or get_posts().

    Thread Starter Pendulum

    (@pendulum)

    Thanks esmi, I knew query_posts wasn’t the ideal method. It was just a quick fix to make the client happy. Before posting this question, I took a look at get_posts and if I remember correctly it doesn’t let you pull posts by tag.

    Reading through the docs for WP_Query it looks like that’s the way to go.

    Thanks again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘query_posts causing page load delay – Best alternative?’ is closed to new replies.