php - Related products by category wordpress -


i have problem, can not related products in category, can tell?

<?php     global $post;     $args = array(         'post_type' => 'twin_posts',         'showposts' => 4     );     $categories = get_the_category( $args );     $old_query = $wp_query;     $wp_query = new wp_query( $args ); ?> <ul class="ps-list row"> <?php     while ( have_posts() ) : the_post();     endwhile;     $wp_query = $old_query; ?> </ul> 

first have current product's category id.

global $post;  $terms = get_the_terms( $post->id, 'product_cat' );  $product_cat_id = array();  foreach ($terms $term) {     $product_cat_id[] = $term->term_id;  } 

then use below wp_query code:

$args = array(     'post_type'             => 'product',     'post_status'           => 'publish',     'ignore_sticky_posts'   => 1,     'posts_per_page'        => '12',     'meta_query'            => array(         array(             'key'           => '_visibility',             'value'         => array('catalog', 'visible'),             'compare'       => 'in'         )     ),     'tax_query'             => array(         array(             'taxonomy'      => 'product_cat',             'field' => 'term_id', //this optional, defaults 'term_id'             'terms'         => $product_cat_id,             'operator'      => 'in' // possible values 'in', 'not in', 'and'.         )     ) ); $products = new wp_query($args); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -