<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'order'=> 'DESC', // sort descending
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
printf( '<a href="%s" class="link">%s</a>', get_permalink(), get_the_title());
}
}
// Restore original post data.
wp_reset_postdata();
?>
This is the first time i've worked with wpquery.
Two questions
- how do i add a comma each item but the last?
- How could i've written this better?