WordPress use WP_Query to get product by ID

I’m trying to get a specific product by its ID in WordPress using WP_Query and i’m struggling to find the right argument from yesterday. I’m stuck.I’ts from ‘product_cat’ taxonomy

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 1,
    'id' => $product_id,
);

Related posts

3 comments

  1. $args = array(
        'post_type' => 'product',
        'posts_per_page' => 1,
        'post__in'=> array($product_id)
    );
    
  2. Stupid questing but i’ve found the answer. Kept my brain struggling for so long…

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 1,
        'post__in'=> array($id),
    );
    
  3. function cs_get_cost_transportation( WP_REST_Request $request ) {
        $productId = $request->get_param( 'productId' );
        $wpq         = new WP_Query( array(
                'post_type'      => 'product',
                 'posts_per_page' => 1,
                 'post__in'=> array($productId)
        ) );
        $postIds     = $wpq->get_posts();
        $productsFilter_cabin = cs_get_products_for_from_post_ids( $postIds );
        return rest_ensure_response($productsFilter_cabin);
    
    }
    

Comments are closed.