Skip to content

Instantly share code, notes, and snippets.

@andykillen
Last active September 6, 2023 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andykillen/38e951ee2866611f65b47d5efd7d0d40 to your computer and use it in GitHub Desktop.
Save andykillen/38e951ee2866611f65b47d5efd7d0d40 to your computer and use it in GitHub Desktop.
taxonomy queries
<?php
$args = [
'post_type' => 'product',
'posts_per_page' => 24,
'post_status' => 'publish',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => 'nike',
],
[
'taxonomy' => 'pa_target_group',
'field' => 'slug',
'terms' => ['men'],
],
[
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => ['sneakers'],
]
]
];
$result = get_posts($args);
@andykillen
Copy link
Author

Gets the first 24 posts that are "brand = nike" , "target_group = men", and "product_cat = sneakers"

@andykillen
Copy link
Author

note that taxonomies created with WooCommerce UI are prefixed with "pa_"

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