WordPress category php query running out of memory?

I’ve got a wordpress page which has a map of a recent trip I did.

http://www.marksmayo.com/south-american-journey/

Read More

If I then write a basic php query function to list all posts in the category ‘south america 2010’, it works fine for 4-5 posts, but as I added posts to the category, it started running out of memory with this query (giving errors on the page), and now just doesn’t load anything below the map.

The same code is on:

http://www.marksmayo.com/northern-europe-and-asia-mission/

and is currently working, but as I add more posts that’ll presumably stop too.

The code is:

<?php

// The Query
query_posts( array ( 'category_name' => 'south america 2010', 'posts_per_page' => -1 ) );
// The Loop
while ( have_posts() ) : the_post();
    echo '<li><a href=';
        the_permalink();
        echo'>';
    the_title();

    echo '</a></li>';
endwhile;

// Reset Query
wp_reset_query();

?>

Related posts

Leave a Reply

5 comments

  1. Two things: use a simpler query and raise WP’s memory allocation.

    Try this query, which resets itself and can be used any number of times on a page (with php execution) or in a page template. Change the name of the category and showposts to a number, or -1 to show all.

    <?php $my_query = new WP_Query('category_name=mycategory&showposts=10'); ?>
    
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    
    <?php the_title(); ?></a>
    
    <?php endwhile; ?>
    

    Raise WP’s memory allocation in wp-config.php with this near the top:

    define('WP_MEMORY_LIMIT', '64M');
    

    and check the php.ini file in your account to see if php memory is set to a very low limit. You’re on Bluehost, so if you don’t have a php.ini file, you can add a single php.ini in php configuration in Cpanel and then edit it in your root account.

    Bluehost will allow that much RAM for php, but be aware that Bluehost will throttle your account CPU sometimes; check Cpanel for your CPU usage and throttling amounts.

  2. I’d use the category ID rather than the category slug, so your query becomes:

    query_posts( array ( 'cat' => 4, 'posts_per_page' => -1 ) );
    

    (I’d assume a integer search would be faster, and less intensive.)
    (obviously replace 4 with your category ID).

    You could also remove the call for ‘the_title()’ and replace it with $post->post_title, as you have called the_post(), although to be honest I’m not 100% sure if this will have an impact.

    Also if your on shared hosting you might consider a caching plugin. I’d recommend W3 Total Cache, it will build flat files only running this query when the content actually updates.

    Lastly, what preceeds your code snippet, you haven’t already got this in a loop, so its looping through something else, AND running this every time, have you?

  3. Even if you bump the memory limit in your wp-config.php file as suggested in a separate answer, you won’t escape an out of memory problem down the road because of the massively wasteful manner that $wpdb manages memory. (It creates and stores two copies of each row it retrieves, in WP 3.0, and even that is a huge improvement as compared to what it did in earlier versions.)

    I’d advise to limit the number of posts per page to something more reasonable, e.g. 50.

  4. Have you tried using WP_Query class?
    Maybe this would help:

    // Query posts
    $the_query = new WP_Query( $args );
    
    // Loop the result
    while ( $the_query->have_posts() ) : $the_query->the_post();
        // your impl
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();