2

I need to run a background process in a plugin that does some work on ALL the posts in a person's database once the plugin is installed. I'm very new at working with wordpress so my research has shown that I can use the wp query or I can use "the loop".

Since I'm going through absolutely every single post speed is of the essence. I need to check the title, body, categories, meta tags, publish state and password protected. So based on this, which one of these would be fastest?

2 Answers 2

4

"The Loop" is just a name given to the while (have_posts()): the_post(); loop used to iterate over an array of posts returned by WP_Query(). The other function used for querying posts is the get_posts() function, which returns a simple (non-extended) array which you can loop through with a foreach loop.

I don't think it matters a whole lot; however, if you're nitpicking, the get_posts() method is slightly faster and less memory-intensive, since it doesn't call setup_postdata() (which populates the template tags, etc.) on every post it loops through.

Either way, you're probably going to run out of memory on some setups with thousands of posts... Make sure you build in fallbacks in that case.

6
  • aaah... how do I explicitly deallocate memory after finishing with a given post?
    – Thirlan
    Commented Mar 23, 2011 at 23:22
  • looking at the codex API it seems there's an 'offset' and 'numberposts'. I can use this to only go through 100 at a time right? If so, that would solve the memory issue through garbage collection? Or are things cached in memory : / ?
    – Thirlan
    Commented Mar 23, 2011 at 23:28
  • To be honest, I'm not totally sure. Unset each of the local variables you're using before looping on to the next post. Call ini_set('max_execution_time', $somehugenumber); before starting. And include some kind of progress indicator to discourage user interrupt. Or break your task into chunks and run it as scheduled cron events. Commented Mar 23, 2011 at 23:34
  • Yes, you can use 'offset' to go through your posts in reasonable-sized chunks. That way you're only fetching an array of a specified size into memory. The get_posts array returns all the columns in the wp_posts table, so calling get_posts('numberposts=-1') on a site with 10000 posts loads into memory an array including the full post content from all 10000 posts. That would be the memory issue... Commented Mar 23, 2011 at 23:38
  • 1
    @Thirlan: Many functions that do something with a post will call get_post(), which will add the post to the cache. So even if you do multiple database queries for a single HTTP request, not all memory will be reclaimed. I recommend you do this with multiple requests (via Ajax, with a progressbar for feedback?), where each request processes a number of posts.
    – Jan Fabry
    Commented Mar 24, 2011 at 12:40
0

"The loop" is just a name applied to a foreach loop applied to a WP_Query object. So you need both. Typically the loop also includes the_post() so that you can use template tags.

There's another option though, that may be faster. Depending on what you need to do, you could do a direct database query to retrieve just the fields you want to edit. But since you also need categories and meta tags it would be a complicated join. For easier updates and backward compatability, you may as well use the regular WP_Query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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