Skip to content

Commit

Permalink
WP_Model::count() minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonybudd committed Jun 1, 2017
1 parent ed2cb71 commit 15d25b6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ Product::single(); // Returns the current model if on a single page or in the lo

Product::exists(15); // Returns (bool) true or false

Product::count(); // Efficient way to get the number of models (Don't use count(WP_Model::all()))
Product::count($postStatus = 'publish'); // Efficient way to get the number of models (Don't use count(WP_Model::all()))

$product->get($attribute, $default) // Get attribute from the model

Expand Down
4 changes: 2 additions & 2 deletions src/WP_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,10 @@ public static function exists($ID, $postTypeSafe = TRUE)
*
* @return int
*/
public static function count()
public static function count($postStatus = 'publish')
{
$count = wp_count_posts(Self::getPostType());
return !is_null($count)? intval($count->publish) : 0;
return !is_null($count)? (isset($count->$postStatus)? intval($count->$postStatus) : 0) : 0;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ function test(){
if(! ( is_int(Product::count())) ){
error(__LINE__ .' count()');
}

if(! ( is_int(Product::count('publish'))) ){
error(__LINE__ .' count()');
}

if(! ( Product::count('draft') == 0) ){
error(__LINE__ .' count()');
}

// ---- post()
$product = Product::insert([
Expand Down

0 comments on commit 15d25b6

Please sign in to comment.