Title: converting from query_posts
Last modified: August 21, 2016

---

# converting from query_posts

 *  [Nick](https://wordpress.org/support/users/tolputtkeeton/)
 * (@tolputtkeeton)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/converting-from-query_posts/)
 * Need to change my code from query_posts to use get_posts, however, I’m struggling
   to change and keep the way it currently displays info.
 * You can see some of the code here: [http://pastebin.com/UqmdyYmS](http://pastebin.com/UqmdyYmS)
   containing what I need to convert.
 * I then echo the info out later on in the page like this:
 *     ```
       <h3><?php echo $blog_title[0]; ?></h3>
       <p><?php echo $blog_excerpt[0]; ?></p>
       <?php echo $postdata[0]; ?>
       ```
   
 * Changing [0] for [1], [2] etc… to display different content.
 * The examples on wordpress functions page has a foreach function which I dont 
   think I need, or do I ?
 * Thanks.

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/converting-from-query_posts/#post-4624517)
 * > Need to change my code from query_posts to use get_posts
 * Why?
 *  Thread Starter [Nick](https://wordpress.org/support/users/tolputtkeeton/)
 * (@tolputtkeeton)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/converting-from-query_posts/#post-4624518)
 * From WordPress codex:
 * [http://codex.wordpress.org/Function_Reference/query_posts](http://codex.wordpress.org/Function_Reference/query_posts)
 * > Note: This function isn’t meant to be used by plugins or themes. As explained
   > later, there are better, more performant options to alter the main query. Double
   > Note: query_posts() is overly simplistic and problematic way to modify main
   > query of a page by replacing it with new instance of the query.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/converting-from-query_posts/#post-4624536)
 * consider to change the code to `WP_Query()` – which has a much closer relationship
   to `query_posts()` than `get_posts()`;
    [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 * if using `get_posts()` don’t forget to include `setup_postada( $post )` – [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
 * also watch that `'category_name'` uses the **category _slug_** (NOT name); [http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters)
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/converting-from-query_posts/#post-4624541)
 * have you tried using the [pre_get_posts() filter](http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts)?
 *  Thread Starter [Nick](https://wordpress.org/support/users/tolputtkeeton/)
 * (@tolputtkeeton)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/converting-from-query_posts/#post-4624703)
 * Thanks for the pointers.
 * How do I get an array out of the loop using WP_Query?
 * Take this from the WordPress functions page, how do I put get_the_title() into
   an array, amongst other things?
 *     ```
       $the_query = new WP_Query( $args );
   
       // The Loop
       while ( $the_query->have_posts() ) {
       	$the_query->the_post();
       	echo '<li>' . get_the_title() . '</li>';
       }
       ```
   
 * With this, I get Undefined variable title on line…
 *     ```
       $the_query = new WP_Query( $args );
   
       // The Loop
       while ( $the_query->have_posts() ) {
       	$the_query->the_post();
       	$title[] = get_the_title();
       }
       ```
   
 * I just echo $title[0] or $title[1] for example.

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

The topic ‘converting from query_posts’ is closed to new replies.

 * 5 replies
 * 3 participants
 * Last reply from: [Nick](https://wordpress.org/support/users/tolputtkeeton/)
 * Last activity: [12 years, 2 months ago](https://wordpress.org/support/topic/converting-from-query_posts/#post-4624703)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
