Skip to content

Commit fcf429a

Browse files
swissspidyclaude
andauthored
Document the WP_Query arguments wp post list accepts (#643)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5dc9619 commit fcf429a

3 files changed

Lines changed: 240 additions & 3 deletions

File tree

README.md

Lines changed: 72 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,77 @@ 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 returns a draft
3178+
from one of those only to a user who can edit it. WP-CLI runs as no user
3179+
unless the global `--user` argument says otherwise, so pass that to filter
3180+
drafts by slug.
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'. Accepts a comma-separated list,
3191+
or 'any' for every type registered without 'exclude_from_search'.
3192+
3193+
[--post_status=<post_status>]
3194+
Filter by post status. Defaults to 'any', which is every status
3195+
registered without 'exclude_from_search' - so trashed and auto-draft posts
3196+
are left out until asked for by name, e.g. `--post_status=trash`.
3197+
3198+
[--post_parent=<post_parent>]
3199+
Filter by the ID of the parent post.
3200+
3201+
[--post_mime_type=<post_mime_type>]
3202+
Filter by MIME type. Only attachments carry one.
3203+
3204+
[--menu_order=<menu_order>]
3205+
Filter by menu order.
3206+
3207+
[--comment_status=<comment_status>]
3208+
Filter by comment status. Accepts 'open' or 'closed'.
3209+
3210+
[--ping_status=<ping_status>]
3211+
Filter by ping status. Accepts 'open' or 'closed'.
3212+
3213+
[--comment_count=<comment_count>]
3214+
Filter by number of comments.
3215+
3216+
[--s=<string>]
3217+
Only list the posts matching this search term.
3218+
3219+
[--year=<year>]
3220+
Filter by four-digit year, e.g. 2024.
3221+
3222+
[--monthnum=<monthnum>]
3223+
Filter by month number, 1 to 12.
3224+
3225+
[--day=<day>]
3226+
Filter by day of the month, 1 to 31.
3227+
3228+
[--m=<yearmonth>]
3229+
Filter by year and month together, e.g. 202401.
3230+
3231+
[--w=<week>]
3232+
Filter by week of the year, 0 to 53.
31633233

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

features/post.feature

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,100 @@ 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+
"""
652+
653+
Scenario: Filtering drafts by slug needs a user
654+
When I run `wp post create --post_title='Beta' --post_name=beta --post_status=draft --porcelain`
655+
Then STDOUT should be a number
656+
657+
# '--name' makes this a single-post query, and WP_Query hands a draft from
658+
# one of those only to a user who can edit it. WP-CLI is no user by default.
659+
When I run `wp post list --name=beta --field=ID`
660+
Then STDOUT should be empty
661+
662+
# The global '--user' argument is what makes it reachable.
663+
When I run `wp post list --name=beta --user=1 --field=ID`
664+
Then STDOUT should not be empty
665+
666+
Scenario: Trashed posts need their status named
667+
When I run `wp post create --post_title='Doomed' --post_status=publish --porcelain`
668+
Then STDOUT should be a number
669+
And save STDOUT as {DOOMED_ID}
670+
671+
When I run `wp post delete {DOOMED_ID}`
672+
Then STDOUT should contain:
673+
"""
674+
Success: Trashed post
675+
"""
676+
677+
# This command defaults post_status to 'any', and WP_Query reads 'any' as
678+
# every status registered without 'exclude_from_search' - which leaves out
679+
# 'trash' and 'auto-draft'.
680+
When I run `wp post list --field=ID`
681+
Then STDOUT should not contain:
682+
"""
683+
{DOOMED_ID}
684+
"""
685+
686+
When I run `wp post list --post_status=trash --field=ID`
687+
Then STDOUT should contain:
688+
"""
689+
{DOOMED_ID}
690+
"""

src/Post_Command.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,77 @@ 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 returns a draft
636+
* from one of those only to a user who can edit it. WP-CLI runs as no user
637+
* unless the global `--user` argument says otherwise, so pass that to filter
638+
* drafts by slug.
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'. Accepts a comma-separated list,
649+
* or 'any' for every type registered without 'exclude_from_search'.
650+
*
651+
* [--post_status=<post_status>]
652+
* : Filter by post status. Defaults to 'any', which is every status
653+
* registered without 'exclude_from_search' - so trashed and auto-draft posts
654+
* are left out until asked for by name, e.g. `--post_status=trash`.
655+
*
656+
* [--post_parent=<post_parent>]
657+
* : Filter by the ID of the parent post.
658+
*
659+
* [--post_mime_type=<post_mime_type>]
660+
* : Filter by MIME type. Only attachments carry one.
661+
*
662+
* [--menu_order=<menu_order>]
663+
* : Filter by menu order.
664+
*
665+
* [--comment_status=<comment_status>]
666+
* : Filter by comment status. Accepts 'open' or 'closed'.
667+
*
668+
* [--ping_status=<ping_status>]
669+
* : Filter by ping status. Accepts 'open' or 'closed'.
670+
*
671+
* [--comment_count=<comment_count>]
672+
* : Filter by number of comments.
673+
*
674+
* [--s=<string>]
675+
* : Only list the posts matching this search term.
676+
*
677+
* [--year=<year>]
678+
* : Filter by four-digit year, e.g. 2024.
679+
*
680+
* [--monthnum=<monthnum>]
681+
* : Filter by month number, 1 to 12.
682+
*
683+
* [--day=<day>]
684+
* : Filter by day of the month, 1 to 31.
685+
*
686+
* [--m=<yearmonth>]
687+
* : Filter by year and month together, e.g. 202401.
688+
*
689+
* [--w=<week>]
690+
* : Filter by week of the year, 0 to 53.
621691
*
622692
* [--field=<field>]
623693
* : Prints the value of a single field for each post.

0 commit comments

Comments
 (0)