That’s a pretty good article in Codex. So use either get_posts or preferably a new WP_Query for your ‘loops’. For example:
a simple loop to query posts:
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Okay. One vote for Codex. Regarding your example. What are the objects you have populated your array with? I know that “post_type” is a field in the post table but what is ‘caller_get_posts’ and ‘posts_per_page’? Can you tell me where I can find an exhaustive list of these objects, so I can get back to work building a theme or application. I’m itching to get back to my theme but I hit a wall with the different types of loops and all the confusing info out there. So I feel like I’m back to square one I just want to get this right so when and if I release a theme it’s not all mucked up with bad code.
Thanks
I just found this link before checking back here. Thanks.
On this page it states.
This function is used to get a list of all the posts that are defined in the blog. For the most part get_posts can be considered a mini version of query_posts, supporting nearly (but not all) optional parameters. Support parameters are listed below.
I went to the query_post page which also has many parameters listed and usage examples. Can all of this be transfered into the wp_query?
The wp_query page has a list of Methods and Properties but there is little in the way of usage examples. Seems strange if wp_query is the best method for multiple loops that there is not more extensive documentation about usage as there is with the other two loop methods.
Thanks for direction Michael and RVoodoo. A new journey begins.
What purpose does wp_reset_query()
serve if not to support multiple calls to query_posts()
Comments directly from wp-includes/query.php ..
/**
* Destroy the previous query and setup a new query.
*
* This should be used after {@link query_posts()} and before another {@link
* query_posts()}. This will remove obscure bugs that occur when the previous
* wp_query object is not destroyed properly before another is setup.
*
* @since 2.3.0
* @uses $wp_query
*/
As long as wp_reset_query
is called, it should be valid to use multiple calls to query_posts
should it not?
You have a point there t31os_, but though I thought Otto42’s advice was pretty good:
“When you want to override the “main” query, you use query_posts.”
Note: Otto42 was the one who added that to Codex:
http://codex.wordpress.org/index.php?title=Template_Tags%2Fquery_posts&diff=61715&oldid=61371
Well i can certainly understand that problems may occur as a result of re-using the main query. For example if something else on the page relies on looking at data in the “main query” (something that loads after your series of loops perhaps), it would be looking at incorrect data because the original query has changed (you can clone the original query’s data to fix things like this).
Overall i agree the advice is good, but i don’t think it’s strictly true that you can’t reuse query_posts … i’ve done so without causing any problems as far as i can observe, but if i’m making a mistake in doing so i’d be interested in a further explanation of the following..
Use of query_posts on other Loops than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.
t31os_ and MichaelH. I’m glad this conversation has kept going. As many times as I’ve seen the query_posts used for multiple loops and if one uses the wp_reset query method without any sinister result then I do believe that article in the Codex should be revised and revisited. I think using query_posts is the easiest way to setup a loop.
I’d say it’s proberly good advice to use a new query, just to avoid causing problems with other queries or code that has dependancies on the main query…
In short i think the answer is… you can use multiple calls, but don’t be surprised if other things sometimes give wrong data sets as a result of re-using the main query..
I’m not intimately familiar with the WP_Query class, so i’m not sure on it’s limitations, problems or it’s correct (suggested?) usage, so i’ll happily concede to anyone who can pick my logic apart… and explain how it should be used, and why it should be used a given way…
I’d like to hear all about that too. Thanks t31os_
I’m not entirely sure that really addresses the question asked, though i suppose it is partially related, i had read the discussion before.
Perhaps it would just be easier if i say i agree with Otto in so much that general practice should be to create a new query object for each loop … unless you need to repeat the same query (you have rewind_posts for that though) .. just to avoid those cases when “unpredictable results” can occur ..
Again that’s not to say re-using query_posts won’t work, but in general there’s far less chance of getting skewed results if a given user chooses to utilize a new query in place of re-using the main query (ie. query posts)..
One small question remains though, what was the intention of the reset function if not to facilitate the re-use of query_posts?
What’s your take Michael, do you generally use a new query object, or do you recycle the main query? … And what was your personal reasoning to using that particular approach over the other? (I’m only curious).
@t31os_
Well it just seemed easier to use a new query object and not have to worry about messing up the ‘other’ query that WordPress automatically populates. Plus in testing when answering forum questions I’m usually throwing the code at the top of index.php or archive.php or sidebar.php for the Default theme and the new query just worked 😉
But you are right about the wp_reset_query and I suppose that if you know what you are doing, either approach will work.
Note – I figured you’d read that hackers thread but just added for future readers.