Skip to content

Commit a6e4c61

Browse files
authored
Merge pull request #241 from atanas-angelov-dev/4573-add-count-to-post-type-and-taxonomy
Add count to post-type and taxonomy commands
2 parents de1f95c + 47f91a0 commit a6e4c61

File tree

5 files changed

+199
-19
lines changed

5 files changed

+199
-19
lines changed

README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3037,7 +3037,7 @@ wp post-type get <post-type> [--field=<field>] [--fields=<fields>] [--format=<fo
30373037

30383038
**AVAILABLE FIELDS**
30393039

3040-
These fields will be displayed by default for each post type:
3040+
These fields will be displayed by default for the specified post type:
30413041

30423042
* name
30433043
* label
@@ -3049,7 +3049,9 @@ These fields will be displayed by default for each post type:
30493049
* cap
30503050
* supports
30513051

3052-
There are no optionally available fields.
3052+
These fields are optionally available:
3053+
3054+
* count
30533055

30543056
**EXAMPLES**
30553057

@@ -3101,7 +3103,9 @@ These fields will be displayed by default for each post type:
31013103
* public
31023104
* capability_type
31033105

3104-
There are no optionally available fields.
3106+
These fields are optionally available:
3107+
3108+
* count
31053109

31063110
**EXAMPLES**
31073111

@@ -3618,6 +3622,24 @@ wp taxonomy get <taxonomy> [--field=<field>] [--fields=<fields>] [--format=<form
36183622
- yaml
36193623
---
36203624

3625+
**AVAILABLE FIELDS**
3626+
3627+
These fields will be displayed by default for the specified taxonomy:
3628+
3629+
* name
3630+
* label
3631+
* description
3632+
* object_type
3633+
* show_tagcloud
3634+
* hierarchical
3635+
* public
3636+
* labels
3637+
* cap
3638+
3639+
These fields are optionally available:
3640+
3641+
* count
3642+
36213643
**EXAMPLES**
36223644

36233645
# Get details of `category` taxonomy.
@@ -3674,10 +3696,14 @@ These fields will be displayed by default for each term:
36743696
* name
36753697
* label
36763698
* description
3677-
* public
3699+
* object_type
3700+
* show_tagcloud
36783701
* hierarchical
3702+
* public
36793703

3680-
There are no optionally available fields.
3704+
These fields are optionally available:
3705+
3706+
* count
36813707

36823708
**EXAMPLES**
36833709

features/post-type.feature

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ Feature: Manage WordPress post types
66
Scenario: Listing post types
77
When I run `wp post-type list --format=csv`
88
Then STDOUT should be CSV containing:
9-
| name | label | description | hierarchical | public | capability_type |
10-
| post | Posts | | | 1 | post |
11-
| page | Pages | | 1 | 1 | page |
9+
| name | label | description | hierarchical | public | capability_type |
10+
| post | Posts | | | 1 | post |
11+
| page | Pages | | 1 | 1 | page |
12+
13+
@require-wp-5.0
14+
Scenario: Listing post types with count
15+
When I run `wp post-type list --fields=name,count --format=csv`
16+
Then STDOUT should be CSV containing:
17+
| name | count |
18+
| post | 1 |
19+
| page | 2 |
1220

1321
Scenario: Get a post type
1422
When I try `wp post-type get invalid-post-type`
@@ -31,3 +39,11 @@ Feature: Manage WordPress post types
3139
"""
3240
"title":true
3341
"""
42+
43+
@require-wp-5.0
44+
Scenario: Get a post type with count
45+
When I try `wp post-type get page --fields=name,count`
46+
Then STDOUT should be a table containing rows:
47+
| Field | Value |
48+
| name | page |
49+
| count | 2 |

features/taxonomy.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ Feature: Manage WordPress taxonomies
1616
| name | label | description | object_type | show_tagcloud | hierarchical | public |
1717
| nav_menu | Navigation Menus | | nav_menu_item | | | |
1818

19+
@require-wp-5.0
20+
Scenario: Listing taxonomies with counts
21+
When I run `wp taxonomy list --fields=name,count --format=csv`
22+
Then STDOUT should be CSV containing:
23+
| name | count |
24+
| category | 1 |
25+
| post_tag | 0 |
26+
1927
Scenario: Get taxonomy
2028
When I try `wp taxonomy get invalid-taxonomy`
2129
Then STDERR should be:
@@ -30,3 +38,11 @@ Feature: Manage WordPress taxonomies
3038
| name | category |
3139
| object_type | ["post"] |
3240
| label | Categories |
41+
42+
@require-wp-5.0
43+
Scenario: Get taxonomy with count
44+
When I run `wp taxonomy get category --fields=name,count`
45+
Then STDOUT should be a table containing rows:
46+
| Field | Value |
47+
| name | category |
48+
| count | 1 |

src/Post_Type_Command.php

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,37 @@ class Post_Type_Command extends WP_CLI_Command {
3434
'capability_type',
3535
);
3636

37+
/**
38+
* Gets the post counts for each supplied post type.
39+
*
40+
* @param array $post_types Post types to fetch counts for.
41+
* @return array Associative array of post counts keyed by post type.
42+
*/
43+
protected function get_counts( $post_types ) {
44+
global $wpdb;
45+
46+
if ( count( $post_types ) <= 0 ) {
47+
return [];
48+
}
49+
50+
$query = $wpdb->prepare(
51+
"SELECT `post_type`, COUNT(*) AS `count`
52+
FROM $wpdb->posts
53+
WHERE `post_type` IN (" . implode( ',', array_fill( 0, count( $post_types ), '%s' ) ) . ")
54+
GROUP BY `post_type`",
55+
$post_types
56+
);
57+
$counts = $wpdb->get_results( $query );
58+
59+
// Make sure there's a count for every item.
60+
$counts = array_merge(
61+
array_fill_keys( $post_types, 0 ),
62+
wp_list_pluck( $counts, 'count', 'post_type' )
63+
);
64+
65+
return $counts;
66+
}
67+
3768
/**
3869
* Lists registered post types.
3970
*
@@ -71,7 +102,9 @@ class Post_Type_Command extends WP_CLI_Command {
71102
* * public
72103
* * capability_type
73104
*
74-
* There are no optionally available fields.
105+
* These fields are optionally available:
106+
*
107+
* * count
75108
*
76109
* ## EXAMPLES
77110
*
@@ -100,7 +133,18 @@ class Post_Type_Command extends WP_CLI_Command {
100133
public function list_( $args, $assoc_args ) {
101134
$formatter = $this->get_formatter( $assoc_args );
102135

103-
$types = get_post_types( $assoc_args, 'objects' );
136+
$fields = $formatter->fields;
137+
$types = get_post_types( $assoc_args, 'objects' );
138+
$counts = [];
139+
140+
if ( count( $types ) > 0 && in_array( 'count', $fields, true ) ) {
141+
$counts = $this->get_counts( wp_list_pluck( $types, 'name' ) );
142+
}
143+
144+
$types = array_map( function( $type ) use ( $counts ) {
145+
$type->count = isset( $counts[ $type->name ] ) ? $counts[ $type->name ] : 0;
146+
return $type;
147+
}, $types );
104148

105149
$formatter->display_items( $types );
106150
}
@@ -132,7 +176,7 @@ public function list_( $args, $assoc_args ) {
132176
*
133177
* ## AVAILABLE FIELDS
134178
*
135-
* These fields will be displayed by default for each post type:
179+
* These fields will be displayed by default for the specified post type:
136180
*
137181
* * name
138182
* * label
@@ -144,7 +188,9 @@ public function list_( $args, $assoc_args ) {
144188
* * cap
145189
* * supports
146190
*
147-
* There are no optionally available fields.
191+
* These fields are optionally available:
192+
*
193+
* * count
148194
*
149195
* ## EXAMPLES
150196
*
@@ -169,6 +215,15 @@ public function get( $args, $assoc_args ) {
169215
$assoc_args['fields'] = $default_fields;
170216
}
171217

218+
$formatter = $this->get_formatter( $assoc_args );
219+
$fields = $formatter->fields;
220+
$count = 0;
221+
222+
if ( in_array( 'count', $fields, true ) ) {
223+
$count = $this->get_counts( [ $post_type->name ] );
224+
$count = $count[ $post_type->name ];
225+
}
226+
172227
$data = array(
173228
'name' => $post_type->name,
174229
'label' => $post_type->label,
@@ -179,9 +234,8 @@ public function get( $args, $assoc_args ) {
179234
'labels' => $post_type->labels,
180235
'cap' => $post_type->cap,
181236
'supports' => get_all_post_type_supports( $post_type->name ),
237+
'count' => $count,
182238
);
183-
184-
$formatter = $this->get_formatter( $assoc_args );
185239
$formatter->display_item( $data );
186240
}
187241

src/Taxonomy_Command.php

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ public function __construct() {
4444
parent::__construct();
4545
}
4646

47+
/**
48+
* Gets the term counts for each supplied taxonomy.
49+
*
50+
* @param array $taxonomies Taxonomies to fetch counts for.
51+
* @return array Associative array of term counts keyed by taxonomy.
52+
*/
53+
protected function get_counts( $taxonomies ) {
54+
global $wpdb;
55+
56+
if ( count( $taxonomies ) <= 0 ) {
57+
return [];
58+
}
59+
60+
$query = $wpdb->prepare(
61+
"SELECT `taxonomy`, COUNT(*) AS `count`
62+
FROM $wpdb->term_taxonomy
63+
WHERE `taxonomy` IN (" . implode( ',', array_fill( 0, count( $taxonomies ), '%s' ) ) . ")
64+
GROUP BY `taxonomy`",
65+
$taxonomies
66+
);
67+
$counts = $wpdb->get_results( $query );
68+
69+
// Make sure there's a count for every item.
70+
$counts = array_merge(
71+
array_fill_keys( $taxonomies, 0 ),
72+
wp_list_pluck( $counts, 'count', 'taxonomy' )
73+
);
74+
75+
return $counts;
76+
}
77+
4778
/**
4879
* Lists registered taxonomies.
4980
*
@@ -77,10 +108,14 @@ public function __construct() {
77108
* * name
78109
* * label
79110
* * description
80-
* * public
111+
* * object_type
112+
* * show_tagcloud
81113
* * hierarchical
114+
* * public
115+
*
116+
* These fields are optionally available:
82117
*
83-
* There are no optionally available fields.
118+
* * count
84119
*
85120
* ## EXAMPLES
86121
*
@@ -112,10 +147,17 @@ public function list_( $args, $assoc_args ) {
112147
$assoc_args['object_type'] = array( $assoc_args['object_type'] );
113148
}
114149

150+
$fields = $formatter->fields;
115151
$taxonomies = get_taxonomies( $assoc_args, 'objects' );
152+
$counts = [];
153+
154+
if ( count( $taxonomies ) > 0 && in_array( 'count', $fields, true ) ) {
155+
$counts = $this->get_counts( wp_list_pluck( $taxonomies, 'name' ) );
156+
}
116157

117-
$taxonomies = array_map( function( $taxonomy ) {
158+
$taxonomies = array_map( function( $taxonomy ) use ( $counts ) {
118159
$taxonomy->object_type = implode( ', ', $taxonomy->object_type );
160+
$taxonomy->count = isset( $counts[ $taxonomy->name ] ) ? $counts[ $taxonomy->name ] : 0;
119161
return $taxonomy;
120162
}, $taxonomies );
121163

@@ -147,6 +189,24 @@ public function list_( $args, $assoc_args ) {
147189
* - yaml
148190
* ---
149191
*
192+
* ## AVAILABLE FIELDS
193+
*
194+
* These fields will be displayed by default for the specified taxonomy:
195+
*
196+
* * name
197+
* * label
198+
* * description
199+
* * object_type
200+
* * show_tagcloud
201+
* * hierarchical
202+
* * public
203+
* * labels
204+
* * cap
205+
*
206+
* These fields are optionally available:
207+
*
208+
* * count
209+
*
150210
* ## EXAMPLES
151211
*
152212
* # Get details of `category` taxonomy.
@@ -179,6 +239,15 @@ public function get( $args, $assoc_args ) {
179239
$assoc_args['fields'] = $default_fields;
180240
}
181241

242+
$formatter = $this->get_formatter( $assoc_args );
243+
$fields = $formatter->fields;
244+
$count = 0;
245+
246+
if ( in_array( 'count', $fields, true ) ) {
247+
$count = $this->get_counts( [ $taxonomy->name ] );
248+
$count = $count[ $taxonomy->name ];
249+
}
250+
182251
$data = array(
183252
'name' => $taxonomy->name,
184253
'label' => $taxonomy->label,
@@ -189,9 +258,8 @@ public function get( $args, $assoc_args ) {
189258
'public' => $taxonomy->public,
190259
'labels' => $taxonomy->labels,
191260
'cap' => $taxonomy->cap,
261+
'count' => $count,
192262
);
193-
194-
$formatter = $this->get_formatter( $assoc_args );
195263
$formatter->display_item( $data );
196264
}
197265

0 commit comments

Comments
 (0)