Sort list of posts/products by product attribute
-
Hi guys,
Om my WordPress webshop I have made this great custom shortcode which echos all woocommerce products including the value of the products attribute “pa_stoerrelse” within a specific category.
The shortcodes works well, but I cant figure out how to sort the output. The order is default to product titel but i want to sort by the attribute “pa_stoerrelse”
Is this at all possible? Any ideas?
function my_woocommerce_titles_list($atts) { extract(shortcode_atts(array( 'number_of_posts' => -1, 'category' => '', 'orderby' => 'title', ), $atts)); $return_string = '<table><tr><th>Condom</th><th>size</th></tr>'.PHP_EOL; global $post; $args = array( 'posts_per_page' => $number_of_posts, 'offset' => 0, 'post_status' => 'publish', 'post_type' => 'product', 'product_cat' => $category, 'order' => 'ASC', 'orderby' => $orderby ); $postslist = get_posts( $args ); foreach ( $postslist as $post ) : setup_postdata( $post ); $my_permalink = get_the_permalink(); $my_title = get_the_title(); $size = get_the_terms( $post->id, 'pa_stoerrelse'); foreach ( $size as $size_names ) { global $size_name; $size_name = $size_names->name; } $return_string .= '<tr><td><a class="name_size" href="' . $my_permalink . '">' . $my_title . '</a></td><td class="size">' . $size_name . '</td></tr>'.PHP_EOL; endforeach; wp_reset_postdata(); $return_string .= '</table>'.PHP_EOL; return $return_string; }; function my_register_shortcodes(){ add_shortcode('my_product_titles', 'my_woocommerce_titles_list'); } add_action( 'init', 'my_register_shortcodes');
Here is a link to where the shortcode is beeing used, if this is any help: https://secretly.dk/kondom-stoerrelsesguide/
Thanks in advance
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Sort list of posts/products by product attribute’ is closed to new replies.