Write WP_Query for products list filtered by product_category='featured' ...

Hello,

I've created a product_category called 'featured'

I'm now trying to write and submit a WP_Query that will give me all data including regular_price, sale_price_amount metadata for all published products with the ability to limit by category (in this case 'featured') but there will be others in the future.

Can someone provide a code snippet for this ?

My code shown below is returning all products and data as desired, however it is ignoring the filter on category=featured so gives all products

$filter_query['taxonomy']='product_category';
$filter_query['field']='slug';
$filter_query['terms']='featured';

$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'showposts' => -1,
'tax_query' => $filter_query,
'order_by' => 'date',
'order' => 'ASC'
);

$sqlResultsObj = new WP_Query( $args );

What sayeth the group?

Jay
CompuMatter

  • Rupok
    • Support Ninja

    Hi Jay

    Hope you had a wonderful day.

    I believe, our plugin developer can give you best idea regarding this. So I'm flagging our developer here for his valuable opinion regarding this. I believe, he will come up with a solution/suggestion very soon.

    BTW, I can see you are still using MarketPress 2 which is a very old version. Can you please update to the latest version?

    Have a nice day. Cheers!
    Rupok

  • Nathan Onn
    • Design Lord, Child of Thor

    Here's the query you need:

    $args = array(
    	'post_type' => MP_Product::get_post_type(),
    	'post_status' => 'publish',
    	'showposts' => -1,
    	'product_category' => 'featured',
    	'order_by' => 'date',
    	'order' => 'ASC'
    );
    
    $sqlResultsObj = new WP_Query( $args );
  • Jay
    • The Incredible Code Injector

    Hello and thank you for your reply.

    Unfortunately it returns no records. Here is an echo of the SQL based on echo $wpdb->last_query and your code snippet.
    SELECT cm_37_posts.* FROM cm_37_posts WHERE 1=1 AND ( 0 = 1 ) AND cm_37_posts.post_type = 'product' AND ((cm_37_posts.post_status = 'publish')) GROUP BY cm_37_posts.ID ORDER BY cm_37_posts.menu_order, cm_37_posts.post_date ASC To be certain I also copied / pasted the above query in phpmyadmin and got: MySQL returned an empty result set (i.e. zero rows)

    However, if I comment out the portion of your query 'product_category' => 'featured' it returns all records just as before. Here is the returned SQL associated with that query.
    SELECT post_id, meta_key, meta_value FROM cm_37_postmeta WHERE post_id IN (417,419) ORDER BY meta_id ASC

    I have exactly 2 records which I am using for this test. One is featured, one is not (see attached pictures)

    Thanks for your consideration of this problem.

    Jay
    CompuMatter

  • Nithin
    • Support Wizard

    Hi Jay,

    Hope you are doing good today. :slight_smile:

    The above code seems to work, can you confirm the category slug you entered is "featured", since the value assigned to 'product_category' => 'featured', is actually the name of the product category slug rather than the category name. If not, please replace the value with a slug name.

    It seems like the support staff access is not working, if you still have issue, please enable the access and let us know how it goes.

    Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.