WooCommerce products in Custom Post Type

Martin Hugo picture Martin Hugo · May 30, 2016 · Viewed 13k times · Source

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.

Answer

Martin Hugo picture Martin Hugo · May 31, 2016

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!