2

I'm trying to display a list of posts from a certain category that I can paginate through. Since I've read that get_posts() doesn't support pagination but WP_Query does, I'm using the latter.

Here's my query:

    $mainPosts = new WP_Query(array(
      'post_type'        => 'post',
      'posts_per_page'   => 1,
      'category_name'    => 'main',
    ));

I originally has posts_per_page set to 10, but I've set to 1. No matter what I try, I get an error similar to the following:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 140513280 bytes) in /app/public/wp-includes/functions.php on line 3730

I understand that this would happen if I am trying to pull thousands of posts or performing some kind of very expensive query, but this category has a total of 5-6 posts. I can print_r the returned object and see that it's only pulling the single post that I specified.

I should note that this only occurs if I try to run while ($mainPosts->have_posts()) { ... }.

Here's the full code of what I am doing with the query:

if ($mainPosts->have_posts()) {

    while ($mainPosts->have_posts()) {
        $postItemImage = the_post_thumbnail_url();
        $postPermalink = the_permalink();
        $postTitle = the_title();
        $postDay = the_date('F jS Y');
        $postExcerpt = the_excerpt();

        echo "<div class='row post-item'><div class='col-6 post-item-left'>
                        <div class='post-item-image' style='background-image:url(\"{$postItemImage}\")'></div></div>
                        <div class='col-6 post-item-detail'><h3>{$postTitle}</h3><div class='post-item-detail-header'>{$postDay}</div>
                        <div class='post-item-detail-main'>{$postExcerpt}</div><a href='{$postPermalink}'>Read more</a></div></div>";

    }
}

What am I doing wrong here and how can I list the few posts using WP_Query without having this memory allocation issue?

Edit: I've found that using a break; seems to resolve the memory allocation issue, however this is of course undesirable if I intend to run the loop more than once.

9
  • But it doesn’t say that this is the query that used all that memory. This is the one that exhausted it. It means that there may be some other places that are consuming a lot of memory... May 30, 2018 at 16:51
  • @KrzysiekDróżdż When I remove that one query, everything runs smoothly. I've not had this issue at all in development until I wrote this line of code.
    – AKor
    May 30, 2018 at 17:00
  • because this is the one that goes over the limit... Let’s say that memory limit is 256MB and your site uses 255 of it. If you add one more query that uses 2MB you will go over the limit. It doesn’t mean that this query is the problem... May 30, 2018 at 17:03
  • @KrzysiekDróżdż I've ran memory_get_usage() while all code in question is commented out and I see a usage of 4081472 bytes. Correct me if I am wrong but this implies that a while loop on an object containing a single post is taking 200+ MB of memory?
    – AKor
    May 30, 2018 at 17:09
  • it looks so. What is the content of that post? Does it contain some shortcodes or something like that? May 30, 2018 at 17:12

2 Answers 2

4

Your query looks OK, so I doubt that it will cause any problems. But then... Let's take a look at your loop:

if ($mainPosts->have_posts()) {

    while ($mainPosts->have_posts()) {
        $postItemImage = the_post_thumbnail_url();
        ...    
        echo "...";

    }
}

First of all you don't have any else in there, so there is no point in this if. But there is even bigger problem with this loop...

It will be an infinite loop, because there is no $mainPosts->the_post(), so it doesn't "consume" posts, so there are always all of them when checking loop condition.

This will work fine:

while ($mainPosts->have_posts()) {
    $mainPosts->the_post();
    $postItemImage = the_post_thumbnail_url();
    ...    
    echo "...";
}
2
  • @AKor No problem :) May 30, 2018 at 21:15
  • It's working for me. Upvote from my side Jun 20, 2023 at 11:16
1

Probably some of your plugins use memory. If I were you, I would turn off all the plugins and try it again. Then I might try to find the plugin. On the other hand, theme with poor code might be the culprit.

1
  • The issue only occurs after that one query is written. Throughout development I had not seen this issue until that query. The issue goes away when I don't loop through the query results.
    – AKor
    May 30, 2018 at 17:02

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.