In this article let’s learn about how to execute the WP_Query into the WP CLI commands.
Note: If you read some recent articles then you can skip some below steps that are related to plugin creation.
Register WP CLI Command Register WP CLI Command
Register PHP class WordPress_Examples_WP_CLI e.g.
if ( ! class_exists( 'WordPress_Examples_WP_CLI' ) && class_exists( 'WP_CLI_Command' ) ) :
class WordPress_Examples_WP_CLI extends WP_CLI_Command {
}
endif;
Here,
We have registered a new class WordPress_Examples_WP_CLI and extend it with WP_CLI_Command.
Add Examples Command Add Examples Command
Now let’s register the examples. E.g.
WP_CLI::add_command( 'examples', 'WordPress_Examples_WP_CLI' );
Here,
We have used the function WP_CLI::add_command() to register our examples command.
The function WP_CLI::add_command() accepts 2 parameters. The first parameter is the command name. Which is examples in our case.
And second is a callback class which is WordPress_Examples_WP_CLI.
Add custom_wp_query sub command Add custom_wp_query sub command
public function custom_wp_query( $args, $assoc_args ) {
WP_CLI::line( 'Hello' );
}
Here,
We have added a function custom_wp_query() with two parameters.
$argscontain the arguments.$assoc_argscontain the associate arguments.
Testing the Command Testing the Command
Open command prompt/terminal. Go to your WordPress setup. I have set up WordPress in D:\xampp\htdocs\dev.test
So, Execute the below commands:
cd D:\xampp\htdocs\dev.testwp examples custom_wp_query
You can see the output something like below:
? wp examples custom_wp_query Hello
This command just shows the string Hello.
Using WP_Query() Using WP_Query()
Its time to execute another command in our WP CLI command with WP_Query().
public function custom_wp_query( $args, $assoc_args ) {
$query_args = array(
'post_type' => 'post',
// Query performance optimization.
'fields' => 'ids',
'no_found_rows' => true,
'posts_per_page' => -1,
);
$query = new WP_Query( $query_args );
if ( $query->posts ) {
foreach ( $query->posts as $key => $post_id ) {
WP_CLI::line( $post_id );
}
}
Here, We have executed the WP_Query and print each post ID with WP_CLI::line( $post_id );
Let’s execute the command wp examples custom_wp_query.
? wp examples custom_wp_query 162 160 89 1
Using \WP_CLI\Formatter() Using \WP_CLI\Formatter()
Now, Lets try to use \WP_CLI\Formatter() to display the posts list in the table format.
In this example I’m using the post type. You can use any other custom post type which you want.
public function custom_wp_query( $args, $assoc_args ) {
$query_args = array(
'post_type' => 'post',
);
$query = new WP_Query( $query_args );
$display_fields = array(
'ID',
'post_title',
'post_author',
'post_date',
'post_type',
'post_status',
'comment_status',
'comment_count',
// 'post_date_gmt',
// 'post_content',
// 'post_excerpt',
// 'ping_status',
// 'post_password',
// 'post_name',
// 'to_ping',
// 'pinged',
// 'post_modified',
// 'post_modified_gmt',
// 'post_content_filtered',
// 'post_parent',
// 'guid',
// 'menu_order',
// 'post_mime_type',
// 'filter',
);
// Initialize the class.
$formatter = new \WP_CLI\Formatter( $assoc_args, $display_fields );
// Display the data.
$formatter->display_items( $query->posts );
}
Here, I have not passed the ‘fields’ => ‘ids’, in WP_Query $query_args list. Because I want to display some other information on the posts too.
? wp examples custom_wp_query +-----+--------------+-------------+---------------------+-----------+-------------+----------------+---------------+ | ID | post_title | post_author | post_date | post_type | post_status | comment_status | comment_count | +-----+--------------+-------------+---------------------+-----------+-------------+----------------+---------------+ | 162 | Sample Post | 1 | 2019-11-05 02:11:16 | post | publish | open | 0 | | 160 | Sample Post | 1 | 2019-11-05 02:10:05 | post | publish | open | 0 | | 89 | Another Post | 1 | 2019-10-15 23:32:57 | post | publish | open | 0 | | 1 | Hello world! | 1 | 2019-10-05 08:23:44 | post | publish | open | 1 | +-----+--------------+-------------+---------------------+-----------+-------------+----------------+---------------+
In recent articles we learn about the --fields associate arguemnts. With --fields associate arguments we can display the specific columns in the result.
For example, lets see only ID, Title & Comment Count on the list.
wp examples custom_wp_query --fields=ID,post_title,comment_count
Here, We have pass the --fields=ID,post_title,comment_count so we can only see these columns.
? wp examples custom_wp_query --fields=ID,post_title,comment_count +-----+--------------+---------------+ | ID | post_title | comment_count | +-----+--------------+---------------+ | 162 | Sample Post | 0 | | 160 | Sample Post | 0 | | 89 | Another Post | 0 | | 1 | Hello world! | 1 | +-----+--------------+---------------+
How to use the wp_query in WP CLI command.
Tweet