Wordpress orderby meta_value not ordering properly - php

I have a custom post type called 'events' and I am to display the first 4 more recent events in event date order on the home page. I have got the events to show on the home page however the ordering doesn't seem to be working (see below the example). It seems to be ordering in its own way.
$args = array('post_type' => 'events', 'meta_key' => 'event-date', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => -1);
$events = new WP_Query( $args );
That is my code and here are the results (the dates) I get back.
16/04/2014
16/05/2014
19/03/2014
25/02/2014
27/02/2014
28/02/2014
As you can see, this is not ordering by ASC so what have I done wrong?!
Thanks in advance

Try to use meta_value_num instead of meta_value in orderby parameter. Use following code:
$args = array(
'post_type' => 'events',
'meta_key' => 'event-date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1
);
$events = new WP_Query( $args );

Related

WordPress sort wp_query multiple meta_values

I would like to sort my post and tried nearly everything to make it work.. but no luck :-(
In my loop is a custom post type called deal and expired deal (meta_value through ACF) posts. I want to show the normal posts first and then the expired posts.
This is my code so far:
$args = array(
'posts_per_page' => -1,
'post_type' => 'deal',
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => array($not_in),
);
Any ideas how I can put the "expired" posts behind the normal posts?
What you want to do is set orderby to meta_value and meta_key to your custom field.
$args = array(
'posts_per_page' => -1,
'post_type' => 'deal',
'meta_key' => 'YOURCUSTOMFIELDHERE',
'orderby' => 'meta_value',
'order' => 'DESC',
'post__not_in' => array($not_in),
);
If the DESC order is the wrong direction you can switch it to ASC.

WordPress: switch meta_query orderby in the frontoffice

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

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

Wordpress loop for multiple custom post types

Looking for a way to loop through multiple custom post types and get the 4 most recent posts.
I have the below but doesn't quite work as if you 2 posts of the same post type, it'll show the most recent rather than showing most recent of ALL custom posts.
$args = array('post_type' => array('cs_trainee', 'cs_graduates', 'cs_pros', 'sd_trainee', 'sd_graduates'), 'posts_per_page' => 4, 'orderby' => 'menu_order', 'order' => 'ASC');
$careers = new WP_Query( $args );
Thanks
Just change the query_posts parameters a bit....
query_posts( array(
'post_type' => array( 'post', 'report', 'opinion', bookmark' ),
'cat' => 3,
'orderby' => 'date',
'order' => 'DESC',
'showposts' => 5 )
);
That should take care of it for you.

Display only future events (cpt)

My current code shows 5 posts from the cpt 'events'. This is the code i'm using:
//get post type ==> events
global $post;
$args = array(
'post_type' => 'events',
'numberposts' => $data['home_events_count'],
'orderby' => 'meta_value',
'meta_key' => 'timestamp_earth_event_start_date',
'meta_query' => $meta_query
);
I want to filter out posts (events) that their 'timestamp_earth_event_start_date' are older than the current date.
I've tried to modify the code myself, but still past events are shown.
Any help would be most appreciated!
Try this one passing query string
$numposts=$data['home_events_count'];
query_posts('post_type=events
&post_status=publish&numberposts=$numposts
&posts_per_page=1
&meta_key=timestamp_earth_event_start_date
&orderby=meta_value_num
&order=DESC')
By passing array
$args = array(
'post_status' => 'publish',
'post_type' = 'events' ,
'meta_key' => 'timestamp_earth_event_start_date',
'order' => 'DESC',
'orderby' => 'meta_value_num'
);
$event_posts = new WP_Query( $args );
Other option you can add filter for the query like
add_filter( 'posts_orderby', 'my_posts_orderby_date', 10, 2 );
function my_posts_orderby_date( $orderby, $query ) {
global $wpdb;
return " CAST( $wpdb->postmeta.meta_value AS DATE ) " . $query->get( 'order' );
}

Categories