Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Created May 20, 2025 09:56
Show Gist options
  • Save shameemreza/41dec1814e52db5de7895831e8f6ea95 to your computer and use it in GitHub Desktop.
Save shameemreza/41dec1814e52db5de7895831e8f6ea95 to your computer and use it in GitHub Desktop.
Shortcode to display all attribute terms in WooCommerce, including unused ones, with working filter links.
/**
* Shortcode: [all_attribute_terms attribute="pa_size"]
* Shows all terms for a product attribute, even if not assigned to products.
* Links use WooCommerce's layered nav filter format.
*/
function show_all_attribute_terms( $atts ) {
$atts = shortcode_atts( [
'attribute' => '',
], $atts );
if ( empty( $atts['attribute'] ) ) {
return '';
}
$taxonomy = wc_sanitize_taxonomy_name( $atts['attribute'] );
$attribute = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $taxonomy ) );
$terms = get_terms( [
'taxonomy' => $taxonomy,
'hide_empty' => false,
] );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return '';
}
// Get the shop page URL
$shop_url = wc_get_page_permalink( 'shop' );
$output = '<ul class="all-attribute-terms">';
foreach ( $terms as $term ) {
// Build WooCommerce layered nav filter URL
$url = add_query_arg( [
'filter_' . $attribute => $term->slug,
'query_type_' . $attribute => 'or',
], $shop_url );
$output .= sprintf(
'<li><a href="%s">%s</a> (%d)</li>',
esc_url( $url ),
esc_html( $term->name ),
intval( $term->count )
);
}
$output .= '</ul>';
return $output;
}
add_shortcode( 'all_attribute_terms', 'show_all_attribute_terms' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment