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.
Related
I'd like to exclude the category of my current post from the loop. Pretty easy usually, this time it doesn't work and I can't figure out what's wrong here.
Here's my page'code:
$postid = get_the_ID(); // curret product ID
<section class="related products">
<?php
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'cat' => '-33' // Exclude cat
);
$related_products = new WP_Query($args);
?>
<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php if( $related_products->have_posts() ) {
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile; }
?>
</section>
End of page
<?php
wp_reset_postdata();
?>
It shows all products (except the displayed one, which is correct).
Do you have any suggestion?
Try with the following additional tax_query, as product categories are a custom taxonomy:
<?php
$related_products = new WP_Query( array(
'post__not_in' => array( get_the_ID() ), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( 33 ), // HERE the product category to exclude
'operator' => 'NOT IN',
) ),
) );
if( $related_products->have_posts() ) : ?>
<h2><?php esc_html_e( 'Related products' ); ?></h2>
<div class="owl-carousel rigid-owl-carousel" >
<?php
while( $related_products->have_posts() ) : $related_products->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
?>
</div>
<?php endif; ?>
You can get current post's category using get_the_category
and you can exclude categories using category__not_in in your argument.
So your argument should be like bellow
$args = array(
'post__not_in' => array($postid), // Exclude displayed product
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '6',
'category__not_in' => get_the_category(get_the_ID())//exclude category of current post
);
Try this then let me know the result. Thanks
Im successfully filtering ALL my Wordpress posts by Likes (count) with a Custom Plugin (and meta_key) wich also let me filter the most liked posts in categories. I display (query) the result in a custom page template. Everything works fine.
The Like function works also on Woocommerce Products. But so far i was not able to set up a page where i sort the Products (post_type) the same way in a specific shop cateogry as i do it with my posts. The closest i came was to display the most liked posts on the page BUT the sorting for a category does not filter the posts - it displays the same as in the main page. The category List and the url call for the cateogy links are working fine.
NOTE: THE url query-string "product-cato" is a custom one - im using a ajax filter plugin wich uses this query-string and the category id like .../?product-cato=6Please do not mix it up wicht product_cat for example
This is what i came up so far - beginning with the code for the posts (wich works fine). Any idea how to solve this issue? thx
The query (works fine)
if (isset($_GET['category'])) {
$args = array(
'meta_key' => '_recoed',
'meta_compare' => '>',
'meta_value' => '0',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'category_name' => sanitize_text_field($_GET['category']),
'paged' => $paged
);
}
query_posts($args);
The Category List to Filter the Post in each Category (works fine)
<?php $categories = get_categories();
foreach($categories as $category) { ?>
<li>
<a class="popular-categories" href="<?php echo get_permalink(); ?>?category=<?php echo $category->category_nicename; ?>"><?php echo $category->name; ?></a>
</li>
<?php } ?>
Now the Woocommerce Query and Category Part where I am stucks
The Query for the Products post_type
if (isset($_GET['product-cato'])) {
$args = array(
'meta_key' => '_recoed',
'meta_compare' => '>',
'meta_value' => '0',
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'taxonomy' => sanitize_text_field($_GET['product_cat']),
'paged' => $paged
);
query_posts($args);
The Category List to Filter the Post in each Shop-Category
<?php
$product_categories = get_terms( 'product_cat' );
$count = count($product_categories);
foreach ( $product_categories as $product_category ) { ?>
<li>
<a class="popular-categories" href="<?php echo get_permalink(); ?>?product-cato=<?php echo $product_category->term_id; ?>"><?php echo $product_category->name; ?></a>
</li>
}
?>
As Product Categories are a custom taxonomy 'product_cat', use a tax_query instead.
So instead of wrong:
'taxonomy' => sanitize_text_field($_GET['product_cat'])
… use this (defining correctly the 'field' argument):
'tax_query' => array( // the product category query
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', // (also 'name' or 'slug') <== <== <== <== <==
'terms' => sanitize_text_field($_GET['product-cato']),
),
),
Check also in sanitize_text_field($_GET['product_cat']) that 'product_cat' is the right slug, as you use also product-cato…
If sanitize_text_field($_GET['product_cat']) is not a product category "slug", you should need to change 'field' => 'term_id', with the correct field type ('name' or 'slug').
So your code should be (defining correctly the 'field' argument):
if (isset($_GET['product-cato'])) {
query_posts( array(
'meta_key' => '_recoed',
'meta_compare' => '>',
'meta_value' => '0',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => $paged,
'post_type' => 'product',
//'posts_per_page' => 20,
'post_status' => 'publish',
// The product category query
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', // (also 'name' or 'slug') <== <== <== <==
'terms' => sanitize_text_field($_GET['product-cato']),
),
),
) );
}
It should work …
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();
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.
I am trying to create a custom shortcode for WooCommerce, I want to display featured products from a specific catagory at the end of a post.
There is a standard shortcode:
[featured_products per_page="12" columns="4" orderby="date" order="desc"]
I want to add catagory to this, so the new shortcode will be:
[featured_category_products category="13" per_page="4" columns="4" orderby="date" order="desc"]
To get it work it's officiously necessary to create a function for it, so I found the class-wc-shortcodes.php file with all the default shortcodes.
I add a new function based on the default featured product:
public function featured_category_products( $atts ) {
global $woocommerce_loop;
extract(shortcode_atts(array(
'category' => '',
'per_page' => '4',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc'
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
),
array(
'key' => '_featured',
'value' => 'yes'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr($category) ),
'field' => 'slug',
'operator' => 'IN'
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
I added the category variable extraction (and checked if it worked) and added tax-query part (found it from another function that show product based on categories). So I thought this should work, but off course not. I don't get any results nor a error.
Anybody an idea how the get this to work?
If you're only looking for one product category, use 'terms' => $category. Or you can do an explode() to split a comma separated string into an array.
See Wp Query : Taxonomy Parameters.
Other notes:
Don't touch the plugin files, your changes will be lost in the next update.
Create your own plugin.
Copy the function you created to it.
Register the shortcode to make it available (using your custom function as callback):
add_shortcode( 'featured_category_product', 'featured_category_product_function' );