1

I have this code in my themes' shortcode to display featured product rating product like this. But I need to get product by category show. Which code do I have to add. Any suggestions?

if($type == 'featured'){

        /**********QUERY FOR FEATURED PRODUCT**********/

        $query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product' );



        $query_args['meta_query'] = array();



        $query_args['meta_query'][] = array(

            'key' => '_featured',

            'value' => 'yes'

        );

        $query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();

        $query_args['meta_query'][] = $woocommerce->query->visibility_meta_query();

        /**********END QUERY FOR FEATURED PRODUCT**********/

    }elseif($type == 'top-rated'){

        /**********QUERY FOR TOP-RATED PRODUCT**********/

        add_filter( 'posts_clauses',  array( $woocommerce->query, 'order_by_rating_post_clauses' ) );



        $query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product' );



        $query_args['meta_query'] = array();



        $query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();

        $query_args['meta_query'][] = $woocommerce->query->visibility_meta_query();

        /**********END QUERY FOR TOP-RATED PRODUCT**********/
1

First, you have to determine from what category you want to display those products. If this happends on a single post display, you can get all post's categories via wp_get_object_terms function

global $post;
$query_args[ 'category__in' ] = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );

If this happens on a archive page, you can get the category id from query vars:

$query_args[ 'cat' ] = get_query_var('cat');

Check http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.