0

I have a huge number of posts and the pagination with no limits eats my server CPU and ram so I need a function to limit the number of posts for all query_posts on my site and I need this function to work in the admin area too if possible, any suggestions??

function limit_posts {
$args = array(
    'posts_per_page' => 10,
    'post_type' => 'job_listing',
);

global $wp_query;
$wp_query = new WP_Query( $args );
  • Are you sure it is the number of posts on Wordpress Admin that hamper your server performance? – sri Nov 28 '13 at 14:22
  • my admin post area is sooo slow and the posts eats my cpu on the main site when i have a lot of visitors – user1921704 Nov 28 '13 at 14:26
  • CPU is used when you have a lot of visitors. I do not think that is in direct relation to WordPress admin. – sri Nov 28 '13 at 14:32
  • 1
    What is "huge"? How many posts do you have? – s_ha_dum Nov 28 '13 at 15:02
  • 1
    Go into the "Screen Settings" tab in the admin area (upper right corner) and adjust the number of posts per page. And you're done. – kaiser Nov 28 '13 at 15:09
0

It is not that hard to enforce a hard limit on results:

add_action(
  'post_limits',
  function($limit) {
    $limits = explode(',',$limit);
    if (isset($limits[1]) && 2000 < $limits['1']) {
      $limits[1] = 2000;
    }
    $limit = implode(',',$limits);
    return $limit;
  }
);

I would not be surprised if that does not solve your problem though. Just searching over that many posts may be too much for your server, no matter what the limit is on the returned results.

I am also not 100% sure that that will not break things. Be careful.

You could use 'no_found_rows'=>true for any queries that do not need pagination. That might help.

Ultimately, I suspect that you are just doing too much for your server and/or you need someone who is very, very good with server configuration to help you tweak settings and possibly load balance.

0

http://digwp.com/2009/12/limit-posts-without-plugin/ i think this may be useful for you

<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<p>?php the_time(); ?></p>
<?php the_content(); ?>
<p><?php the_tags(); ?></p>
<p><?php next_posts_link(); ?></p>
<p><?php previous_posts_link(); ?></p>
<h1>Not Found</h1>
<p>Silly monkey.</p>
or second method
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<p>?php the_time(); ?></p>
<?php the_content(); ?>
<p><?php the_tags(); ?></p>
<p><?php next_posts_link(); ?></p>
<p><?php previous_posts_link(); ?></p>
<h1>Not Found</h1>
<p>Silly monkey.</p>
  • Sorry, exactly how does any of that code posted limit the number of posts on a page? – s_ha_dum Dec 2 '13 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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