WordPress: switch meta_query orderby in the frontoffice - php

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

Related

Display and order using two meta keys

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'.

Order by multiple custom field values in wordpress

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

WordPress WP_Query: Display custom post type based on custom meta value, and also order on another custom meta value

I am using the WordPress plug-ins Advanced Custom Fields, and Custom Post Type UI.
I have built a WP_Query to display a particular post type, filtered by a particular custom field value.
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'news_story_type',
'meta_value' => 'release',
'posts_per_page' => 3
) );
I now want to sort the resulting posts by another custom field, date_of_publication rather than use WordPress's menu_order or date. The ACF documentation says to specify orderby and meta_key in the query args.
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC' );
But alas, doing so conflicts with the meta_key I've already supplied to filter.
Has anyone encountered this before and found a solution?
Try using meta_query
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 3,
'meta_query' => array(
array('key' => 'news_story_type', 'value' => 'release')
)
) );

Ordering the output of a loop by a custom field containing a datestamp

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

Wordpress query by multiple metaboxes and order by date

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

Categories