Skip to content

Commit e69911f

Browse files
committed
Filter wp post list by the columns it displays
The command forwards whatever it is given to WP_Query, and the fields it displays are named after wp_posts columns. Where the two spellings differ, passing the column name reached WP_Query as an argument it does not know, so it was dropped and every post came back - a filter that reads as if it works and quietly does not. Fifteen of the twenty-five available fields are filters now. Eight already were and are only documented here: post_status, post_type, post_parent, post_mime_type, comment_status, comment_count, ping_status and menu_order. The other seven are translated to the argument that filters on them: - post_title -> title - post_author -> author - post_name -> post_name__in - post_date, post_date_gmt, post_modified, post_modified_gmt -> date_query bounded by the given value, so a full timestamp matches that second and a date matches that whole day post_name goes through 'post_name__in' rather than 'name' because 'name' makes it a single-post query, and WP_Query discards a draft returned by one of those unless the current user can edit it. Running as no user, it cannot, so the slug filter found nothing for exactly the posts most worth finding. That only showed up under '--field'; '--format=count' and '--format=ids' set 'fields' to 'ids' and took a different path through WP_Query. The remaining fields stay output only, with no WP_Query argument behind them, and the docs now say so rather than leaving a reader to find that out by passing one and believing the result. 'ID' is left to WP_Query's own '--p'. Documenting it, even as an alias, would put '--s', '--m', '--w' and '--tb' within an edit or two of a documented parameter, and those are real query arguments. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SSZzqMJRDTiLiDxQEPYcL
1 parent 5dc9619 commit e69911f

3 files changed

Lines changed: 263 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,7 @@ wp post get <id> [--field=<field>] [--fields=<fields>] [--format=<format>]
31503150
Gets a list of posts.
31513151

31523152
~~~
3153-
wp post list [--<field>=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>]
3153+
wp post list [--<field>=<value>] [--post_title=<post_title>] [--post_name=<post_name>] [--post_author=<post_author>] [--post_status=<post_status>] [--post_type=<post_type>] [--post_parent=<post_parent>] [--post_mime_type=<post_mime_type>] [--comment_status=<comment_status>] [--comment_count=<comment_count>] [--ping_status=<ping_status>] [--menu_order=<menu_order>] [--post_date=<date>] [--post_date_gmt=<date>] [--post_modified=<date>] [--post_modified_gmt=<date>] [--field=<field>] [--fields=<fields>] [--format=<format>]
31543154
~~~
31553155

31563156
Display posts based on all arguments supported by [WP_Query()](https://developer.wordpress.org/reference/classes/wp_query/).
@@ -3161,6 +3161,52 @@ Only shows post types marked as post by default.
31613161
[--<field>=<value>]
31623162
One or more args to pass to WP_Query.
31633163

3164+
[--post_title=<post_title>]
3165+
Filter by post title, matched in full.
3166+
3167+
[--post_name=<post_name>]
3168+
Filter by post slug.
3169+
3170+
[--post_author=<post_author>]
3171+
Filter by the ID of the post's author.
3172+
3173+
[--post_status=<post_status>]
3174+
Filter by post status. Defaults to 'any'.
3175+
3176+
[--post_type=<post_type>]
3177+
Filter by post type (comma-separated).
3178+
3179+
[--post_parent=<post_parent>]
3180+
Filter by the ID of the post's parent.
3181+
3182+
[--post_mime_type=<post_mime_type>]
3183+
Filter by MIME type.
3184+
3185+
[--comment_status=<comment_status>]
3186+
Filter by comment status.
3187+
3188+
[--comment_count=<comment_count>]
3189+
Filter by number of comments.
3190+
3191+
[--ping_status=<ping_status>]
3192+
Filter by ping status.
3193+
3194+
[--menu_order=<menu_order>]
3195+
Filter by menu order.
3196+
3197+
[--post_date=<date>]
3198+
Filter by the date the post was published. Accepts a timestamp or a date;
3199+
a value carrying no time of day matches that whole day.
3200+
3201+
[--post_date_gmt=<date>]
3202+
Filter by the GMT date the post was published, as `--post_date` does.
3203+
3204+
[--post_modified=<date>]
3205+
Filter by the date the post was last modified, as `--post_date` does.
3206+
3207+
[--post_modified_gmt=<date>]
3208+
Filter by the GMT date the post was last modified, as `--post_date` does.
3209+
31643210
[--field=<field>]
31653211
Prints the value of a single field for each post.
31663212

@@ -3213,6 +3259,10 @@ These fields are optionally available:
32133259
* filter
32143260
* url
32153261

3262+
The fields listed as options above double as filters, and 'ID' is filtered
3263+
with WP_Query's own '--p'. The remaining fields are output only, with no
3264+
WP_Query argument behind them.
3265+
32163266
**EXAMPLES**
32173267

32183268
# List post

‎features/post.feature‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,107 @@ Feature: Manage WordPress posts
406406
new post
407407
"""
408408

409+
Scenario: Filter the post list by the columns it displays
410+
When I run `wp post create --post_title='Alpha' --post_name=alpha --post_status=publish --porcelain`
411+
Then save STDOUT as {ALPHA_ID}
412+
413+
When I run `wp post create --post_title='Beta' --post_name=beta --post_status=draft --post_parent={ALPHA_ID} --menu_order=3 --porcelain`
414+
Then save STDOUT as {BETA_ID}
415+
416+
# Named after the wp_posts column, so each of these reached WP_Query as an
417+
# argument it does not know and every post came back instead.
418+
When I run `wp post list --post_title=Alpha --field=ID`
419+
Then STDOUT should be:
420+
"""
421+
{ALPHA_ID}
422+
"""
423+
424+
When I run `wp post list --post_name=beta --post_status=any --field=ID`
425+
Then STDOUT should be:
426+
"""
427+
{BETA_ID}
428+
"""
429+
430+
# Only the bundled post has an author; posts created here have none, so this
431+
# discriminates rather than counting everything.
432+
When I run `wp post list --post_author=1 --post_status=any --format=count`
433+
Then STDOUT should be:
434+
"""
435+
1
436+
"""
437+
438+
When I run `wp post list --post_title='No Such Post' --post_status=any --format=count`
439+
Then STDOUT should be:
440+
"""
441+
0
442+
"""
443+
444+
# Already reached WP_Query under their own name, and still do.
445+
When I run `wp post list --post_status=draft --field=ID`
446+
Then STDOUT should be:
447+
"""
448+
{BETA_ID}
449+
"""
450+
451+
When I run `wp post list --post_parent={ALPHA_ID} --post_status=any --field=ID`
452+
Then STDOUT should be:
453+
"""
454+
{BETA_ID}
455+
"""
456+
457+
When I run `wp post list --menu_order=3 --post_status=any --field=ID`
458+
Then STDOUT should be:
459+
"""
460+
{BETA_ID}
461+
"""
462+
463+
# Output only: no WP_Query argument stands behind it, so it is not a filter
464+
# and is not documented as one.
465+
When I run `wp post list --field=post_content --post_status=any`
466+
Then STDOUT should not be empty
467+
468+
Scenario: Filter the post list by a date column
469+
When I run `wp post create --post_title='Old' --post_date='2023-01-24 09:52:00' --post_status=publish --porcelain`
470+
Then save STDOUT as {OLD_ID}
471+
472+
When I run `wp post create --post_title='New' --post_date='2025-06-11 14:30:00' --post_status=publish --porcelain`
473+
Then save STDOUT as {NEW_ID}
474+
475+
# A full timestamp matches that second.
476+
When I run `wp post list --post_date='2023-01-24 09:52:00' --field=ID`
477+
Then STDOUT should be:
478+
"""
479+
{OLD_ID}
480+
"""
481+
482+
# A value carrying no time of day matches that whole day.
483+
When I run `wp post list --post_date=2025-06-11 --field=ID`
484+
Then STDOUT should be:
485+
"""
486+
{NEW_ID}
487+
"""
488+
489+
When I run `wp post list --post_date=1999-01-01 --format=count`
490+
Then STDOUT should be:
491+
"""
492+
0
493+
"""
494+
495+
When I run `wp post list --post_modified=1999-01-01 --format=count`
496+
Then STDOUT should be:
497+
"""
498+
0
499+
"""
500+
501+
# A --date_query of its own is narrowed by these rather than replaced, and its
502+
# relation governs only its own clauses: an 'OR' must not reach the clause the
503+
# date column adds and match a post satisfying neither half of the request.
504+
When I run `wp post list --date_query='{"relation":"OR","0":{"before":"1999-01-01"},"1":{"before":"1998-01-01"}}' --post_date='2023-01-24 09:52:00' --format=count`
505+
Then STDOUT should be:
506+
"""
507+
0
508+
"""
509+
409510
Scenario: List posts with tax query
410511
When I run `wp term create category "First Category" --porcelain`
411512
And I run `wp term create category "Second Category" --porcelain`

‎src/Post_Command.php‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,52 @@ protected function delete_callback( $post_id, $assoc_args ) {
619619
* [--<field>=<value>]
620620
* : One or more args to pass to WP_Query.
621621
*
622+
* [--post_title=<post_title>]
623+
* : Filter by post title, matched in full.
624+
*
625+
* [--post_name=<post_name>]
626+
* : Filter by post slug.
627+
*
628+
* [--post_author=<post_author>]
629+
* : Filter by the ID of the post's author.
630+
*
631+
* [--post_status=<post_status>]
632+
* : Filter by post status. Defaults to 'any'.
633+
*
634+
* [--post_type=<post_type>]
635+
* : Filter by post type (comma-separated).
636+
*
637+
* [--post_parent=<post_parent>]
638+
* : Filter by the ID of the post's parent.
639+
*
640+
* [--post_mime_type=<post_mime_type>]
641+
* : Filter by MIME type.
642+
*
643+
* [--comment_status=<comment_status>]
644+
* : Filter by comment status.
645+
*
646+
* [--comment_count=<comment_count>]
647+
* : Filter by number of comments.
648+
*
649+
* [--ping_status=<ping_status>]
650+
* : Filter by ping status.
651+
*
652+
* [--menu_order=<menu_order>]
653+
* : Filter by menu order.
654+
*
655+
* [--post_date=<date>]
656+
* : Filter by the date the post was published. Accepts a timestamp or a date;
657+
* a value carrying no time of day matches that whole day.
658+
*
659+
* [--post_date_gmt=<date>]
660+
* : Filter by the GMT date the post was published, as `--post_date` does.
661+
*
662+
* [--post_modified=<date>]
663+
* : Filter by the date the post was last modified, as `--post_date` does.
664+
*
665+
* [--post_modified_gmt=<date>]
666+
* : Filter by the GMT date the post was last modified, as `--post_date` does.
667+
*
622668
* [--field=<field>]
623669
* : Prints the value of a single field for each post.
624670
*
@@ -671,6 +717,10 @@ protected function delete_callback( $post_id, $assoc_args ) {
671717
* * filter
672718
* * url
673719
*
720+
* The fields listed as options above double as filters, and 'ID' is filtered
721+
* with WP_Query's own '--p'. The remaining fields are output only, with no
722+
* WP_Query argument behind them.
723+
*
674724
* ## EXAMPLES
675725
*
676726
* # List post
@@ -730,6 +780,67 @@ public function list_( $args, $assoc_args ) {
730780
$query_args['post_type'] = explode( ',', $query_args['post_type'] );
731781
}
732782

783+
// Several of the fields this command displays are named after the wp_posts
784+
// column rather than the WP_Query argument that filters on it. Passing the
785+
// column name reached WP_Query as an argument it does not recognize, so it
786+
// was dropped and every post came back. Translate them instead.
787+
$column_to_query_arg = [
788+
'post_author' => 'author',
789+
'post_title' => 'title',
790+
];
791+
792+
foreach ( $column_to_query_arg as $column => $query_arg ) {
793+
if ( ! isset( $query_args[ $column ] ) ) {
794+
continue;
795+
}
796+
797+
$query_args[ $query_arg ] = $query_args[ $column ];
798+
unset( $query_args[ $column ] );
799+
}
800+
801+
// The slug goes through 'post_name__in' rather than 'name'. Both filter on
802+
// the same column, but 'name' makes this a single-post query, and WP_Query
803+
// throws away a draft returned by one of those unless the current user can
804+
// edit it - which, running as no user, it cannot.
805+
if ( isset( $query_args['post_name'] ) ) {
806+
$query_args['post_name__in'] = [ $query_args['post_name'] ];
807+
unset( $query_args['post_name'] );
808+
}
809+
810+
// The date columns reach WP_Query only through a date query. Bounding the
811+
// range by the given value on both ends matches it as precisely as it was
812+
// written: a full timestamp matches that second, a date matches that day.
813+
$date_query = [];
814+
815+
foreach ( [ 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt' ] as $column ) {
816+
if ( ! isset( $query_args[ $column ] ) ) {
817+
continue;
818+
}
819+
820+
$date_query[] = [
821+
'column' => $column,
822+
'after' => $query_args[ $column ],
823+
'before' => $query_args[ $column ],
824+
'inclusive' => true,
825+
];
826+
827+
unset( $query_args[ $column ] );
828+
}
829+
830+
// A '--date_query' of its own is kept rather than replaced, and nested a level
831+
// down rather than appended to, because its 'relation' governs whatever shares
832+
// its list: an 'OR' would reach the clauses added here and match a post that
833+
// satisfies neither half of what was asked for.
834+
if ( ! empty( $date_query ) ) {
835+
$given = isset( $query_args['date_query'] ) && is_array( $query_args['date_query'] )
836+
? $query_args['date_query']
837+
: [];
838+
839+
$query_args['date_query'] = empty( $given )
840+
? $date_query
841+
: array_merge( [ 'relation' => 'AND', $given ], $date_query );
842+
}
843+
733844
if ( 'ids' === $formatter->format ) {
734845
$query_args['fields'] = 'ids';
735846
$query = new WP_Query( $query_args );

0 commit comments

Comments
 (0)