WooCommerce seems to only orderby date and not price - php

I am loading in variable products via a custom WP_Query
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'date',
'order' => 'desc'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="product-node cat-beast-balls">
<?php wc_get_template_part( 'content', 'single-product' ); ?>
</div>
<?php endwhile;
}
wp_reset_postdata();
This seems to work fine. However, I am using ajax to reload the products but with a different loop such as this one.
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'price',
'order' => 'asc'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="product-node cat-beast-balls">
<?php wc_get_template_part( 'content', 'single-product' ); ?>
</div>
<?php endwhile;
}
wp_reset_postdata();
I can notice however that between 'asc' and 'desc' the order is flipped, so at least that's working. My problem is that the orderby value seems to make no difference. How can I make it so that the loop changes whether or not the products are ordered by date or price?
Thanks all!

Try this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc'
);

Related

WooCommerce products custom loop filtered by an array of SKUs

I have an array which has a couple of product SKU's in it I want to dispay the products that are connected to the SKU's.
I found the custom woocommerce product loop, but can't find any documentation on how to extend this.
$skus = array(sku1, sku2, sku3);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
}
wp_reset_postdata();
In a WP_Query, you will need to use a meta query to get products that are connected to an array of SKUs.
The code:
$skus = array('sku1', 'sku2', 'sku3');
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => '_sku',
'value' => $skus,
'compare' => 'IN',
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
endif;
wp_reset_postdata();
Tested and works.
See WP_Query and custom field post meta parameters documentation
You could try to do this
$skus = array(sku1, sku2, sku3);
$meta_query = array['relation' => 'OR',];
foreach ($skus as $sku) {
$meta_query[] = array(
'key' => '_sku',
'value' => $sku,
'compare' => '='
),
}
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => $meta_query
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
}
wp_reset_postdata();

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 do I add an echo inside an array?

I'm trying to show products from a certain category on a page like his:
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'prcategory1', 'orderby' => 'price');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
This one works. However, I'd like 'prcategory1' to be taken from a custom field of the page. Something like this (incorrect code incoming):
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'get_post_meta(get_the_ID(), 'custom_cat_name', TRUE); ?>', 'orderby' => 'price');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
There is some errors in your code, try this for your array :
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => get_post_meta(
get_the_ID(),
'custom_cat_name',
TRUE
),
'orderby' => 'price'
)

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.

How do I add order & orderby parameters to this wordpress query?

I managed to paint myself into a corner when using this snippet, but I can't manage to work out how to sort the query. Usually I can do it but with this snippet that excludes one tag on the tag page I can't really work it out. Anyone?
$exclude_tags = array(17);
global $wp_query;
$wp_query->set('tag__not_in', $exclude_tags);
$wp_query->get_posts();
if (have_posts()) : while (have_posts()) : the_post();
This example will be help full for you :-
$args = array(
'post_type' => 'post',
'meta_key' => 'pb_issue_featured',
'orderby' => 'meta_value',
'order' => 'DESC',
'posts_per_page' => $posts,
'paged' => $paged,
'paged' => 1,
'meta_query' => array(
array(
'key' => 'headline',
'value' => 1,
'compare' => '!='
)
)
);
add_filter( 'posts_orderby', 'filter_query' );
$q = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_query' );
function filter_query( $query ) {
$query .= ', wp_posts.menu_order ASC';
return $query;
}
Referenced From
Please look at this example. You can do like this.
The code will display the title of last ten posts sorted alphabetically in ascending order.
<?php
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div>
<?php the_title(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
Please refer the link http://codex.wordpress.org/Template_Tags/get_posts for more details.

Categories