Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce how to display products by product SKU

One my client want to display all woocommerce product by product SKU.

Normally i used following code for display products.

$postArg = array('post_type'=>'product',
                            'post_status'=>'publish',
                            'posts_per_page'=>-1,
                            'orderby'=>'data',
                            'order'=>'DESC',

                    );

            $queryGetFiles = get_posts($postArg);

But now my client want to show all products by product SKU in front side.

SKU like this 1041-14, 1041-12, 1041-16 ,1041,2001,3501

all product has different sku value and display which doesn't have "-" character

Anyone know how should i do this?

like image 342
Samir Sheikh Avatar asked Jan 27 '23 14:01

Samir Sheikh


1 Answers

Try this

$postArg = array(
       'post_type'      => 'product',
       'post_status'    => 'publish',
       'posts_per_page' => -1,
       'meta_key'       => '_sku',
       'orderby'        => 'meta_value' // meta_value_num if ordered by intergers
       'order'          => 'DESC',
);

$queryGetFiles = get_posts($postArg);

Answered with help of this post

like image 181
hemnath mouli Avatar answered Jan 30 '23 03:01

hemnath mouli