Skip to content

Commit 8cf4ad9

Browse files
committed
Document the WP_Query arguments wp post list accepts
The command passes what it is given to WP_Query, so a good deal works that nothing mentions. This documents the arguments that filter on what the command displays, the way #642 did for `wp site list`. Three of the fields it displays are named after the wp_posts column rather than the WP_Query argument that filters on it, and passing the column name reached WP_Query as an argument it does not know: it was dropped, and every post came back - a filter that reads as if it works and quietly does not. $ wp post list --post_title='Hello world!' --format=count 2 wp-cli resolves parameter aliases before a command runs, so declaring them in the synopsis is enough to make the column spellings work: - --title, with --post_title as an alias - --name, with --post_name as an alias - --author, with --post_author as an alias - --p, with --ID as an alias Everything else here already filtered and is only being written down. Each one was checked against WP_Query rather than assumed: 'post__in', 'post_name__in' and 'author__in' are left out because they are only read as arrays, so a comma-separated value would quietly match on the first entry alone, and making them usable is more than documentation. '--name' keeps WP_Query's behaviour of making the query a single-post one, which returns a draft only to a user who can edit it. Running as no user, WP-CLI cannot, so the note says to filter drafts another way rather than rerouting the argument to mean something WP_Query does not. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SSZzqMJRDTiLiDxQEPYcL
1 parent 5dc9619 commit 8cf4ad9

3 files changed

Lines changed: 195 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 69 additions & 2 deletions
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>] [--p=<id>|ID] [--title=<title>|post_title] [--name=<slug>|post_name] [--author=<author>|post_author] [--author_name=<author_name>] [--post_type=<post_type>] [--post_status=<post_status>] [--post_parent=<post_parent>] [--post_mime_type=<post_mime_type>] [--menu_order=<menu_order>] [--comment_status=<comment_status>] [--ping_status=<ping_status>] [--comment_count=<comment_count>] [--s=<string>] [--year=<year>] [--monthnum=<monthnum>] [--day=<day>] [--m=<yearmonth>] [--w=<week>] [--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/).
@@ -3159,7 +3159,74 @@ Only shows post types marked as post by default.
31593159
**OPTIONS**
31603160

31613161
[--<field>=<value>]
3162-
One or more args to pass to WP_Query.
3162+
One or more args to pass to WP_Query. The arguments below are the ones
3163+
that filter on what this command displays; anything else WP_Query accepts
3164+
still works and is documented with WP_Query itself.
3165+
3166+
[--p=<id>|ID]
3167+
Filter by post ID. `--ID` is the name of the column this filters and is
3168+
accepted as an alias.
3169+
3170+
[--title=<title>|post_title]
3171+
Filter by post title, matched in full. `--post_title` is the name of the
3172+
column this filters and is accepted as an alias.
3173+
3174+
[--name=<slug>|post_name]
3175+
Filter by post slug. `--post_name` is the name of the column this filters
3176+
and is accepted as an alias.
3177+
Note: this makes the query a single-post one, and WP_Query only returns a
3178+
draft from one of those to a user who can edit it - which, running as no
3179+
user, WP-CLI cannot. Pass `--post_status=draft` with `--author` instead to
3180+
find drafts.
3181+
3182+
[--author=<author>|post_author]
3183+
Filter by the ID of the post's author. `--post_author` is the name of the
3184+
column this filters and is accepted as an alias.
3185+
3186+
[--author_name=<author_name>]
3187+
Filter by the 'user_nicename' of the post's author.
3188+
3189+
[--post_type=<post_type>]
3190+
Filter by post type. Defaults to 'post'; pass 'any' for every type.
3191+
3192+
[--post_status=<post_status>]
3193+
Filter by post status. Pass 'any' for every status.
3194+
3195+
[--post_parent=<post_parent>]
3196+
Filter by the ID of the parent post.
3197+
3198+
[--post_mime_type=<post_mime_type>]
3199+
Filter by MIME type. Only attachments carry one.
3200+
3201+
[--menu_order=<menu_order>]
3202+
Filter by menu order.
3203+
3204+
[--comment_status=<comment_status>]
3205+
Filter by comment status. Accepts 'open' or 'closed'.
3206+
3207+
[--ping_status=<ping_status>]
3208+
Filter by ping status. Accepts 'open' or 'closed'.
3209+
3210+
[--comment_count=<comment_count>]
3211+
Filter by number of comments.
3212+
3213+
[--s=<string>]
3214+
Only list the posts matching this search term.
3215+
3216+
[--year=<year>]
3217+
Filter by four-digit year, e.g. 2024.
3218+
3219+
[--monthnum=<monthnum>]
3220+
Filter by month number, 1 to 12.
3221+
3222+
[--day=<day>]
3223+
Filter by day of the month, 1 to 31.
3224+
3225+
[--m=<yearmonth>]
3226+
Filter by year and month together, e.g. 202401.
3227+
3228+
[--w=<week>]
3229+
Filter by week of the year, 0 to 53.
31633230

31643231
[--field=<field>]
31653232
Prints the value of a single field for each post.

‎features/post.feature‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,61 @@ Feature: Manage WordPress posts
591591
"""
592592
{"block_version":1}
593593
"""
594+
595+
Scenario: Filtering by the wp_posts column names
596+
When I run `wp post create --post_title='Alpha' --post_status=publish --porcelain`
597+
Then STDOUT should be a number
598+
599+
# 'title', 'name' and 'author' are WP_Query's names for these filters. The
600+
# columns they filter on are spelled differently, and passing the column
601+
# name used to reach WP_Query as an argument it does not know: it was
602+
# dropped, and every post came back. They are aliases now.
603+
When I run `wp post list --title='Hello world!' --format=count`
604+
Then STDOUT should be:
605+
"""
606+
1
607+
"""
608+
609+
When I run `wp post list --post_title='Hello world!' --format=count`
610+
Then STDOUT should be:
611+
"""
612+
1
613+
"""
614+
615+
When I run `wp post list --name=alpha --format=count`
616+
Then STDOUT should be:
617+
"""
618+
1
619+
"""
620+
621+
When I run `wp post list --post_name=alpha --format=count`
622+
Then STDOUT should be:
623+
"""
624+
1
625+
"""
626+
627+
# Only the bundled post matches: a post created by WP-CLI has author 0,
628+
# because WP-CLI runs as no user unless told otherwise.
629+
When I run `wp post list --author=1 --format=count`
630+
Then STDOUT should be:
631+
"""
632+
1
633+
"""
634+
635+
When I run `wp post list --post_author=1 --format=count`
636+
Then STDOUT should be:
637+
"""
638+
1
639+
"""
640+
641+
When I run `wp post list --p=1 --format=count`
642+
Then STDOUT should be:
643+
"""
644+
1
645+
"""
646+
647+
When I run `wp post list --ID=1 --format=count`
648+
Then STDOUT should be:
649+
"""
650+
1
651+
"""

‎src/Post_Command.php‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,74 @@ protected function delete_callback( $post_id, $assoc_args ) {
617617
* ## OPTIONS
618618
*
619619
* [--<field>=<value>]
620-
* : One or more args to pass to WP_Query.
620+
* : One or more args to pass to WP_Query. The arguments below are the ones
621+
* that filter on what this command displays; anything else WP_Query accepts
622+
* still works and is documented with WP_Query itself.
623+
*
624+
* [--p=<id>|ID]
625+
* : Filter by post ID. `--ID` is the name of the column this filters and is
626+
* accepted as an alias.
627+
*
628+
* [--title=<title>|post_title]
629+
* : Filter by post title, matched in full. `--post_title` is the name of the
630+
* column this filters and is accepted as an alias.
631+
*
632+
* [--name=<slug>|post_name]
633+
* : Filter by post slug. `--post_name` is the name of the column this filters
634+
* and is accepted as an alias.
635+
* Note: this makes the query a single-post one, and WP_Query only returns a
636+
* draft from one of those to a user who can edit it - which, running as no
637+
* user, WP-CLI cannot. Pass `--post_status=draft` with `--author` instead to
638+
* find drafts.
639+
*
640+
* [--author=<author>|post_author]
641+
* : Filter by the ID of the post's author. `--post_author` is the name of the
642+
* column this filters and is accepted as an alias.
643+
*
644+
* [--author_name=<author_name>]
645+
* : Filter by the 'user_nicename' of the post's author.
646+
*
647+
* [--post_type=<post_type>]
648+
* : Filter by post type. Defaults to 'post'; pass 'any' for every type.
649+
*
650+
* [--post_status=<post_status>]
651+
* : Filter by post status. Pass 'any' for every status.
652+
*
653+
* [--post_parent=<post_parent>]
654+
* : Filter by the ID of the parent post.
655+
*
656+
* [--post_mime_type=<post_mime_type>]
657+
* : Filter by MIME type. Only attachments carry one.
658+
*
659+
* [--menu_order=<menu_order>]
660+
* : Filter by menu order.
661+
*
662+
* [--comment_status=<comment_status>]
663+
* : Filter by comment status. Accepts 'open' or 'closed'.
664+
*
665+
* [--ping_status=<ping_status>]
666+
* : Filter by ping status. Accepts 'open' or 'closed'.
667+
*
668+
* [--comment_count=<comment_count>]
669+
* : Filter by number of comments.
670+
*
671+
* [--s=<string>]
672+
* : Only list the posts matching this search term.
673+
*
674+
* [--year=<year>]
675+
* : Filter by four-digit year, e.g. 2024.
676+
*
677+
* [--monthnum=<monthnum>]
678+
* : Filter by month number, 1 to 12.
679+
*
680+
* [--day=<day>]
681+
* : Filter by day of the month, 1 to 31.
682+
*
683+
* [--m=<yearmonth>]
684+
* : Filter by year and month together, e.g. 202401.
685+
*
686+
* [--w=<week>]
687+
* : Filter by week of the year, 0 to 53.
621688
*
622689
* [--field=<field>]
623690
* : Prints the value of a single field for each post.

0 commit comments

Comments
 (0)