2

I'm running a simple query just to find simple WooCommerce products, here is the query which works fine in the header of the site:

$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_type',
            'field' => 'slug',
            'terms' => array('simple')
        ),
    ),
);

$simpleProducts = new WP_Query($args);

This query returns 1065 products which are correct. If I run the same query through a WP CLI command, I get 0 posts... however, if I remove the tax query it's returns all the products on the site.

Here is the command that runs:

// Add the command.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
   \WP_CLI::add_command('update-stock', \stockChecker::syncStockFile());
}

Has anyone had this issue with WP CLI?

EDIT

To add, this runs fine:

$args = array(
    'post_type' => 'product_variation',
    'posts_per_page' => -1,
);

$products = new WP_Query($args);
  • I've created a custom WP-CLI command. – WebDevB Jul 7 at 6:56
  • @leymannx I've added the CLI command above. – WebDevB Jul 7 at 8:54
  • to simplify the issue, did you try it as a raw wp-cli eval-file PHP script to see what happens? – mozboz Jul 7 at 11:27
  • Out of interest, did you make any progress here? – mozboz Jul 15 at 20:33
  • Unfortunately, not @mozboz! Had to move it into the functions and call the WPCLI command from there... that seems to work. – WebDevB Jul 21 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.