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

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

Related

Custom Post Type ACF date field wordpress

Good friends I have a custom post type with custom fields ACF of type date I have the following query:
$hoy= date('Ymd');
$args = array(
'post_type' => 'celebridades', // name custom post type
'nopaging' => true,
'orderby' => 'meta_value_num',
'meta_key' => 'nacimiento_', //ACF date
'meta_query' => array( array(
'key' => 'nacimiento_',
'value' => $hoy,
'compare' => '>=',
'type' => 'DATE'
))
);
with the above query I get all the custom inputs,
I need to filter the entries according to the current day and month, the idea is to be able to visualize a list of celebrities who were born on a date like today.
Hi you can try this code:
<?php
$today = date("Y-m-d");
$args = array(
'post_type' => 'celebridades', // name custom post type
'posts_per_page' => 4,
'meta_key' => 'nacimiento_', //ACF date
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_value_num' => $today,
'meta_compare' => '>'
);

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

Order by query post wordpress by select field

I have a select field created with custom field . Field contains a list of numbers from 1 to 10 .
How could do to make the post an ORDER BY query this select?
"orden" is the field select type
Maybe the problem is because the field select is a string?
$args = array(
'post_type' => 'directivo',
'orderby' => 'orden',
'order' => 'ASC',
'showposts' => -1,
'meta_query' => array(
array(
'key' => 'principal_o_suplente',
'value' => 'principal',
'compare' => '=='
),
array(
'key' => 'empresarios_o_',
'value' => 'gobiernos',
'compare' => '=='
)
)
);

wordpress query posts by meta date value

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

Wordpress - meta_query - Possible to use multiple or nested meta_query arguments?

I am using wordpress' meta_query to try to build a basic events system.
Each event has a number of meta keys/values. For example:
Start Date
End Date
Ongoing (Yes/No)
I want to separate events that are Ongoing (Ongoing = Yes) and Not Ongoing (Ongoing = No).
Then within my Ongoing events, I want to separate events that are:
Starting within two weeks OR Ending within two weeks
Already started AND ending beyond two weeks
My problem lies within case 1 which is, in essence:
(Start Date <= Two Weeks OR End Date <= Two Weeks) AND Ongoing == YES
Using WP_Query, I have the following:
$ongoingSoon = array(
'category_name' => 'event',
'meta_key' => 'End Date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'Start Date',
'value' => $today,
'compare' => '>='
),
array(
'key' => 'End Date',
'value' => $nexttwoweeks,
'compare' => '<='
),
array(
'key' => 'Ongoing',
'value' => 'Yes'
)
)
);
Which only returns Ongoing events that Start AND ALSO End within the next two weeks. If I change the relation to OR, then it shows events that are not ongoing as well. Is there a way to achieve what I need?
As meta_query is currently written, I'm 99% sure it's impossible to do this.
What you can do to get around that is to create a (probably private) custom taxonomy for Ongoing that acts like a boolean and only ever uses one term ('yes' or '1' or whatever). Your WP_Query would then be changed to look like this:
$ongoingSoon = array(
'category_name' => 'event',
'meta_key' => 'End Date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'Start Date',
'value' => $today,
'compare' => '>='
),
array(
'key' => 'End Date',
'value' => $nexttwoweeks,
'compare' => '<='
)
),
'tax_query' => array(
array(
'taxonomy' => 'ongoing',
'field' => 'slug',
'terms' => 'yes'
)
)
);

Resources