Press enter to see results or esc to cancel.

Code example: Custom WP-CLI Command and How it works

If you’re a developer managing WordPress sites, you might need to automate certain tasks or fetch information quickly using the command line, and that’s where WP-CLI commands come in handy. This particular command checks if the “Show featured image in the posts lists only but hide it in the single post view” option is enabled for posts.

<?php    
/**
     * Custom WP CLI command to check the option "Show featured image in the posts lists only but hide it in the single post view".
     */
    class Check_Featured_Image_Option_Command {
        /**
         * Checks if the "Show featured image in the posts lists only but hide it in the single post view" option is enabled for posts.
         *
         * ## OPTIONS
         *
         * [--post_id=<post_id>]
         * : The ID of the post to check. If not provided, all posts will be checked.
         *
         * ## EXAMPLES
         *
         * wp check-featured-image-option --post_id=123
         * wp check-featured-image-option
         *
         * @when after_wp_load
         */
        public function __invoke($args, $assoc_args) {
            $post_id = isset($assoc_args['post_id']) ? intval($assoc_args['post_id']) : null;
            
            if ($post_id) {
                $this->check_option_for_post($post_id);
            } else {
                $posts = get_posts([
                    'post_type'      => 'post',
                    'posts_per_page' => -1,
                    'post_status'    => 'publish',
                ]);
                
                foreach ($posts as $post) {
                    $this->check_option_for_post($post->ID);
                }
            }
        }

        /**
         * Checks the option for a specific post.
         *
         * @param int $post_id The ID of the post to check.
         */
        private function check_option_for_post($post_id) {
            $show_featured_image_option = get_post_meta($post_id, 'show_featured_image_list_only', true);
            
            if ($show_featured_image_option) {
                WP_CLI::log("Post ID {$post_id}: The option 'Show featured image in the posts lists only but hide it in the single post view' is enabled.");
            } else {
                WP_CLI::log("Post ID {$post_id}: The option is not enabled.");
            }
        }
    }
    
    WP_CLI::add_command('check-featured-image-option', 'Check_Featured_Image_Option_Command');
}

Let’s break down the code step by step.

Overview of the Code

The code defines a custom command named check-featured-image-option. The purpose of this command is to check whether the option to display the featured image in post lists only (but not in the individual post view) is enabled for specific posts or all posts.

Class and Methods Breakdown

The core of the code is the Check_Featured_Image_Option_Command class. This class contains methods to perform the task of checking the featured image setting for posts.

1. Declaring the WP-CLI Command

At the end of the code, the line:

WP_CLI::add_command('check-featured-image-option', 'Check_Featured_Image_Option_Command');

registers the custom command check-featured-image-option with WP-CLI, associating it with the Check_Featured_Image_Option_Command class. This means you can run the command from the terminal using WP-CLI, such as:

wp check-featured-image-option --post_id=123

or, to check all posts:

wp check-featured-image-option

2. The Main Function: __invoke()

The __invoke() method is a special function in PHP that allows an object to be called like a function. In this case, it serves as the main handler for the command. It takes two arguments: $args and $assoc_args.

  • $args represents positional arguments (not used here).
  • $assoc_args represents associative arguments (like --post_id), which are used to specify which post to check.

If the post_id argument is provided, the method calls check_option_for_post() for that specific post. Otherwise, it fetches all posts and checks each one.

The relevant code looks like this:

$post_id = isset($assoc_args['post_id']) ? intval($assoc_args['post_id']) : null;

if ($post_id) {
    $this->check_option_for_post($post_id);
} else {
    $posts = get_posts([
        'post_type'      => 'post',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    ]);

    foreach ($posts as $post) {
        $this->check_option_for_post($post->ID);
    }
}

This means that if no post_id is provided, the script will retrieve all published posts and loop through each of them to perform the check.

3. Checking the Option: check_option_for_post()

The check_option_for_post() method is responsible for checking if the option is enabled for a specific post. It retrieves the post meta value for 'show_featured_image_list_only' using get_post_meta().

  • If the option is enabled, it logs a message indicating that the featured image setting is enabled for that post.
  • If not, it logs a different message stating that the option is not enabled.

The code for this is:

$show_featured_image_option = get_post_meta($post_id, 'show_featured_image_list_only', true);

if ($show_featured_image_option) {
    WP_CLI::log("Post ID {$post_id}: The option 'Show featured image in the posts lists only but hide it in the single post view' is enabled.");
} else {
    WP_CLI::log("Post ID {$post_id}: The option is not enabled.");
}

This means the command will output messages to the terminal, indicating the status of this setting for each post it checks.

How to Use the Command

  1. Checking a Specific Post: To check if the option is enabled for a specific post, run: wp check-featured-image-option --post_id=123
  2. Checking All Posts: To check all posts, simply run: wp check-featured-image-option

This will loop through all published posts and report their featured image settings.

Summary

This custom WP-CLI command provides a useful way to verify whether the “Show featured image in the posts lists only but hide it in the single post view” option is enabled for WordPress posts. It can be particularly handy for developers or site managers who need to audit or troubleshoot featured image settings across multiple posts. The use of WP-CLI allows for efficient command-line execution, saving time compared to manually inspecting each post in the WordPress admin dashboard.

Key Takeaways

  • Custom WP-CLI Command: The code creates a new command named check-featured-image-option to check the featured image setting.
  • Flexible Usage: You can check either a specific post or all posts at once.
  • Helpful for Audits: Ideal for bulk-checking posts, especially useful for developers managing a lot of content.

Now you know how to create and use a custom WP-CLI command to automate the task of checking featured image settings in WordPress posts. Feel free to expand or modify the command for more specific needs!