0

I am working on a custom plugin and one of it's functionailities is to scan all the posts/pages on daily basis and update a meta-data in the database. I also wanted to create a custom WP-CLI command for system administrators' convenience. Here is my code:

<?php
if( defined( 'WP_CLI' ) && WP_CLI ) {
    /**
     * Scans all public posts and updates the custom_plugin_last_scan post_meta.
     *
     * ## EXAMPLES
     *
     *     wp posts scan --post_type=post --status=draft
     *
     * @when after_wp_load
     */
    class ScanPostsCommand {

        /**
         * Scans all public posts and updates the custom_plugin_last_scan meta key.
         *
         * ## OPTIONS
         *
         * [--post_type=<post_type>]
         * : Post type to scan. Default is 'any'.
         *
         * [--status=<status>]
         * : Post status to scan. Default is 'publish'.
         */
        public function __invoke($args, $assoc_args) {
            WP_CLI::line('Scanning posts...');

            $post_type = isset($assoc_args['post_type']) ? $assoc_args['post_type'] : 'any';
            $post_status = isset($assoc_args['status']) ? $assoc_args['status'] : 'publish';

            $query = new WP_Query(array(
                'post_type' => $post_type,
                'post_status' => $post_status,
                'posts_per_page' => -1,
            ));

            $scanned_posts = 0;
            while ($query->have_posts()) {
                $query->the_post();
                update_post_meta(get_the_ID(), 'custom_plugin_last_scan', current_time('mysql'));
                $scanned_posts++;
            }
            wp_reset_postdata();

            WP_CLI::success("Scanned $scanned_posts posts and updated their meta data.");
        }
    }

    WP_CLI::add_command('posts scan', 'ScanPostsCommand');
}
?>

But when I run the command wp posts scan it does not work and generate an error

Error: 'posts' is not a registered wp command. See 'wp help' for available commands.
Did you mean 'post'?

I have WP-CLI installed on my system. When I run wp --info I get below results:

OS:     Darwin 24.0.0 Darwin Kernel Version 24.0.0: Thu May 30 21:32:37 PDT 2024; root:xnu-11215.0.31.511.2~1/RELEASE_ARM64_T6030 arm64
Shell:  /bin/zsh
PHP binary:     /opt/homebrew/Cellar/php/8.3.2/bin/php
PHP version:    8.3.2
php.ini used:   /opt/homebrew/etc/php/8.3/php.ini
MySQL binary:
MySQL version:
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /Users/farhanali/Sites/wordpress/wp-content/plugins/custom-plugin/app/admin-pages
WP-CLI packages dir:
WP-CLI cache dir:       /Users/farhanali/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.10.0

1 Answer 1

0

You are trying to add a subcommand to a non-existing command posts according to the error message.

Modify it to a single word command like posts-scan or add a subcommand to an existing command, like post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.