How to have a custom post type not query all posts

I have a custom post type that has well over 100k posts in it right now. When I try to go to the main page of the post type in the admin panel, I’m getting the error:

Fatal error: Allowed memory size of 94371840 bytes exhausted (tried to allocate 84 bytes) in /home/spottercharts/spottercharts.com/wp-includes/cache.php on line 506

Read More

The main page usually lists all the posts that are of that type. Since there are so many, it is eating up all the PHP memory.

If increasing the PHP memory in php.ini is not an option right now, is there another way to have WordPress not query all posts of that type? I only need the capability to search the posts, not list them all out. If not, is there a way to optimize this post type so it doesn’t consume so much memory?

Related posts

Leave a Reply

1 comment

  1. First thing I have to ask is how have you got over 100k posts? That’s a hell of a lot to have in your database as individual posts, and I suspect that they’re not all actual posts but rather are being used for some other purpose?

    You have a few options here:

    Short term – memory limit

    Memory limits can be modified on a server in a number of ways, depending upon your server configuration or what your host allows. If you don’t have root access, you could put a php.ini file into the home directory of your website.

    Alternatively, modify your .htaccess file by adding the following:

    php_value memory_limit 256M
    

    As you’re using WordPress, you can also add the following line to your wp-config.php file:

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

    Better method

    Obviously the above is a good way to at least get the site functioning quickly, but really that is very high usage.

    A few suggestions as to how you can improve your memory usage:

    1. Decrease the size of the database. Is there alternative methods you
      could use to decrease how many individual posts you have?
    2. Make sure that you request a specific number you would like returned when using query_posts. This will make sure that it stops pulling too many from the database and therefore leaves the server with less to deal with.

    There’s many other things you can do, but really you need to consider why you have so much data in the database, and how you can minimise the amount of queries and work that WordPress is required to undertake to return your data.