wordpress query posts by meta date value - wordpress

I am trying to query a custom post type by a custom field that contains a date. My query looks like this:
query_posts(
array(
'showposts' => 500,
'post_type' => 'holidays',
'post_status' => 'publish',
'order' => 'desc',
'orderby' => 'meta_value',
'meta_query' => array
(
array
(
'key' => 'from_date',
'value' => array( '01-01-2012', '31-12-2014' ),
'type' => 'DATE', // TRIED: DATE, SIGNED, NUMBER
'compare' => 'BETWEEN'
)
),
));
My custom fields store the date like this: 19-11-2013
When i run the query no results are shown, although dates within the range exist.
Am i approaching this is the correct manor, or am i missing something?

I can confirm Dan White's answer is correct, when querying by a date range the required format needs to be 'YYYY-mm-dd'. Your final code should look like this:
query_posts(
array(
'showposts' => 500,
'post_type' => 'holidays',
'post_status' => 'publish',
'order' => 'desc',
'orderby' => 'meta_value',
'meta_query' => array
(
array
(
'key' => 'from_date',
'value' => array( '2012-01-01', '2014-12-31' ),
'type' => 'DATE',
'compare' => 'BETWEEN'
)
),
));

Related

Ordering posts by custom field within another query

I've googled and tried to figure this out on my own but here goes. I'm querying a custom post type and certain posts within that custom post type with an ID of 180. I then need to order these posts by a custom field. This is my code:
$namskeid = new WP_query(array(
'posts_per_page' => '6',
'post_type' => 'namskeid',
'meta_query' => array(
array (
'key' => 'course_type_display',
'compare' => 'LIKE',
'value' => get_the_ID( 180 )
))
));
Is there any way to do this?
I finally figured it out. I failed to specify meta_type = Date and orderby => 'meta_value_num'.
Here is the code that works:
$namskeid = new WP_query(array(
'posts_per_page' => '6',
'post_type' => 'namskeid',
'meta_type' => DATE,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'course_date',
'meta_query' => array(
array (
'key' => 'course_type_display',
'compare' => 'LIKE',
'value' => get_the_ID( 180 )
))
));

Wordpress: compare one date coming as custom field with current date

I would like to know how to compare a date coming as custom field with the current date in a custom loop. I have following script, but it does not work...
enter$args = array(
'post_type' => 'books',
'meta_key'=>'course_date',//the format comes like this Ydm
'meta_value'=> date('d.m.Y'),
'meta_compare'=> '<'
);
My goal is to show all books with when the date is > then the current date. If the current date is > then the related post must be hidden.
Use this Query to get past event list ( 'compare' => '<') and get future events ( 'compare' => '>=')
$event_args = array(
'post_type' => 'books',
'meta_query' => array(
array(
'key' => 'course_date',
'value' => date('Ymd'),
'type' => 'DATE',
'compare' => '<'
)
),
'paged' =>$page,
'meta_key' => 'course_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);

WP_Query Order By Two Meta Fields

I need to order a WP_Query by two meta fields: meta-order and meta-last-name. First by meta-order numerically, if blank, order by meta-last-name ASC. How do I do that, the documentation is not very clear on that. I've tried this but it does not work.
array(
'post_type' => 'student',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'meta-order meta-last-name',
);
You will need to use the pre_get_posts action to be able to achieve this.
See answers from https://wordpress.stackexchange.com/q/169999 on how to do it.
This worked for me:
array(
'post_type' => 'student',
'meta_query' => array(
'relation' => 'OR',
'custom_order_clause' => array(
'key' => 'meta-custom-order',
'compare' => 'LIKE',
),
'last_name_clause' => array(
key' => 'meta-last-name',
'compare' => '=',
),
),
'orderby' => array(
'custom_order_clause' => 'ASC',
'last_name_clause' => 'ASC'
),
)
CAVEAT:
Make sure you delete empty meta values from DB otherwise this will not work.

Wp_query with 2 meta keys and array of meta values

Hi in my post_type=shop i have 2 meta keys and array of values
Custom fields
Name Values
cu_status pending,processing,completed
cu_date 12-Jan-2016 , 13-Jan-2016, ...... any date in the same format date("d-M-Y")
Now i need to loop through all posts with cu_status =pending,processing and cu_date is between 12-Jan-2016 to 13-Apr-2016
What will the query ?
Iam very confused . For to get all post with status pending,processing I know the query
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
)
),
'posts_per_page' => -1
);
Pleases help to complete the query .
You need to use the relation operator e.g.
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
) ,
array(
'key' => 'cu_date',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
),
'posts_per_page' => -1
);
Also use the compare=>BETWEEN for getting the difference in 2 dates.
You may need to tweak the code a bit as I have not tested it.

wp_query on 2 post types, one having custom field for further filtering

This is my first question on here; I have hunted around for an answer but if it is out there already apologies.
I am using wp_query() to fetch both 'posts' and a custom post type called 'reviews'. However the custom post type has a sub type and I need to filter out all but one of these sub types just for reviews in the same query!
This is where I got to:
$type = array( 'post', 'review' );
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array( // I need to filter the review post type further by subtype
array(
'key' => 'subtype',
'value' => 'review'
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);
Obviously, when I run this the default post type is not listed as it doesnt have the subtype custom field. How can I only apply this filter (meta_query) to the custom post type in this query and not have it apply to the default post type 'post'?
Thanks in advanced,
Kris...
UPDATE:
OK, I have tried this but with no success - any ideas?
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'subtype',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'subtype',
'value' => 'review',
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);
This will check the 'subtype' only if it exists in the post (not tested)
$type = array( 'post', 'review' );
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => AND,
array(
'key' => 'subtype',
'compare' => 'EXISTS'
),
array(
'key' => 'subtype',
'value' => 'review'
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);

Resources