Skip to content

Commit ce66cd7

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

3 files changed

Lines changed: 463 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 137 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>] [--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>]
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>] [--cat=<cat>] [--category_name=<category_name>] [--tag=<tag>] [--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>] [--hour=<hour>] [--minute=<minute>] [--second=<second>] [--m=<yearmonth>] [--w=<week>] [--meta_key=<meta_key>] [--meta_value=<meta_value>] [--orderby=<orderby>] [--order=<order>] [--post__in=<ids>] [--post__not_in=<ids>] [--post_name__in=<slugs>] [--post_parent__in=<ids>] [--post_parent__not_in=<ids>] [--author__in=<ids>] [--author__not_in=<ids>] [--category__in=<ids>] [--category__and=<ids>] [--category__not_in=<ids>] [--tag_id=<tag_id>] [--tag__in=<ids>] [--tag__and=<ids>] [--tag__not_in=<ids>] [--tag_slug__in=<slugs>] [--tag_slug__and=<slugs>] [--page_id=<page_id>] [--pagename=<pagename>] [--attachment_id=<attachment_id>] [--date_query=<json>] [--meta_query=<json>] [--tax_query=<json>] [--meta_compare=<meta_compare>] [--sentence=<sentence>] [--exact=<exact>] [--search_columns=<columns>] [--perm=<perm>] [--posts_per_page=<number>] [--paged=<paged>] [--offset=<offset>] [--nopaging=<nopaging>] [--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/).
@@ -3186,6 +3186,17 @@ Only shows post types marked as post by default.
31863186
[--author_name=<author_name>]
31873187
Filter by the 'user_nicename' of the post's author.
31883188

3189+
[--cat=<cat>]
3190+
Filter by category ID. Accepts a comma-separated list to match any of
3191+
them, and a negative ID excludes that category instead.
3192+
3193+
[--category_name=<category_name>]
3194+
Filter by category slug.
3195+
3196+
[--tag=<tag>]
3197+
Filter by tag slug. Accepts a comma-separated list to match any of them,
3198+
or a '+'-separated list to require all of them.
3199+
31893200
[--post_type=<post_type>]
31903201
Filter by post type. Defaults to 'post'. Accepts a comma-separated list,
31913202
or 'any' for every type registered without 'exclude_from_search'.
@@ -3225,12 +3236,137 @@ Only shows post types marked as post by default.
32253236
[--day=<day>]
32263237
Filter by day of the month, 1 to 31.
32273238

3239+
[--hour=<hour>]
3240+
Filter by hour, 0 to 23.
3241+
3242+
[--minute=<minute>]
3243+
Filter by minute, 0 to 59.
3244+
3245+
[--second=<second>]
3246+
Filter by second, 0 to 59.
3247+
32283248
[--m=<yearmonth>]
32293249
Filter by year and month together, e.g. 202401.
32303250

32313251
[--w=<week>]
32323252
Filter by week of the year, 0 to 53.
32333253

3254+
[--meta_key=<meta_key>]
3255+
Filter by posts having this meta key. Pair it with `--meta_value` to
3256+
filter on the value as well.
3257+
3258+
[--meta_value=<meta_value>]
3259+
Filter by this meta value. Needs `--meta_key` to say which key it
3260+
belongs to.
3261+
3262+
[--orderby=<orderby>]
3263+
Order the results by this field. Accepts what WP_Query accepts, e.g.
3264+
'date', 'title', 'ID', 'menu_order', 'rand', or 'meta_value' alongside
3265+
`--meta_key`.
3266+
3267+
[--order=<order>]
3268+
Direction to order by. Accepts 'ASC' or 'DESC'.
3269+
3270+
[--post__in=<ids>]
3271+
Only list the posts with these IDs (comma-separated).
3272+
3273+
[--post__not_in=<ids>]
3274+
Exclude the posts with these IDs (comma-separated).
3275+
3276+
[--post_name__in=<slugs>]
3277+
Only list the posts with these slugs (comma-separated). Unlike `--name`
3278+
this is not a single-post query, so it reaches drafts as well.
3279+
3280+
[--post_parent__in=<ids>]
3281+
Only list the posts whose parent is one of these IDs (comma-separated).
3282+
3283+
[--post_parent__not_in=<ids>]
3284+
Exclude the posts whose parent is one of these IDs (comma-separated).
3285+
3286+
[--author__in=<ids>]
3287+
Only list the posts by these author IDs (comma-separated).
3288+
3289+
[--author__not_in=<ids>]
3290+
Exclude the posts by these author IDs (comma-separated).
3291+
3292+
[--category__in=<ids>]
3293+
Only list the posts in these category IDs (comma-separated).
3294+
3295+
[--category__and=<ids>]
3296+
Only list the posts in all of these category IDs (comma-separated).
3297+
3298+
[--category__not_in=<ids>]
3299+
Exclude the posts in these category IDs (comma-separated).
3300+
3301+
[--tag_id=<tag_id>]
3302+
Filter by tag ID.
3303+
3304+
[--tag__in=<ids>]
3305+
Only list the posts with these tag IDs (comma-separated).
3306+
3307+
[--tag__and=<ids>]
3308+
Only list the posts with all of these tag IDs (comma-separated).
3309+
3310+
[--tag__not_in=<ids>]
3311+
Exclude the posts with these tag IDs (comma-separated).
3312+
3313+
[--tag_slug__in=<slugs>]
3314+
Only list the posts with these tag slugs (comma-separated).
3315+
3316+
[--tag_slug__and=<slugs>]
3317+
Only list the posts with all of these tag slugs (comma-separated).
3318+
3319+
[--page_id=<page_id>]
3320+
Filter by page ID. Needs `--post_type=page` to match anything.
3321+
3322+
[--pagename=<pagename>]
3323+
Filter by page slug. Needs `--post_type=page` to match anything.
3324+
3325+
[--attachment_id=<attachment_id>]
3326+
Filter by attachment ID. Needs `--post_type=attachment` to match
3327+
anything.
3328+
3329+
[--date_query=<json>]
3330+
Filter by a date query, given as JSON. See WP_Date_Query.
3331+
3332+
[--meta_query=<json>]
3333+
Filter by a meta query, given as JSON. See WP_Meta_Query.
3334+
3335+
[--tax_query=<json>]
3336+
Filter by a taxonomy query, given as JSON. See WP_Tax_Query.
3337+
3338+
[--meta_compare=<meta_compare>]
3339+
Operator to test `--meta_value` with, e.g. '=', '!=', '>' or 'LIKE'.
3340+
3341+
[--sentence=<sentence>]
3342+
Match `--s` as one phrase rather than as separate words. Accepts 1 or 0.
3343+
3344+
[--exact=<exact>]
3345+
Match `--s` against the whole column rather than part of it. Accepts 1
3346+
or 0.
3347+
3348+
[--search_columns=<columns>]
3349+
Comma-separated list of columns `--s` looks in. Accepts 'post_title',
3350+
'post_excerpt' and 'post_content'. Needs WordPress 6.2 or later.
3351+
3352+
[--perm=<perm>]
3353+
Filter by what the current user may do with the post. Accepts
3354+
'readable' or 'editable'; pair it with the global `--user` argument,
3355+
since WP-CLI is no user by default.
3356+
3357+
[--posts_per_page=<number>]
3358+
How many posts to return. Defaults to -1, meaning every post.
3359+
3360+
[--paged=<paged>]
3361+
Which page of results to return, counted in `--posts_per_page` steps.
3362+
3363+
[--offset=<offset>]
3364+
How many posts to skip. Needs `--posts_per_page` set to something other
3365+
than -1, which otherwise takes precedence.
3366+
3367+
[--nopaging=<nopaging>]
3368+
Return every post, ignoring `--posts_per_page`. Accepts 1 or 0.
3369+
32343370
[--field=<field>]
32353371
Prints the value of a single field for each post.
32363372

‎features/post.feature‎

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,193 @@ Feature: Manage WordPress posts
688688
"""
689689
{DOOMED_ID}
690690
"""
691+
692+
Scenario: Filtering by category and tag
693+
When I run `wp term create category 'Alpha Cat' --porcelain`
694+
Then save STDOUT as {CAT_ID}
695+
696+
When I run `wp term create post_tag 'Alpha Tag' --slug=alpha-tag --porcelain`
697+
Then save STDOUT as {TAG_ID}
698+
699+
When I run `wp post create --post_title='Tagged' --post_status=publish --post_category={CAT_ID} --tags_input=alpha-tag --porcelain`
700+
Then STDOUT should be a number
701+
702+
When I run `wp post list --cat={CAT_ID} --format=count`
703+
Then STDOUT should be:
704+
"""
705+
1
706+
"""
707+
708+
# A negative ID excludes that category rather than selecting it.
709+
When I run `wp post list --cat=-{CAT_ID} --field=post_title`
710+
Then STDOUT should not contain:
711+
"""
712+
Tagged
713+
"""
714+
715+
When I run `wp post list --tag=alpha-tag --format=count`
716+
Then STDOUT should be:
717+
"""
718+
1
719+
"""
720+
721+
When I run `wp post list --category_name=alpha-cat --format=count`
722+
Then STDOUT should be:
723+
"""
724+
1
725+
"""
726+
727+
Scenario: Filtering by meta, time of day and order
728+
When I run `wp post create --post_title='Timed' --post_status=publish --post_date='2020-03-04 05:06:07' --porcelain`
729+
Then STDOUT should be a number
730+
And save STDOUT as {TIMED_ID}
731+
732+
When I run `wp post meta add {TIMED_ID} color blue`
733+
Then STDOUT should not be empty
734+
735+
When I run `wp post create --post_title='Locked' --post_status=publish --post_password=secret --porcelain`
736+
Then STDOUT should be a number
737+
738+
# Time of day, alongside the year/month/day filters already documented.
739+
# Two units at once exercises the combined path; all three of hour, minute
740+
# and second together is left out on purpose, because that path compares a
741+
# DATE_FORMAT() string and the SQLite integration plugin does not emulate
742+
# it the way MySQL does, so it matches nothing there.
743+
When I run `wp post list --hour=5 --minute=6 --field=post_title`
744+
Then STDOUT should be:
745+
"""
746+
Timed
747+
"""
748+
749+
When I run `wp post list --second=7 --field=post_title`
750+
Then STDOUT should be:
751+
"""
752+
Timed
753+
"""
754+
755+
# A meta key on its own, then narrowed by its value.
756+
When I run `wp post list --meta_key=color --field=post_title`
757+
Then STDOUT should be:
758+
"""
759+
Timed
760+
"""
761+
762+
When I run `wp post list --meta_key=color --meta_value=red --format=count`
763+
Then STDOUT should be:
764+
"""
765+
0
766+
"""
767+
768+
When I run `wp post list --orderby=title --order=ASC --field=post_title`
769+
Then STDOUT should be:
770+
"""
771+
Hello world!
772+
Locked
773+
Timed
774+
"""
775+
776+
When I run `wp post list --orderby=title --order=DESC --field=post_title`
777+
Then STDOUT should be:
778+
"""
779+
Timed
780+
Locked
781+
Hello world!
782+
"""
783+
784+
Scenario: Filtering by lists and JSON queries
785+
When I run `wp post create --post_title='Zebra Title' --post_content='nothing' --post_status=publish --porcelain`
786+
Then save STDOUT as {TITLED}
787+
788+
When I run `wp post create --post_title='Plain' --post_name=plain --post_content='zebra in body' --post_status=publish --porcelain`
789+
Then save STDOUT as {BODIED}
790+
791+
When I run `wp post meta add {TITLED} rank 5`
792+
Then STDOUT should not be empty
793+
794+
# Arguments carrying '__' are split on commas before they reach WP_Query,
795+
# which is what makes them usable from the command line at all.
796+
When I run `wp post list --post__in={TITLED},{BODIED} --format=count`
797+
Then STDOUT should be:
798+
"""
799+
2
800+
"""
801+
802+
When I run `wp post list --post__not_in={TITLED},{BODIED} --format=count`
803+
Then STDOUT should be:
804+
"""
805+
1
806+
"""
807+
808+
# Unlike --name, this reaches a draft, because it is not a single-post query.
809+
When I run `wp post list --post_name__in=plain --field=post_title`
810+
Then STDOUT should be:
811+
"""
812+
Plain
813+
"""
814+
815+
When I run `wp post list --s=zebra --format=count`
816+
Then STDOUT should be:
817+
"""
818+
2
819+
"""
820+
821+
# The nested queries are given as JSON, which this command decodes.
822+
When I run `wp post list --meta_query='[{"key":"rank","value":"5"}]' --field=post_title`
823+
Then STDOUT should be:
824+
"""
825+
Zebra Title
826+
"""
827+
828+
When I run `wp post list --meta_key=rank --meta_value=4 --meta_compare=">" --field=post_title`
829+
Then STDOUT should be:
830+
"""
831+
Zebra Title
832+
"""
833+
834+
# --offset only means anything once --posts_per_page is bounded.
835+
When I run `wp post list --posts_per_page=10 --offset=1 --format=count`
836+
Then STDOUT should be:
837+
"""
838+
2
839+
"""
840+
841+
When I run `wp post list --posts_per_page=1 --nopaging=1 --format=count`
842+
Then STDOUT should be:
843+
"""
844+
3
845+
"""
846+
847+
# 'search_columns' is a WP_Query argument as of WordPress 6.2. Before that it
848+
# is not recognised, so '--s' searches every column and the narrowing here
849+
# would not happen.
850+
@require-wp-6.2
851+
Scenario: Narrowing a search to particular columns
852+
When I run `wp post create --post_title='Zebra Title' --post_content='nothing' --post_status=publish --porcelain`
853+
Then STDOUT should be a number
854+
855+
When I run `wp post create --post_title='Plain' --post_content='zebra in body' --post_status=publish --porcelain`
856+
Then STDOUT should be a number
857+
858+
When I run `wp post list --s=zebra --format=count`
859+
Then STDOUT should be:
860+
"""
861+
2
862+
"""
863+
864+
When I run `wp post list --s=zebra --search_columns=post_title --field=post_title`
865+
Then STDOUT should be:
866+
"""
867+
Zebra Title
868+
"""
869+
870+
When I run `wp post list --s=zebra --search_columns=post_content --field=post_title`
871+
Then STDOUT should be:
872+
"""
873+
Plain
874+
"""
875+
876+
When I run `wp post list --s=zebra --search_columns=post_title,post_content --format=count`
877+
Then STDOUT should be:
878+
"""
879+
2
880+
"""

0 commit comments

Comments
 (0)