WooCommerce products in Custom Post Type
Solution 1
So after much struggle here is the code snippet:
<?php
//retrieves the term variable from the admin page - replace "product_category" with the name of your post type
$part_terms = get_the_terms( $post->ID, 'product_category' );
if( $part_terms && !is_wp_error( $part_terms ) ) {
foreach( $part_terms as $term ) {
}
}
//create a variable to filter your Wordpress Loop
$part_args = array(
'post_type' => 'product',
'hierarchical' => true,
'posts_per_page' => -1,
'tax_query' => array(array(
'taxonomy' => 'product_category',
'field' => 'slug',
'terms' => array($term->slug),
'operator' => 'IN'
))
);
$loop = new WP_Query( $part_args );
//the loop
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
//some handy woocommerce coding
echo '<div class="background-img"><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().'<br /> '.get_the_title().'</a>';
echo $product->get_price_html();
echo '<form class="cart" method="post" enctype="multipart/form-data">
<input type="hidden" name="add-to-cart" value="';
echo esc_attr($product->id);
echo '">
<button type="submit">';
echo $product->single_add_to_cart_text();
echo '</button>
</form>';
echo '</div>';
endwhile;
?>
Thanks JayDeep for setting me on the right track!
Solution 2
You need to use tax_query for use filter by texonomy.
Check this link: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Martin Hugo
Updated on June 12, 2022Comments
-
Martin Hugo almost 2 years
I am trying to use the Woocommerce category taxonomy to display relevant products on a page. The custom post type allows me to add the product category list to the custom page but I am not sure how to filter it to only show the category I select for the respective page. At the moment it is showing all my products, not filtering the ones I need:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'taxonomy' => 'product_cat' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; echo '<div class="background-img"><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().'<br /> '.get_the_title().'</a>'; echo $product->get_price_html(); echo '<form class="cart" method="post" enctype="multipart/form-data"> <input type="hidden" name="add-to-cart" value="'; echo esc_attr($product->id); echo '"> <button type="submit">'; echo $product->single_add_to_cart_text(); echo '</button> </form>'; echo '</div>'; endwhile; $attachment_ids = $product->get_gallery_attachment_ids(); foreach( $attachment_ids as $attachment_id ) { echo $image_link = wp_get_attachment_url( $attachment_id ); } wp_reset_query();
Here is a version of the dev site, it is still quite raw and needs alot of styling but should give an idea:
http://betamarine.mainboard.com/engine/beta-14-z482/
I'm using the following plugin:
https://wordpress.org/plugins/custom-post-type-ui/
I can create a custom taxonomy but it is an extra step and produces the same results. I think I may be missing something small but I'm just not getting it.
-
Martin Hugo almost 8 yearsThanks for the response, read through the codex and came up with this:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'tax-query' => array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array($cat->slug), 'operator' => 'IN' )) );
it is not doing anything though, still just showing all the products, not very good at PHP - am I on the right track? -
JayDeep Nimavat almost 8 yearsYes, you are in right track you can change : 'tax-query' => array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array($cat->slug), 'operator' => 'IN' )) array to as per your requirement. for e.g. operator support "IN", "=", "LIKE", ">", "<" etc.
-
Martin Hugo almost 8 yearsOh my word, just realised I was using 'tax-query' instead of 'tax_query' - with an underscore! That said, if I add the term name (eg. 'engine')in the place of array($category->slug) it works, if I add the variable call nothing shows. I suspect it may be calling something else on the page?