Wp post list (WP-CLI)
wp post list: Retrieve a list of posts/pages/etc.
- Without additional specifications, this command is limited to posts only - Pages, products, etc. will be excluded. Do note however, that ID is global. So this problem doesn't occur when editing stuff
- Output is not limited to e.g., 100 lines
- Option
--posts_per_page
is supported, but not much documented - Option
--offset
is supported, but not much documented - Option
--post_type
is supported, but not documented throughwp help post list
- I included it myself below
$ wp help post list NAME wp post list DESCRIPTION Gets a list of posts. SYNOPSIS wp post list [--post_type=<value>] [--<field>=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>] Display posts based on all arguments supported by [WP_Query()][1]. --- [1] https://developer.wordpress.org/reference/classes/wp_query/ OPTIONS [--<field>=<value>] One or more args to pass to WP_Query. [--field=<field>] Prints the value of a single field for each post. [--fields=<fields>] Limit the output to specific object fields. [--format=<format>] Render output in a particular format. --- default: table options: - table - csv - ids - json - count - yaml --- AVAILABLE FIELDS These fields will be displayed by default for each post: * ID * post_title * post_name * post_date * post_status These fields are optionally available: * post_author * post_date_gmt * post_content * post_excerpt * comment_status * ping_status * post_password * to_ping * pinged * post_modified * post_modified_gmt * post_content_filtered * post_parent * guid * menu_order * post_type * post_mime_type * comment_count * filter * url EXAMPLES # List post $ wp post list --field=ID 568 829 1329 1695 # List posts in JSON $ wp post list --post_type=post --posts_per_page=5 --format=json [{"ID":1,"post_title":"Hello world!","post_name":"hello-world","post_date":"2015-06-20 09:00:10","post_status":"publish"},{"ID":1178,"post_title":"Markup: HTML Tags and Formatting","post_name":"markup-html-tags-and-formatting","post_date":"2013-01-11 20:22:19","post_status":"draft"}] # List all pages $ wp post list --post_type=page --fields=post_title,post_status +-------------+-------------+ | post_title | post_status | +-------------+-------------+ | Sample Page | publish | +-------------+-------------+ # List ids of all pages and posts $ wp post list --post_type=page,post --format=ids 15 25 34 37 198 # List given posts $ wp post list --post__in=1,3 +----+--------------+-------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+--------------+-------------+---------------------+-------------+ | 3 | Lorem Ipsum | lorem-ipsum | 2016-06-01 14:34:36 | publish | | 1 | Hello world! | hello-world | 2016-06-01 14:31:12 | publish | +----+--------------+-------------+---------------------+-------------+
Default behaviour
Without any additional arguments, wp post list
limits itself to posts, not pages or any other kind of object:
wp post list # This will list "posts", not "pages"! +----+--------------+-------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+--------------+-------------+---------------------+-------------+ | 1 | Hello world! | hello-world | 2017-11-26 11:11:28 | publish | +----+--------------+-------------+---------------------+-------------+
List pages
To retrieve pages rather than posts, use the --post_type
option:
wp post list --post_type=page +----+---------------+---------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+---------------+---------------+---------------------+-------------+ | 7 | test (3) | | 2017-11-26 12:26:42 | draft | | 6 | test (2) | test-2 | 2017-11-26 12:23:05 | publish | | 2 | Sample Page | sample-page | 2017-11-26 11:11:28 | publish | +----+---------------+---------------+---------------------+-------------+
List products
# Show products # wp post list --post_type=product
# Je kunt filteren op post_name, maar niet op post_title # wp post list --post_type=page --name="test-3" --field=ID 8
# Redirect output naar een variable: Gebruik i=$(...) # i=$(wp post list --post_type=page --name="test-3" --field=ID) echo "gevonden id: $i"
# When selecting multiple fields, there is no space after the comma # wp post list --post_type="page" --fields=ID,post_title,post_name +-------+---------------------------------------------------------------------------+--------------------------------------------------------------------+ | ID | post_title | post_name | +-------+---------------------------------------------------------------------------+--------------------------------------------------------------------+ | 63472 | Widgets met kortste afmeting groter dan 10 mm | Widgets-met-kortste-afmeting-groter-dan10-mm | | 63469 | Widgets met kortste afmeting 10mm | Widgets-met-kortste-afmeting-10mm | | 63467 | Widgets met kortste afmeting tussen 8 & 10 mm | Widgets-met-kortste-afmeting-tussen-8-10-mm | | 63252 | Alle afmetingen | | | 63239 | Widgets met kortste afmeting tot 5mm | Widgets-met-kortste-afmeting-tot-5mm | | 63233 | Widgets 5x8mm - Zonder stroomdraad, zonder connector - With spring | Widgets-5x8mm-zonder-stroomdraad-zonder-connector-met-veer | | 51513 | Zoeken op afbeelding - Diagram 93 | zoeken-op-afbeelding-diagram-93 | | 51507 | Zoeken op afbeelding - Diagram 79 | zoeken-op-afbeelding-diagram-79 | | 51167 | Widgets for Vritex - Kies je Toepassing | Widgets-voor-vritex-kies-je-apparaat | | 51165 | Widgets for Styr - Kies je Toepassing | Widgets-voor-styr-kies-je-apparaat | | 51163 | Widgets for Spt - Kies je Toepassing | Widgets-voor-spt-kies-je-apparaat | | 413 | Shop | shop | | 393 | Zoeken op Afbeelding | zoeken-op-afbeelding | | 181 | Over ons | over-ons | | 101 | Contact | contact | | 11 | Winkelmand | winkelmand | | 12 | Checkout | checkout | | 13 | Mijn account | mijn-account | | 7 | Webshop for Widgets | webshop-widgets | +-------+---------------------------------------------------------------------------+--------------------------------------------------------------------+
# Use of "--posts_per_page" & "--offset": # wp post list --post_type="page" --posts_per_page=5 --offset=12;
More about --post_type
The --post_type
parameter of the wp post list command in WP-CLI specifies which post types to include in the result. The possible values for this parameter are based on the post types registered on your WordPress site. By default, WordPress includes several built-in post types:
- post: Standard blog posts.
- page: Pages, which are hierarchical and can have child pages.
- attachment: Media items, like images and videos, added to your media library.
- revision: Revisions of posts or pages.
- nav_menu_item: Items that are part of your navigation menus.
- custom_css: Custom CSS snippets.
- customize_changeset: Changesets for the WordPress Customizer.
- oembed_cache: Cached oEmbed post types.
- user_request: User data request.
- wp_block: Reusable blocks (introduced with the Gutenberg editor).
In addition to these default post types, WordPress themes and plugins can register their own custom post types. For example, many e-commerce plugins create a product post type, while event plugins might register an event post type.
To get a list of all registered post types on your specific WordPress installation, you can use this WP-CLI command:
wp post-type list --field=name
This will give you a list of all post types, both built-in and custom ones, that are currently registered on your site.
Some general examples concerning --post_type
:
wp post list --post_type=page,product,attachment
wp post list --post_type=any
Get a specific file (attachment)
With
wp post list --post_type=attachment
all attachments (including images) are returned.
I want to get a pdf form. The URL of the page is formulier-voor-herroeping
. As far as I am concerned, I can't use wildcards here:
wp post list --post_type="attachment" --name="formulier-voor-herroeping"
And as the final step: I only need to retrieve the ID:
wp post list --post_type="attachment" --name="formulier-voor-herroeping" --field=ID
Retrieve products
Fortunately, no limit to the number of outputted items
wp post list --post_type=product
Only retieve IDs:
wp post list --post_type=product --field=ID
How many objects are there?
The [--format=<format>]
has count
as one of its options. In that case, it will return the number of objects:
$ wp post list --post_type=product --format=count 17109