Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When using the 'fields' => 'ids' parameter, is it better to create a new WP_Query in the code below rather than doing get_posts?

How would the loop look if I used WP_Query?

function myplugin_delete_image_items( $postid ) {

    $args = array(
        'post_type'     => 'myplugin_image_item', 
        'cache_results' => false,   // Disable caching, used for one-off queries
        'no_found_rows' => true,    // We don't need pagination, so disable it
        'fields'        => 'ids',   // Returns post IDs only
        'meta_query'    => array(
                                array(
                                'key'     => 'myplugin_image_id',
                                'value'   => $postid,
                                'compare' => 'LIKE' 
                                )
        )                   
    );

    $image_item_ids = get_posts( $args ); 
    foreach( $image_item_ids as $image_item_id )  {          
            $purge = wp_delete_post( $image_item_id, true ); // delete the image item, skipping trash    
    }
    wp_reset_postdata();
}  
share|improve this question
    
Better by what criteria? –  s_ha_dum Mar 15 '13 at 20:56
    
Performance wise. –  drtanz Mar 15 '13 at 21:17
2  
get_posts itself just creates a new WP_Query, all parameters being equal it's exactly the same. –  Milo Mar 16 '13 at 5:24
add comment

2 Answers

up vote 2 down vote accepted

get_posts() simply uses a WPQuery object and sets a few default values. In general the values it sets make it mildly more performant. But I wouldn't worry to much about it.

get_posts() is simplier so as a rule of thumb if you don't need any of the more involved features of WP_Query (including pagination) - then stick with it. But largely comes down to personal preference.

I would leave your example as is. Though you don't need to explicitly set all the values as `get_posts' sets some by default.

share|improve this answer
add comment

If you use WP_Query and create a new object you wont touch the main query keeping it clean. Its safe to use anywhere. Its more complicated but gives you more control and power.

share|improve this answer
    
Actually here is a useful post wordpress.stackexchange.com/questions/1753/… –  Jimbo Mar 15 '13 at 20:42
    
how would loop look if I used WP_Query (considering I am using the 'fields' => 'ids' parameter? –  drtanz Mar 16 '13 at 4:43
    
Something like.. $delete_images = new WP_Query($args); while($delete_images->have_posts()){ $delete_images->the_post(); wp_delete_post( get_the_ID(), true ); } –  Jimbo Mar 16 '13 at 15:19
    
Actually that didn't work as the_post is not available (I believe) when using the 'fields' => 'ids' paramter. –  drtanz Mar 16 '13 at 16:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.