I am trying to get a simple shortcode to loop products based on price (descending). For some reason the below code doesn’t order it by price, it just ignores the order attribute. Code is below:
function sort_by_price( $atts ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 20,
'orderby' => 'price',
'order' => 'desc'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
add_shortcode( 'sorter', 'sort_by_price' );
Any help is appreciated!
Price isn’t a recognized value for the
WP_Query
sortby
parameter.Try the following args: