WP_Query not ordered correctly - php

I have a custom post type cota where all data is displayed in a <table> in my site. I need to order this table by:
administradora - some kind of "category" that the user specifies. Ordered ASC (a-z);
valor - The price of the cota. Each cota has a price. Ordered DESC (9 - 0) ;
The problem : It's already ordered by administradora, but not correctly ordered by valor.
Example: The order by valor displays first a cota with US$9.000,00 and then another cota with US$1.000.000,00. It's wrong. The one million dollar cota should come first, and then the nine thousand dollars cota.
Here's an image that shows better the situation
Code for ordering the cota:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'slug',
'terms' => $atts['tipo'],
),
),
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
'valor_clause' => array(
'key' => 'valor',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'valor_clause' => 'DESC'
)
)
);
EDIT 1: After some research, i found out that wordpress saves all its data as longtext in the database. But still cannot solve the problem.
I can provide more info if needed.

Try Below Code:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'id',
'terms' => $atts['tipo'],
),
),
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
'valor_clause' => array(
'key' => 'valor',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'valor_clause' => 'DESC'
)
)
);

SOLVED
The following code should work:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'slug',
'terms' => $atts['tipo'],
),
),
'meta_query' => array(
'relation' => 'AND',
'credito_clause' => array(
'key' => "valor",
'orderby' => 'meta_value_num',
'type' => 'DECIMAL',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'credito_clause' => 'DESC'
)
)
);

Related

Issue with Woocommerce query by meta and attributes

I have 3 products having meta key 'check_key' = 1. 2 of them have attribute 'pa_size_10_20' with values '100' and '30'; other one have attribute 'pa_size_30_40' with value '70'.
Please help me understand why the query below returns all 3 products? I expect only 2 products with 'pa_size_10_20' attribute.
$args = array(
'post_type' => 'product',
'visibility' => 'visible',
'post_status' => 'publish',
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'check_key',
'value' => '1',
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy'=> 'pa_size_10_20',
'field' => 'name',
'terms' => 0,
'operator' => '!='
)
)
);
$posts = get_posts( $args );
TIA
It seems as though your tax_query would return all taxonomy terms that aren't named 0 an integer?
I think if you know the values for IN then I would use those instead.
$args = array(
'post_type' => 'product',
'visibility' => 'visible',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'check_key',
'value' => '1',
'compare' => '='
),
),
'tax_query' => array(
array(
'taxonomy'=> 'pa_size_10_20',
'field' => 'name',
'terms' => array('100', '300'),
'operator' => 'IN'
),
),
);

wc_order_query with meta_query not working when using relation

I am trying to use WC_Order_Query, to get all orders where a custom meta_key doesn't exist, is empty or equal to 0
I've tried like a lot of the stuff documented on this site, but nothing seems to work. It just returns all content, which is the opposite of what i'm trying to do.
This is what most people have recommended so far, but it doesn't seem to work as intended or maybe I am not seeing the issue here
$args = array(
'limit' => 9999,
'return' => 'ids',
'orderby' => 'date',
'order' => 'DESC',
'status' => 'processing',
'date_created' => '>='.$startdate,
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
This ended up being the solution:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
'orderby' => 'the_date',
'order' => 'DESC',
'date_query' => array(
array(
'after' => $startdate . $starttime,
'inclusive' => true
)
),
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WP_Query( $args );
$query_posts = $query->get_posts();
Thanks a lot to Boris for the assistance.

Wordpress custom field order by

I'm trying to order by the custom field InPrice but not able to do that. I'd know if possible in another way makes it ('orderby' => 'meta_value_num', 'order' => 'ASC',)
$args = array(
'post_type' => 'post',
'posts_per_page'=>-1,
'meta_key' => 'InPrice',
'meta_value' => array( $pieces[0], $pieces[1]),
'meta_type' => 'numeric',
'meta_compare' => 'BETWEEN',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'btown' => array(
'key' => 'Town',
'value' => $town,
'type' => 'STRING',
'compare' => 'LIKE',
),
broom' => array(
'key' => 'Rooms',
'value' => $rooms,
'compare' => 'LIKE',
),
),
),
);
referred to Wordpress Documentation
you can use something like code below :
$args = array(
'post_type' => 'post',
'posts_per_page'=>-1,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'age',
'value' => array( array( $pieces[0], $pieces[1]) ),
'compare' => 'IN',
), // other meta queries that you want
//don't forget to set a relation type for them in here ex:'relation'=>'AND',
),
);
$query = new WP_Query( $args );

How can I get the number of products "on-sale" for a specific category?

I'm woocommerce 2.8, How can I count the products existing in the shop "on sale" for a specific category (ex: computers) ?
I found a way to count all products :
$count_posts = wp_count_posts( 'product' );
return $count_posts->publish;
But how can I get the number of products "on-sale" for a specific category ?
Thanks
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => here goes your category id, you can also go for slug
)
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$query = new WP_Query($args);
$onSaleCount = $query->found_posts;
The above looks for simple and variable products on sale belonging to a category mentioned under tax_query.
I'm not sure if there's a better method, but this should work.
LE - Changed args to exclude products out of stock as per comments.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => here goes your category id, you can also go for slug
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
),
array(
'key' => '_stock_status',
'value' => 'instock'
),
)
);

WordPress Loop Not Respecting Post Status

I have a series of loops spitting out 'event' custom post types that I want to only pull posts with a post_status of 'publish'. I've added the post_status of 'publish' in my wp_query array but it doesn't seem to work. I still have scheduled posts showing up.
<?php
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'meta_key' => '_expiration_date',
'orderby' => 'meta_value',
'order' => 'ASC',
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
$wp_query4 = new WP_Query($args_hotel_feature);
if($wp_query4->have_posts()) :
while($wp_query4->have_posts()) :
$wp_query4->the_post();
?>
Anyone else experienced this issue?
Use meta_query
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
)
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
Turns out the query was fine, however, the post expiration plug-in "Posts Expiration Date" breaks post_staus. Use "Post Expirator" instead.

Categories