WordPress.org

Forums

[resolved] query_posts causing page load delay - Best alternative? (3 posts)

  1. Pendulum
    Member
    Posted 3 years ago #

    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; ?>
  2. esmi
    Forum Moderator
    Posted 3 years ago #

    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().

  3. Pendulum
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic