• 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)
  • @thomasmi87 your issue is that you’ve told the code to order by title. In your line ‘orderby’ => ‘title’.

    You need to change how you are ordering, I believe you need to use ‘meta_key’. https://codex.www.ads-software.com/Class_Reference/WP_Query#Order_.26_Orderby_Parameters has more information on using orderby with queries.

    Thread Starter thomasmi87

    (@thomasmi87)

    HI @anphira,

    Thank you for your reply.

    I already tried to change it to order by meta_value, but unfirtunatly it seems like woocommerce attributes are not considered a meta_value, so it does not seem to work.

    this is what i have tried:

    	$args = array(
    		'posts_per_page' => $atts['number_of_posts'],
    		'offset'           		=> 0,
    		'post_status'      		=> 'publish',
    		'post_type'      		=> 'product',
    		'product_cat' => $atts['category'],
    		'order'				=> 'ASC',
    		'orderby' => $atts['meta_value'],
    		'meta_key' => $atts['pa_stoerrelse']
    	);

    @thomasmi87 you might also want to try some of the facebook groups for WooCommerce — the members tend to be a bit more active than these message boards. Unfortunately, it looks like the WooCommerce official employees seem to have abandoned these boards.

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