Get WooCommerce featured products in a WP_Query - php

I updated WooCommerce to version 3.0 but I can't show the featured products on my theme, I googled a while and get WC deleted the _feature and add this in taxonomy. But I don't understand so much how my theme get the featured products.
Here is the code of the wrong featured productcs.
$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
'key' => '_featured',
'value' => 'yes'
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'meta_query' => $meta_query
);
And if you know where is the featured item in the DataBase. Thanks so much.

Since Woocommerce 3, you need to use a Tax Query instead as featured products are now handled by product_visibility custom taxonomy for the term featured:
// The tax query
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN', // or 'NOT IN' to exclude feature products
);
// The query
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'tax_query' => $tax_query // <===
) );
References:
Official documentation WP_Query Taxonomy Parameters
Source code Woocommerce WC_Shortcodes featured_products() function
You could use wc_get_featured_product_ids() function to get the featured product IDs array but using a tax query in a WP_Query is just fine and the right way…
Related:
Woocommerce meta_query not working for featured products
Show only featured products in Woocommerce shop page
Get featured products in Woocommerce 3
It should works.

This is an old question, but you can use wc_get_featured_product_ids() too:
$args = array(
'post_type' => 'product',
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'post__in' => wc_get_featured_product_ids(),
);
$query = new WP_Query( $args );
Just discovered it here. I hope it helps!

You can now use wc_get_products with parameter featured set to true. See https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
$args = array(
'featured' => true,
);
$products = wc_get_products( $args );
For people looking for getting featured products by category then you can check my notes about this => https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/

$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();

Related

Woocommerce order products by attribute name asc

I have a custom page template with products in Woocommerce, but I want order the products by attribute's terms name asc. I have three terms. I don't know the right way to do it, I have tried this:
<?php
args = array(
'post_type' => 'product',
'posts_per_page' => 20,
'meta_key' => 'pa_tire-type',
'orderby' => 'meta_value_num',
'order' => 'asc'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
— Updated —
You need to add a tax_query for your product attribute with the related terms (slugs) in it this way:
// Set HERE the attibute taxonomy slug
$taxonomy = 'pa_tire-type';
// Set HERE the term slugs for this attribute
$terms_array = array('term_slug1', 'term_slug2', 'term_slug3');
// The loop query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 20,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms_array,
'operator' => 'IN',
) ),
'order_by' => 'terms',
'order' => 'asc'
) );
$product_ids = array();
if ( $loop->have_posts() ): while ( $loop->have_posts() ) : $loop->the_post();
$product_ids[] = $loop->post->ID;
endwhile;
wp_reset_postdata();
endif;
// Testing output
print_pr($product_ids);
This code is tested and works.

How to get custom post type by taxonomy id order by meta key sorting

I use woocommerce plugin.
I have products listed by category. I need to list them by price.
I tried this code below :
$args = array(
'post_type' => 'product',
'meta_key' => '_price',
'orderby' => 'meta_value_num',
'order' => $order,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cid
)
)
);
$my_query = new WP_Query( $args );
The result is not sorted by price, only by id.
Is there any solution?
products listed by category
<?php
$products_category_object= get_queried_object();
$product_category_taxonomy= $products_category_object->taxonomy;
$product_category_term_id= $products_category_object->term_id;
$product_category_name= $products_category_object->name;
$product_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => '_price',
'orderby' => 'meta_value_num', //meta_value Or meta_value_num
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $product_category_taxonomy,
'field' => 'id',
'terms' => $product_category_term_id
)
),
);
$product_my_query = null;
$product_my_query = new WP_Query($product_args);
if( $product_my_query->have_posts() )
{
while ($product_my_query->have_posts()) : $product_my_query->the_post();
echo get_the_title( $post->ID );
endwhile;
}
?>

Ordering products by price in custom wp_query loop

I currently have a very simple wp_query loop to loop through my WooCommerce products like so:
$args = array(
'posts_per_page' => -1,
'product_cat' => $cat,
'post_type' => 'product',
'orderby' => 'price',
'order' => 'DESC'
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
wc_get_template_part( 'content', 'product' );
}
This works as I want it to, except I can't get it to order the products by product price (ascending or descending) - what do I need to do to make this work?
Try this:
$args = array(
'posts_per_page' => -1,
'product_cat' => $cat,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc'
);
Hope it will help you.

WP_Query twice on one page - not getting the desired posts per page

I have a custom post type which has an ACF check box to define if an post is featured or not. I wanted to show 6 featured and 6 non featured posts on a page and so created 2 WP_Query loops both with separate args. Whilst the separation of featured and non-featured is working, the page only shows 6 posts in total and I'm not sure how to resolve that?
My code:
<?php
$args1 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new1 = new WP_Query($args1);?>
<?php while ($new1->have_posts()) : $new1->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( true == $field ):?>
Featured Posts
<?php endif;?>
<?php endwhile; wp_reset_postdata();?>
<?php
$args2 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new2 = new WP_Query($args2);?>
<?php while ($new2->have_posts()) : $new2->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( false == $field ):?>
Non-featured Posts
<?php endif;?>
<?php endwhile;?>
Your WP_Query isn't right. You do two completely identical queries ($args1 = $args2), and expects diffrent results from them. Also you put meta_key into query arguments, but without telling what and how to compare.
Depending on what type you have chosen, the right syntax, if your meta filed is named featured and the query can be:
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => true //or 1, or 'yes` depending of ACF type you have choose
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => false//or 0, or 'no`
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
or if this featured field exists only in featured posts then
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'EXISTS',
),
)
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'NOT EXISTS',
),
)
);
Check ACF query or Codex for the right sintax.
Your arguments should be quoted:
array(
'post_type' => 'fairs',
'posts_per_page' => 6,
'meta_key' => 'start',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
Array keys can either be integers, or strings. Read more in the PHP array() docs.
You should also only use posts_per_page in this case (and remove showposts, which was replaced by posts_per_page in WP 2.1).

Show pagination and exclude category

So, I am trying to show items from a specific category in the woocommerce:
Here is what I have so far:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => '', 'orderby' => 'date', 'order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<div class="content"> Content </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
When the product_cat is empty, it shows all the items. I want to include "exclude category".
For example, I want to show all but items in a "no_good" category.
Could someone help me out with it?
Also, how can I add a pagination to this?
Thanks!
Is product_cat your custom taxonomy? If it is then you need to modify your $args with tax query:
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'tax' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => array('no_good'),
'operator' => 'NOT IN',
),
),
);
...
This assumes that "no_good" is a product_cat name. Adjust field if it isn't.
Regarding the pagination part, do check the codex article regarding pagination.

Categories