0

I am trying to modify the [products] shortcode usage:

https://docs.woocommerce.com/document/woocommerce-shortcodes/#section-11

What I am looking was passing the author id from short code e.g [products author=2] to the query.

With the reference of: https://wordpress.org/support/topic/adding-a-special-product-attribute-to-the-products-shortcode/

But the sample was a true/false parameter to trigger the $query_args change, seems it's not exactly my goal. And the function below is the only part I can put into use.

function htdat_woocommerce_shortcode_products_query( $query_args, $attributes ) {       
$query_args = array(
   //Receive the id//'author'    => '1',
   'post_status'    => 'publish',
   'post_type'      => 'product',
   'post_per_page'      => 12,
   );   
   
  return $query_args;
}
add_filter( 'woocommerce_shortcode_products_query', 'htdat_woocommerce_shortcode_products_query', 10, 2 );

If anyone could help, thank you.

1

Try this:

function htdat_shortcode_atts_products( $out, $pairs, $atts, $shortcode ) {
    if ( isset ( $atts['author'] ) ) $out['author'] = $atts['author'];
    return $out;
}
add_filter('shortcode_atts_products', 'htdat_shortcode_atts_products', 10, 4);

function htdat_woocommerce_shortcode_products_query( $query_args, $attributes ) {       
    if ( isset( $attributes['author'] ) ) $query_args['author'] = $attributes['author'];
    return $query_args;
}
add_filter( 'woocommerce_shortcode_products_query', 'htdat_woocommerce_shortcode_products_query', 10, 2 );
5
  • The output show all products from all authors. I think the $attributes['author'] is not getting right from shortcode before it passes to query? – Cwkin7 Nov 24 '20 at 14:19
  • @Cwkin7 Try to hardcode the author id: $query_args['author'] = 2; return $query_args; and check if it alters the output? – Ivan Shatsky Nov 24 '20 at 14:22
  • No change to the out put. I was looking back to the reference: wordpress.org/support/topic/…. There should be a shortcode_atts function to add up? – Cwkin7 Nov 24 '20 at 14:30
  • @Cwkin7 I added that one to the answer, but if the hardcoded author id doesn't changed the output I think that one won't solve the problem too. I'll give it a live test on one of my sites in a few hours to check what happens under the hood. – Ivan Shatsky Nov 24 '20 at 14:36
  • Yes, it's working, I combined them. function htdat_woocommerce_shortcode_products_query( $query_args, $attributes ) { $query_args = array( 'author' => $attributes['author'], 'post_status' => 'publish', 'post_type' => 'product', 'post_per_page' => 12, ); return $query_args; } – Cwkin7 Nov 24 '20 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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