| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gets the post count for a given post type. |
| 4 |
* |
| 5 |
* @since 1.0.0 |
| 6 |
* @package userswp |
| 7 |
* @param int $user_id User Id. |
| 8 |
* @param string $post_type Post Type. |
| 9 |
* @param string $extra_post_status Optional. Extra post status. |
| 10 |
* @return int Post count. |
| 11 |
*/ |
| 12 |
function uwp_post_count($user_id, $post_type, $extra_post_status = '') { |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
$post_status = ""; |
| 16 |
if ($user_id == get_current_user_id()) { |
| 17 |
$post_status = ' OR post_status = "draft" OR post_status = "private" '; |
| 18 |
} |
| 19 |
|
| 20 |
if (!empty($extra_post_status)) { |
| 21 |
$post_status .= $extra_post_status; |
| 22 |
} |
| 23 |
|
| 24 |
$post_status_where = ' AND ( post_status = "publish" ' . $post_status . ' )'; |
| 25 |
|
| 26 |
if($extra_post_status == 'any'){ |
| 27 |
$post_status_where = ''; |
| 28 |
} |
| 29 |
|
| 30 |
$count = $wpdb->get_var(' |
| 31 |
SELECT COUNT(ID) |
| 32 |
FROM ' . $wpdb->posts. ' |
| 33 |
WHERE post_author = "' . $user_id . '" |
| 34 |
' . $post_status_where . ' |
| 35 |
AND post_type = "' . $post_type . '"' |
| 36 |
); |
| 37 |
return $count; |
| 38 |
} |