Support » Fixing WordPress » Sort list of posts/products by product attribute

  • thomasmi87


    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) {
    		'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;		
    	$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:

    Thanks in advance

Viewing 3 replies - 1 through 3 (of 3 total)
  • Josh


    1) Your orderby usage from the shortcode atts in the get_posts() args function is incorrect:

    'orderby' => $orderby should be 'orderby' => $atts['orderby'].

    Same for the posts_per_page argument., and the product_cat argument.

    'posts_per_page' => $number_of_posts should be 'posts_per_page' => $atts['number_of_posts'].
    'product_cat' => $category should be 'product_cat' => $atts['category'].

    2) Right after you run your get_posts() function, you will get an array of all your matched posts, which is being assigned to your $postslist variable.

    3) So, after you get your get_posts() and just before you run your foreach loop, you can sort your array results (the $postslist variable). You may have to var_dump() to see the output. And you may need a custom sort function to sort the array.

    Play around and let me know how you do. I imagine the custom sort function will be a little tricky, but go ahead and get familiar with the code (and the dumped array results).

    Please let me know when you get “stuck” 🙂



    Hi Josh,

    Thanks alot!

    I will give it a try and let you know if I get it to work 🙂

    Did you get this to work? I want to order by attribute value for my woocommerce products.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sort list of posts/products by product attribute’ is closed to new replies.