Looking in other plugins/widgets code , I usually see TWO different methods for a query :
one would be :
$args = array( 'numberposts' => $limit , // or, 1, or 20 ...
'exclude' => '1049',
'meta_key' => 'some_meta',
'post_status'=>'publish');
$recent_posts = get_posts( $args ); //or get_recent_posts() or any other function
//end query
foreach( $recent_posts as $recent ){ // do something with array
$pop_string .= '<li><a href="' . get_permalink($recent["ID"]) .'>' . $recent["post_title"] .'</a></li> ';
}
echo $pop_string;
the second method would be :
$orig_post = $post;
global $post;
// query posts
$my_query = new WP_Query('posts_per_page=5&meta_key=somekey&meta_compare=>=&meta_value=somevalue&orderby=meta_value&order=ASC');
if( $my_query->have_posts() ) {
while( $my_query->have_posts() ) {
$my_query->the_post();
$pop_string .= '<li><a href="' . the_permalink() . '" rel="upcomming" title="' . the_title() . '">';
$pospstring .= the_title() . '</a></li>';
}
}
echo $pop_string;
$post = $orig_post;
wp_reset_query();
I am very curious as to What is the difference between them .
(I do not mean the fact that one uses array and one is using direct wp functions. - but more the technical capabilities) I understand that the second one is a "custom loop" and the first one is "outside" a loop - but both can be used in widgets for example, to achieve very similar results (if not equal ..)
- Is there something that one can do , that the other can´t ?
- Which one is the preferred choice , and when ?
- what would be the considerations to use one over the other , and how to choose them?
- Is there a performance difference ?
- If I want for example to write some simple widgets to get data - which should I choose ? and why ?