Better way to loop through all posts without using a boatload of memory?

I've been looking, but haven't found any obvious way (perhaps I'm just missing it) to start at the first post, do some processing, and loop to the next, until the last post.

This approach uses just a TON of memory when there's 13,000 posts (yeah, oops):

$postslist = get_posts($args);
    foreach ($postslist as $post) {
    }

Any better techniques?

Thanks!

  • drmike
    • DEV MAN’s Mascot

    Kind of depends. When we hit stuff like this, we try to offload only the needed data into a different database table and access that instead of the full wp database. For example, when we do a Most Viewed Posts, we're looking at a database with only the name of the post, the url, and the number of views. And then to create that database, we're pulling the data once an hour instead of doing it live. Kind of like caching.

    Are you trying to do something specific?

  • David
    • The Crimson Coder

    But there doesn't seem to be an interface for grabbing a post number, just a post ID. I know how to go to the next post (get_next_post). But I haven't figured out how to go to the first post (again, the API doesn't seem to show it).

    Using your approach, I'd need to be able to get the total count of posts and, at the very least, the ID of the first post or some way to set the post index to the first post.

  • drmike
    • DEV MAN’s Mascot

    total count of posts

    Open up the database, see how many records you have and what the highest number is.

    ID of the first post

    1 :slight_smile:

    If post #1 not found, end loop and move on to #2.

    edit: Actually come to think of it, unless you've deleted the default post, the id of the first post should be 1.

    I;m looking at this as a one off, a script that you run once that while may call wordpress functions, it sits outside of the code. I think you;re looking at it as an actual plugin that you can use again.

    Or maybe I;m looking at grab a single post, do what you need, grab the next post, do what you need, grab....

  • David
    • The Crimson Coder

    You are wise man. Duh, one-time use means my old buddy, direct MySQL queries are fair game. That works. By the way, my attempt at get_boundary_post didn't.

    P.S. I don't care what the others say about you, even if you don't share the chocolate, I still like ya!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.