Wordpress custom field order by - php

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 );

Related

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 WP_User_Query() combine query

How to combine wordpress user query look like this example from another question Wordpress combine queries
this question have answer for this it use WP_Combine_Queries class for realizing but this answer only for posts and this not work for users query
$args1 = array(
'role' => 'specialist',
'meta_key' => 'profession',
'meta_value' => $term->ID,
'meta_query' => array(
array(
'key' => 'be_first',
'compare' => 'EXISTS',
),
array(
'key' => 'pro_date',
'value' => current_time('timestamp'),
'compare' => '>'
)
),
'orderby' => 'be_first',
'order' => 'DESC',
'number' => 0,
'fields' =>'ID',
'count_total' => true,
);
$args2 = array(
'role' => 'specialist',
'meta_key' => 'profession',
'meta_value' =>$term->ID,
'meta_query' => array(
array(
'key' => 'pro_date',
'value' => current_time('timestamp'),
'compare' => '>'
)
),
'orderby' => 'pro_date',
'order' => 'DESC',
'number' => 0,
'count_total' => true,
);
$args3 = array(
'role' => 'specialist',
'meta_key' => 'profession',
'meta_value' =>$term->ID,
'orderby' => 'like',
'order' => 'DESC',
// 'offset' => $offset,
// 'number' => $number,
// 'paged' => $paged,
'count_total' => true,
);
$args = array(
'number' => $number,
'paged' => $paged,
'offset' =>$offset
'sublimit' => 1000,
'args' => array( $args1, $args2, $args3),
);
$results = new WP_Combine_Queries( $args );

WP_Query not ordered correctly

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'
)
)
);

Wordpress post query on more than one condition

I have the following function which returns only future events, which works great:
$args = array(
'post_type' => self::POST_TYPE,
'posts_per_page' => $posts_per_page,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'status' => 'publish',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
)
)
);
The problem I have is, I also need to check whether a custom field called "post_is_global" has been set (the type is BOOL by the way) but I don't know how to implement it into this query. Any help would be greatly appreciated.
Many thanks!
The query should look somewhat like this:
$args = array(
...
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => date('Ymd'),
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'post_is_global',
'value' => '1',
'compare' => '=',
),
)
);
$query = new WP_Query($args);
References:
ACF | Query posts by custom fields
Class Reference/WP_Query

Wordpress Query Multiple Arguments Set Based on URL Variables

So I have a custom post type and have created a handful of links in the breadcrumbs to allow sorting of the posts. The sorting links set variables within the URL that are then pulled to determine the arguments for the query.
This is a jumbled mess. But it functions as expected. However, there has to be a better way to do this than this mess of code. The only thing not working with this code is if NO variables are set in the URL it returns NO RESULTS instead of ALL RESULTS.
I am totally open if someone has a better way to sort posts by different meta keys/values on the fly/with links.
These are the variables its pulling from the URL:
$sortby = $_GET['sort'] or $sortby = 'price';
$direction = $_GET['dir'] or $direction = 'desc';
$automake = $_GET['make'] or $automake = '';
$autocat = $_GET['model'] or $autocat = '';
$searchkey = $_GET['s'] or $searchkey = '';
$autocondition = $_GET['cond'] or $autocondition = '';
And here is the jumbled up Query/Args code.
if ( $s !== '' ) {
$args = array (
'post_type' => 'inventory',
'post_status' => 'publish',
'posts_per_page' => -1,
's' => $searchkey,
'order' => $direction,
'orderby' => $orderby,
'meta_key' => $sorting,
'meta_query' => array(
array(
'key' => '_auto_sold',
'value' => 'No',
'compare' => '=',
'type' => 'CHAR',
),
),
);
} elseif ( $autocondition !== '' ) {
$args = array (
'post_type' => 'inventory',
'post_status' => 'publish',
'posts_per_page' => -1,
's' => $searchkey,
'order' => $direction,
'orderby' => $orderby,
'meta_key' => $sorting,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_auto_sold',
'value' => 'No',
'compare' => '=',
'type' => 'CHAR',
),
array(
'key' => '_auto_status',
'value' => $autocondition,
'compare' => '=',
'type' => 'CHAR',
),
),
);
} elseif ( $automake AND $autocat !== '' ) {
$args = array (
'post_type' => 'inventory',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => $direction,
'orderby' => $orderby,
'meta_key' => $sorting,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_auto_sold',
'value' => 'No',
'compare' => '=',
'type' => 'CHAR',
),
array(
'key' => '_auto_make',
'value' => $automake,
'compare' => '=',
'type' => 'CHAR',
),
array(
'key' => '_auto_model',
'value' => $autocat,
'compare' => '=',
'type' => 'CHAR',
),
),
);
} elseif ( $automake !== '' ) {
$args = array (
'post_type' => 'inventory',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => $direction,
'orderby' => $orderby,
'meta_key' => $sorting,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_auto_sold',
'value' => 'No',
'compare' => '=',
'type' => 'CHAR',
),
array(
'key' => '_auto_make',
'value' => $automake,
'compare' => '=',
'type' => 'CHAR',
),
),
);
} elseif ( $autocat !== '' ) {
$args = array (
'post_type' => 'inventory',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => $direction,
'orderby' => $orderby,
'meta_key' => $sorting,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_auto_sold',
'value' => 'No',
'compare' => '=',
'type' => 'CHAR',
),
array(
'key' => '_auto_model',
'value' => $autocat,
'compare' => '=',
'type' => 'CHAR',
),
),
);
} else {
$args = array (
'post_type' => 'inventory',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => $direction,
'orderby' => $orderby,
'meta_key' => $sorting,
'meta_query' => array(
array(
'key' => '_auto_sold',
'value' => 'No',
'compare' => '=',
'type' => 'CHAR',
),
),
);
}
// The Query
$query = new WP_Query( $args );
In the else part use
$args = array ('post_type' =>'inventory', 'post_status' => 'publish','posts_per_page' => -1 );
This would work

Categories