I have a custom field called "sale_status" and the values ("For Sale", "Sold", "Let") are displayed in a radio button in admin. Now each of them can be assigned to a single property.
Currently the query fetches the properties order by post date but I want it to be by sale_status and then date.
My code is as below--
$args = array(
'post_type' => 'zoo-property',
'posts_per_page' => $query__per_page,
'post_status' => 'publish',
'paged' => $query__page,
'meta_key' => 'sale_status',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
$query__types,
$query__locations,
$query__statuses,
$query__investments,
$query__price
)
);
$properties_wp_query = new WP_Query($args);
echo "Last SQL-Query: {$properties_wp_query->request}";
But it not showing in correct order.
Any help is highly appreciated. Thanks in advance.
When you want to include multiple sort criterion in the same query the orderby value must be an array. See WP_Query documentation for details. Try something like this:
$args = array(
'post_type' => 'zoo-property',
'posts_per_page' => $query__per_page,
'post_status' => 'publish',
'paged' => $query__page,
'meta_key' => 'sale_status',
'orderby' => array( 'meta_value_num' => 'DESC', 'post_date' => 'DESC' ),
'meta_query' => array(
'relation' => 'AND',
$query__types,
$query__locations,
$query__statuses,
$query__investments,
$query__price
)
);
Related
I am using WP_Query to get posts that have a specific value in one of the ACF fields. I also need to order them by a separate ACF field. I am not sure how to accomplish this. Everything I've read says to use 'orderby' => 'meta_value' but I believe thats the value of the field I'm filtering the posts by, which is not what I want.
This is what I have right now..
$args = array(
'post_type' => 'contacts',
'posts_per_page' => -1,
'meta_key' => 'department',
'meta_value' => 'Transportation',
'orderby' => 'meta_value'
);
$the_query = new WP_Query( $args );
I need to orderby an ACF field named last_name.
It's possible to assign a name to a meta query, and then refer to that name in your orderby. Something like this.
$args = array (
'post_type' => 'contacts',
'post_status' => 'publish',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array( 'main_query' => array(
'key' => 'department',
'value' => 'Transportation'
), 'orderby_query' => array(
'key' => 'last_name',
)
),
'orderby' => array(
'orderby_query' => 'ASC',
),
);
$the_query = new WP_Query( $args );
Hi I have a custom wordpress page using a query for a meta key if the product is on special for a specific store, however I need to order them on page by a different meta_key called wpcf-order-by.
The following query does not work, it displays my items but not according to the wpcf-order-by.
what am I doing wrong, I have searched and I cannot find anything that suits my case
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'store_opening',
'meta_value' => 'yes',
'orderby' => 'wpcf-order-by',
'order' => 'asc',
'posts_per_page' => '-1'
);
You need to use meta_query for that.
meta_query fits to cases where you are working with more than one meta keys in your wp query requests.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'wpcf-order-by',
'orderby' => 'meta_value',
'meta_query' => array(array(
'key' => 'store_opening',
'value' => 'yes',
)),
'order' => 'asc',
'posts_per_page' => '-1'
);
If the values of 'wpcf-order-by' are numbers, then set 'orderby' => 'meta_value_num' instead of 'meta_value'.
Was looking for a while for a solution, but couldn't find one. So my question is, I have this code that sets posts order by default its DATE:
$args = array(
'post_type'=>'paibcresume',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(),
'tax_query' => array(),
'orderby' => 'date',
'meta_key' => '',
'order' => 'DESC'
);
I need some kind of a switch on the website, so user can pick how to order posts, for example it could be date to order by date, or modified to order by date of modification, or it could be a custom meta_key. How could I do that?
Check below url
http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
$query = new WP_Query( array ( 'post_type' => 'product', 'orderby' => 'meta_value_num', 'meta_key' => 'price' ) );
I'm using a custom field that stores the timestamp of a date entered by the user. I want to display posts sorted by that custom date, but they refuse to order by custom field.
Here I register the custom field using this custom meta boxes tool - https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
// the prefix is _cmb_
array(
'name' => 'Start Time',
'id' => $prefix . 'start_time',
'type' => 'text_datetime_timestamp',
),
Then I use WP_Query to loop through the posts with the following arguments:
$args = array(
'post_type' => 'talks',
'posts_per_page' => -1,
'orderby' => '_cmb_start_time',
'order' => 'ASC'
); // show talks ordered by start time
$custom_query = new WP_Query($args);
This prints out the posts in the order they were added and not in the order of the custom field date.
Update
I was also trying to filter by another custom field type "room" with a value equal to "room 1":
$args = array(
'post_type' => 'talks',
'posts_per_page' => -1,
'orderby' => '_cmb_start_time',
'order' => 'ASC',
'meta_key' => 'room',
'meta_value' => 'room 1'
);
Solution:
With #joebuckle 's solution I ended up with this version that works great:
$args = array(
'post_type' => 'talks',
'posts_per_page' => -1,
'meta_key' => '_cmb_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'room',
'value' => 'room 1',
'compare' => '='
)
));
try it like this instead (ref WP_Query)
$args = array(
'post_type' => 'talks',
'posts_per_page' => -1,
'meta_key' => '_cmb_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC'
); // show talks ordered by start time
$custom_query = new WP_Query($args);
So here is my query:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'meta_key' => 'event_informations_show_on_the_homepage',
'meta_value' => 'Show on the homepage',
'meta_compare' => '==',
'meta_key' => 'event_informations_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
I want to select all posts that have the metabox event_informations_show_on_the_homepage and the value of the metabox event_informations_show_on_the_homepage and order by the date metabox which is stored as a timestamp and is called event_informations_date.
What am I doing wrong?
Hopefully I'm not barking up the wrong tree here.
You can use the key 'meta_query' to filter posts by multiple meta keys like so:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_informations_show_on_the_homepage',
'value' => 'yes',
),
array(
'key' => 'event_informations_date',
'value' => 'yes',
)
)
);
$query = new WP_Query( $args );
What WordPress is doing here is creating multiple wheres against the same column by using innerjoins on the same table, each time using a different alias. It's pretty cool & is probably the fastest way to query like that.
For more information see here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Hope this helps :)