0

I want to add some additional query vars and customise the query for woocommerce [products] shortcode. i'm trying to add some additional fields and conditions with pre_get_posts and posts_fields hooks but both of them is not working. is there any other way to filter (customise) the SQL query specific to woocommerce shortcodes?

1 Answer 1

1

You could use the WooCommerce filter woocommerce_shortcode_products_query to change/manipulate the query arguments.

This example comes from the shortcode documentation.

When using the Products shortcode, you can choose to order products by the pre-defined values above. You can also sort products by custom meta fields using the code below (in this example we order product by price):

add_filter( 'woocommerce_shortcode_products_query', 'woocommerce_shortcode_products_orderby' );

function woocommerce_shortcode_products_orderby( $args ) {

  $standard_array = array('menu_order','title','date','rand','id');

  if( isset( $args['orderby'] ) && !in_array( $args['orderby'], $standard_array ) ) {
    $args['meta_key'] = $args['orderby'];
    $args['orderby']  = 'meta_value_num'; 
  }

 return $args;
}
1
  • I want to use a SQL statement and add a derived column to the query result. Commented Aug 7, 2018 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.