plugin development - woocommerce wc_get_product is not fetching all the product of particular category

admin2025-06-05  7

I have created dummy product data with fakerpress and many products were created with "future" status, so I updated the post status to "publish". I wanted to retrieve all the product of selected category. Code for it is :

    $args = array(
        'limit'    => -1,
        'status'   => 'publish',
        'category' => array('clothing'),
    );

    $products = wc_get_product($args);
    echo "<pre>";
    print_r(count($products));
    echo "</pre>";
    exit();

It is returning only 8 products but i have 128 product in this category. I also created product with the woo-commerce sample csv file, on doing so, I am getting the product count exactly as it should be. Can anyone please provide me the hint for what needs to be done ? Any kinds of help are appreciated. Thanks.

EDIT: What I found is,some products are already in publish status and they are not fetched, but if i edit that product and simply update the product without changing any data, it is fetched. What could be the reason of this. Any ideas ?

WP_Query for fetching posts :

    $categories = array(1,2,3); // IDs of categories
    $args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page'        => -1,

        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'field' => 'term_id', //This is optional, as it defaults to 'term_id'
                'terms'         => implode(',',$categories),
                'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
            )
        )
    );
   $postProducts = new WP_Query($args);

This returns the post data as required but cannot retrieve meta_keys and values associated with product.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749099959a316359.html

最新回复(0)