Title: Alternative to query_posts?
Last modified: August 21, 2016

---

# Alternative to query_posts?

 *  [Bonnie](https://wordpress.org/support/users/bonnie/)
 * (@bonnie)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/alternative-to-query_posts/)
 * I’m using query_posts quite a bit on my site as it seems to offer the most flexibility
   when processing the values of custom fields.
 * I was wondering if there was any way to make my code more efficient? In the example
   below I’m using query_posts to pull a list of all posts with a post type of “
   press” and then sorting them into lists by year of publication date. It feels
   quite resource-heavy though as the page load time is on the long side.
 * FYI: I’m using the WP-Types plugin for the management of custom post types and
   the display of additional fields on the post screen.
 *     ```
       <section id="press-archive">
           <ul>
           	<?php
           	query_posts('posts_per_page=-1&post_type=press&order=desc&orderby=meta_value_num&meta_key=wpcf-publication-date');
               $current_year = '';
               while ( have_posts() ) : the_post();
               	global $post;
               	$this_year = get_post_meta( $post->ID, 'wpcf-publication-date', true );
               	$this_year = date('Y', $this_year);
               	if (($this_year != $current_year)) :
               		if ($current_year != '')
                           echo '</ul>' . "\n";
               			echo '</li>' . "\n";
               			echo '<li class="year">' . "\n";
               			echo '<h2 class="header">' . $this_year . '</h2>' . "\n";
                           echo '<ul>' . "\n";
               			$current_year = $this_year;
               		endif;
               		echo '<li id="' . $this_year . '">' . "\n";
                       ?>
                           <span class="source"><?php echo(types_render_field("source", array())); ?></a> &mdash; <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> <span class="publication-date">(<?php echo(types_render_field("publication-date", array("format" => "jS F"))); ?>)</span>
                       </li>
               <?php
               endwhile;
               if ($current_year != '') echo '</li>' . "\n";
               wp_reset_postdata();
               ?>
           </ul>
       </section>
       ```
   
 * Any help on making this less resource-heavy would be fantastic. Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/alternative-to-query_posts/#post-3677553)
 * review [http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts?rq=1](http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts?rq=1)
 *  Thread Starter [Bonnie](https://wordpress.org/support/users/bonnie/)
 * (@bonnie)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/alternative-to-query_posts/#post-3677555)
 * Thanks for the link! I’d like to think that makes sense to me, but it doesn’t…
   yet. I’m guessing I can’t just replace every occurrence of query_posts with get_posts,
   and I wouldn’t know where to start. May need to do more reading. I feel a bit
   out of my depth with this one!
 *  Thread Starter [Bonnie](https://wordpress.org/support/users/bonnie/)
 * (@bonnie)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/alternative-to-query_posts/#post-3677560)
 * Hmm, just been reading up on this and still can’t find a way to order by a custom
   field with get_posts… does anyone have any ideas?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/alternative-to-query_posts/#post-3677563)
 * > can’t just replace every occurrence of query_posts with get_posts
 * I personally would prefer using `WP_Query()` instead of `get_posts()` as it keeps
   more-or-less the same syntax;
 * > can’t find a way to order by a custom field with get_posts…
 * `get_posts()` alledgedly takes the same parameters as `WP_Query()` or `query_posts()`
 *  Thread Starter [Bonnie](https://wordpress.org/support/users/bonnie/)
 * (@bonnie)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/alternative-to-query_posts/#post-3677567)
 * Using WP_Query now successfully – thank you! I’ve also realised WP_Query has 
   another feature (multiple custom field handling) that I was struggling with when
   using query_posts, so that’s brilliant. I’m so pleased. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Alternative to query_posts?’ is closed to new replies.

 * 5 replies
 * 2 participants
 * Last reply from: [Bonnie](https://wordpress.org/support/users/bonnie/)
 * Last activity: [12 years, 12 months ago](https://wordpress.org/support/topic/alternative-to-query_posts/#post-3677567)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
