WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been trying to limit the amount of posts a user can create in a specific custom post type, and I had some help from Bainternet by checking out his plugin. I read over that and then came up with my own, but it doesn't seem to be working. I want to make my code much more lightweight than an entire new plugin so I am just adding it to mine, but what am I doing wrong here?

The name of my custom post type will be lets say.. newpages. And we'll pretend $custom = 'Developer'; has been placed before the function.

function efpd_limit_posts(){
    global $pagenow,$custom;
    if (is_admin() && $pagenow=='post-new.php?post_type=newpages'){
        $theposts = get_posts(array('post_type'=>'newpages'));
        if (count($theposts) >= '1'){
            wp_die('You have reached the maximum amount of 'newpages' you can create.');
}   }   }   
share|improve this question
Now you left out 'author'=>$current_user->ID, from the array – Bainternet Mar 20 '11 at 15:43
up vote 1 down vote accepted

Your array phrasing is wrong, cange

$count_posts = count(get_posts(array('author'=>$current_user->ID,'post_type','newpages')));


$count_posts = count(get_posts(array('author'=>$current_user->ID,'post_type' => 'newpages')));
share|improve this answer
Thanks again! I ought to just hire you to answer my questions! :) – Jared Mar 20 '11 at 13:45
@Bainternet: I meassured some "count"s and found them beeing extremly slow when not used outside a 'loop' (foreach, if, etc.) or directly on a function like get_posts. It's much faster if you do it like $some_posts = get_posts... $count = count($some_posts) – kaiser Mar 20 '11 at 13:56
@Kaiser: Thanks I'll update my plugin. – Bainternet Mar 20 '11 at 14:24
@Bainternet - I updated my code in my question, it doesn't seem to be working. Is there something else I'm forgetting here? – Jared Mar 20 '11 at 14:49
@Bainternet: ,) – kaiser Mar 21 '11 at 0:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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