0

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.');
}   }   }   
add_action('admin_head','efpd_limit_posts');
1
  • Now you left out 'author'=>$current_user->ID, from the array
    – Bainternet
    Mar 20, 2011 at 15:43

1 Answer 1

1

Your array phrasing is wrong, cange

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

To

$count_posts = count(get_posts(array('author'=>$current_user->ID,'post_type' => 'newpages')));
8
  • Thanks again! I ought to just hire you to answer my questions! :)
    – Jared
    Mar 20, 2011 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, 2011 at 13:56
  • @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, 2011 at 14:49
  • @Bainternet: You're welcome :)
    – kaiser
    Mar 20, 2011 at 15:04
  • 1
    @Bainternet: ,)
    – kaiser
    Mar 21, 2011 at 0:25

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.