Last active
May 2, 2022 16:25
-
-
Save kharissulistiyo/547df4c2216d0a492d33 to your computer and use it in GitHub Desktop.
Extending WooCommerce Product Shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Extending query parameter of product shortcode | |
*/ | |
add_filter('woocommerce_shortcode_products_query', 'my_wc_shortcode_product_query_args', 10, 2); | |
function my_wc_shortcode_product_query_args($args, $atts){ | |
if ( isset( $atts['item'] ) ) { | |
$args['posts_per_page'] = $atts['item']; | |
} | |
if ( isset( $atts['author'] ) ) { | |
$args['author'] = $atts['author']; | |
} | |
return $args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome – thanks ✨
For those interested in actually receiving the data from user input: We have to assign them using the
shortcode_atts_products
hook viaadd_filter
and pass the custom$atts
values to the$out
values first: https://developer.wordpress.org/reference/hooks/shortcode_atts_shortcode/Example from OOP context:
Here
item
andauthor
could be anything custom attribute from the[products]
shortcode.