Woocommerce products query - order_by - wordpress

I am using a query in my template.php file to display wooCommerce products by category.
My query looks like this:
<?php
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 99,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'system_components',
'menu_order' => 'asc'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'add-ons',
'menu_order' => 'asc'
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
)
));
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) :
$loop->the_post(); ?>
.....
This works, and I can see the products I want and the one I don't want are not visible.
The only bit I don't understand is that 'menu_order' => 'ASC' doesn't seem to work.
I doesn't' matter what I type as menu order in the product settings, the order doesn't change.
What am I doing wrong here?
Thanks

Remove 'menu_order' => 'asc' part from tax_query and add to main, that should work:
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 99,
'orderby' => 'menu_order',
'order' => 'ACS',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'system_components'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'add-ons'
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
)
));
As you can see in the documentation, the tax_query hasn't parameter menu_order.

Please, Set page order in your product.
For this go to Admin panel. Click on Products menu from left menu, Then edit product and set page order.

Related

How to query Standard and Video Post Format in WordPress?

I am trying to query posts according to their post format. Currently, I have the video, gallery, and standard post formats on my site. It's quite easy to query these post formats individually. However, I have a combination of tax queries of 10 posts that are standard and video post format, my query fails.
Here's the code I have right now:
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'relation' => 'OR',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
I think, you should put the 'relation' => 'OR', into the 'tax_query'-Array.
You are adding 'relation' => 'OR' in the wrong place. it should be inside the tax_query array. check the below code.
$args = array(
'post_status' => 'publish',
'orderby' => 'date',
'posts_per_page' => 10,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'operator' => 'IN',
'terms' => array( 'post-format-video' )
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video', 'post-format-gallery' ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
USEFUL LINKS
taxonomy-parameters

How do I query posts that don't have both of two tags?

Say I have two tags, red and small. I want to get all posts that are not red and small. I want to include posts that are either red or small, but they can't be both.
The tag__not_in parameter won't work, because it'll exclude posts that are just red or just small.
I also tried the tax query below but then realized it's the same as the above.
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
'operator' => 'NOT IN',
),
)
I feel like there's got to be a way to do this and I'm just missing something simple. That and I'm getting confused by the logic inversions. Any ideas?
You can try again
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'red',
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'small',
),
)
);
$query_red_small = new WP_Query( $args );
$post_ids = wp_list_pluck( $query_red_small->posts, 'ID' );
$args = array(
'post_type' => 'post',
'post__not_in' => $post_ids
);
$query = new WP_Query( $args );
?>

one post per term taxonomx

I need some help, again.
I use this query:
$tax = get_the_terms($id, 'coupon_category');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => 4,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'coupon_category', // Taxonomy
'field' => 'slug',
'terms' => $tax[0]->slug, // Your category slug
),
array(
'taxonomy' => APP_TAX_STORE,
'field' => 'slug',
'terms' => $term->slug,
'operator' => 'NOT IN',
),
),
) );
I know need a solution to only show one post from each Taxonomy term APP_TAX_STORE. Tried some solutions from this userfull forum but non worked for me.

How to make query with three different parameters

I have three different parameters to search with, first one is post title of custom post type, second is taxonomy term and third is post type also, I am able to search if three of them are selected, but how to search related records if two of them are selected only or just one of them ?
$args = array(
'post_type' => 'course',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'course-category',
'terms' => $cs_course_cate,
'field' => 'term_id',
)
),
'meta_query' => array(
array(
'key' => 'cs_course_campus_id',
'value' => $cs_campus,
'compare' => 'LIKE',
),
),
);
View Search Box Here
Try this code.
$args = array(
'post_type' => 'course',
'post_status' => 'publish'
);
if(!empty($cs_course_cate)){
$args = array('tax_query' => array(
array(
'taxonomy' => 'course-category',
'terms' => $cs_course_cate,
'field' => 'term_id',
)
));
}
if(!empty($cs_campus)){
$args = array('meta_query' => array(
array(
'key' => 'cs_course_campus_id',
'value' => $cs_campus,
'compare' => 'LIKE',
),
)
);
}

Sorting Woo Commerce Product by sku for a specific category

I need to sort products for a specific category in WooCommerce by sky. Tried different combinations but non of work. Like I tried the bellow
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'sku',
'order' => 'asc',
'posts_per_page' => $atts['per_page'],
'meta_query' => $meta_query,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'terms' => array_map('sanitize_title', explode(',', $atts['category'])),
'field' => 'slug',
'operator' => $atts['operator']
),
array(
'taxonomy' => 'product_tag',
'terms' => array_map('sanitize_title', explode(',', $atts['tags'])),
'field' => 'slug',
'operator' => $atts['operator']
)
)
);
$products = new WP_Query($args);
Where $attrs[] data are coming from shortcode parameters. But it does not sort product by sku ?
A help will be greatly appreciated.
The product sku (_sku) is saved as meta value, thus you cant simply say "order by sku", you should order by meta value, and set the meta keyto _sku. If they do not have a sku set the products will not be displayed.
Example:
args = array(
'post_type' => 'product',
'meta_key' => '_sku',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_sku',
'compare' => 'EXISTS',
),
),
);
If your sku is numeric you should use "meta_value_num" if the sku's are strings then use "meta_value" instead.
You can read more about it here -> https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

Resources