-
-
Save diekaines/d4b9d924b519ab08f52e to your computer and use it in GitHub Desktop.
List WooCommerce products by tags
This file contains 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 | |
/** | |
* Plugin Name: WooCommerce - List Products by Tags | |
* Plugin URI: http://www.remicorson.com/list-woocommerce-products-by-tags/ | |
* Description: List WooCommerce products by tags using a shortcode, ex: [woo_products_by_tags tags="shoes,socks"] | |
* Version: 1.0 | |
* Author: Remi Corson | |
* Author URI: http://remicorson.com | |
* Requires at least: 3.5 | |
* Tested up to: 3.5 | |
* | |
* Text Domain: - | |
* Domain Path: - | |
* | |
*/ | |
/* | |
* List WooCommerce Products by tags | |
* | |
* ex: [woo_products_by_tags tags="shoes,socks"] | |
* | |
* Updated a bit by Bruno Kos, http://bbird.me/ | |
*/ | |
function woo_products_by_tags_shortcode( $atts, $content = null ) { | |
// Get attribuets | |
extract( shortcode_atts( array( | |
'per_page' => '12', | |
'columns' => '4', | |
'orderby' => 'title', | |
'order' => 'desc', | |
'category' => '', | |
'tags' => '', | |
), $atts ) ); | |
ob_start(); | |
// Define Query Arguments | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => 1, | |
'orderby' => $ordering_args['orderby'], | |
'order' => $ordering_args['order'], | |
'posts_per_page' => $per_page, | |
'product_tag' => $tags, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'product_cat', | |
'terms' => array( esc_attr( $category ) ), | |
'field' => 'slug', | |
'operator' => $operator | |
)) | |
); | |
ob_start(); | |
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) ); | |
$woocommerce_loop['columns'] = $columns; | |
if ( $products->have_posts() ) : ?> | |
<?php woocommerce_product_loop_start(); ?> | |
<?php while ( $products->have_posts() ) : $products->the_post(); ?> | |
<?php wc_get_template_part( 'content', 'product' ); ?> | |
<?php endwhile; // end of the loop. ?> | |
<?php woocommerce_product_loop_end(); ?> | |
<?php endif; | |
woocommerce_reset_loop(); | |
wp_reset_postdata(); | |
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>'; | |
} | |
add_shortcode("woo_products_by_tags", "woo_products_by_tags_shortcode"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment