Get WooCommerce Products by Custom Taxonomy Term Values in a WP_Query
As a WordPress developer, one of the common tasks you may encounter is retrieving products from a WooCommerce store based on specific criteria. In this blog post, we'll explore how to use the WP_Query
class to fetch WooCommerce products by custom taxonomy term values.
Suppose you have a custom taxonomy called "collane" associated with your WooCommerce "product" post type. You want to display two other products that have the same "collane" taxonomy terms as the current product.
To achieve this, we'll use the wp_get_post_terms()
function to retrieve the term IDs for the current product, and then pass those term IDs to the WP_Query
to fetch the related products.
Here's the code:
$taxonomy = 'collane'; // The targeted custom taxonomy
// Get the terms IDs for the current product related to 'collane' custom taxonomy
$term_ids = wp_get_post_terms( get_the_id(), $taxonomy, array('fields' => 'ids') ); // array
$query = new WP_Query( $args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 2, // Limit: two products
'post__not_in' => array( get_the_id() ), // Excluding current product
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id', // can be 'term_id', 'slug' or 'name'
'terms' => $term_ids,
), ),
));
// Test count post output
echo '<p>Posts count: ' . $query->post_count . '</p>';
// The WP_Query loop
if ( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
// Test output
echo '<p>' . $query->post->post_title . ' (' . $query->post->ID . ')</p>';
endwhile;
wp_reset_postdata();
endif;
Let's break down the code:
-
Retrieving the Custom Taxonomy Term IDs:
- We start by defining the custom taxonomy name (
$taxonomy = 'collane';
).
- Then, we use the
wp_get_post_terms()
function to retrieve the term IDs associated with the current product. We pass the get_the_id()
function to get the ID of the current product, the $taxonomy
variable, and an array with the 'fields' => 'ids'
parameter to return only the term IDs.
-
Constructing the WP_Query
:
- We create a new instance of the
WP_Query
class and pass an array of arguments ($args
) to it.
- The
'post_type'
parameter is set to 'product'
to target the WooCommerce products.
'post_status'
is set to 'publish'
to only retrieve published products.
'ignore_sticky_posts'
is set to 1
to exclude any sticky posts.
'posts_per_page'
is set to 2
to limit the number of products returned to two.
'post__not_in'
is set to an array containing the current product's ID, to exclude the current product from the results.
- The
'tax_query'
parameter is set to an array with a single entry. This entry defines the custom taxonomy ('taxonomy' => $taxonomy
), the field type ('field' => 'term_id'
), and the term IDs ('terms' => $term_ids
) to use for the query.
-
Displaying the Results:
- We check if the
WP_Query
has any posts to display.
- If there are posts, we loop through them using the
while
loop and the have_posts()
and the_post()
methods.
- Inside the loop, we can access the post's title and ID using
$query->post->post_title
and $query->post->ID
, respectively.
- Finally, we call
wp_reset_postdata()
to reset the global $post
variable to the original state.
This code will retrieve two WooCommerce products that have the same custom taxonomy terms as the current product. You can further customize the WP_Query
arguments to match your specific requirements, such as sorting the products, filtering by other metadata, or including additional taxonomies.
By using the WP_Query
class and the wp_get_post_terms()
function, you can effectively retrieve and display related products based on custom taxonomy term values in your WooCommerce-powered WordPress website.
Flowpoint.ai can help you identify all the technical errors that are impacting conversion rates on your WooCommerce website and directly generate recommendations to fix them.