Skip to content

Instantly share code, notes, and snippets.

@bbirdme
Forked from corsonr/gist:5933479
Last active March 31, 2020 08:34
Show Gist options
  • Select an option

  • Save bbirdme/a39284bbfee3df67760a to your computer and use it in GitHub Desktop.

Select an option

Save bbirdme/a39284bbfee3df67760a to your computer and use it in GitHub Desktop.
List WooCommerce products by tags
<?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");
Copy link
Copy Markdown

ghost commented Aug 9, 2017

Hey, I'm using this plugin in my woocommerce website.
I had to add this: global $woocommerce_loop; on line 58 to the plugin work properly.
Doesn't you have also ?

@keusta
Copy link
Copy Markdown

keusta commented Mar 31, 2020

thx for the update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment