skip to Main Content

I have added a custom product in woocommerce. I am trying to filter my custom_product using below code but it seems the product_type of my custom_product is still ‘product’. How to filter the my custom product?
E.g. below code can filter for default woocommerce variable products

$filtered_products = $wpdb->get_results(
    "
    SELECT * 
    FROM `" . $wpdb->prefix . "posts` 
    where (post_type='product_variation') AND post_status='publish'
    "
    );

But below code cannot filter the custom product type I added

$filtered_products = $wpdb->get_results(
    "
    SELECT * 
    FROM `" . $wpdb->prefix . "posts` 
    where (post_type='custom_product') AND post_status='publish'
    "
    );

Any other suggestions to filter my custom_product?

2

Answers


  1. Chosen as BEST ANSWER

    Resolved it using WP_QUERY instead of SQL query. Took help from below link Meta Query


  2. I would probably use wc_get_products() if you need to fetch the products via code.

    // Get custom_product products.
    $args = array(
        'type' => 'custom_product',
    );
    $products = wc_get_products( $args );
    

    Source: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query

    To use the REST API I think you would send a request to this URL:

    /wp-json/wc/v3/products?type=custom_product

    https://woocommerce.github.io/woocommerce-rest-api-docs/?shell#list-all-products

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search