Wp post list (WP-CLI)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

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
  • Option --posts_per_page is supported, but not much documented
  • Option --offset is supported, but not much documented.
$ wp help post list

NAME

  wp post list

DESCRIPTION

  Gets a list of posts.

SYNOPSIS

  wp post list [--<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     |
    +----+--------------+-------------+---------------------+-------------+

Voorbeelden

wp post list # This lists "posts" only

+----+--------------+-------------+---------------------+-------------+
| ID | post_title   | post_name   | post_date           | post_status |
+----+--------------+-------------+---------------------+-------------+
| 1  | Hello world! | hello-world | 2017-11-26 11:11:28 | publish     |
+----+--------------+-------------+---------------------+-------------+
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     |
+----+---------------+---------------+---------------------+-------------+
# 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;

Zie ook