wordpress custom query and orderby - wordpress

I am trying to query posts using custom meta fields (date) and order them by the custom meta field (start_date).
I need to look for posts that start on or after todays date or end after today's date.
It all seems to work apart from the order by start date and OR statement, any help appreciated.
<?php
$today = date('y-m-d');
$args = array(
'numberposts' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>='
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '>'
)
)
);
query_posts( $args );
?>

I am assuming you are MySQL database for wordpress and date format should be YYYYMMDD.
Update date format: use $today = date('Y-m-d'); in stead of $today = date('y-m-d');
Also type=>date in meta_query like below:
$args = array(
'numberposts' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'start_date',
'value' => $today,
'type' => 'date',
'compare' => '>='
),
array(
'key' => 'end_date',
'value' => $today,
'type' => 'date',
'compare' => '>'
)
)
);
Hope will help!

Related

Wordpress Advanced custom fields meta query filter in post object

I have the following JSON format for event post type
JSON Format
Event Channel Fields
Event Details Fields
I am trying to filter all upcoming events (i.e. future date) and having "Michael Kay" as an event_channel.
Below is my custom query and is not working. The issue is with the event_channel as it is a post object of 'channel' custom posts. The second portion of the meta query is not working.
<pre>
$today_r = date_i18n('j M Y g:i A');
$today = strtotime($today_r);
$upcoming_events = new WP_QUERY(array(
'post_type' =>'event',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_date',
'meta_value' => $today,
'meta_compare' => '>=',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_start_date',
'value' => $today,
'compare' => '>=',
),
array(
'relation' => 'AND',
'key' => 'event_channel',
array(
'key' => 'post_title',
'value' => 'Michael Kay',
'compare' => 'LIKE',
),
)
),
"posts_per_page" => 6
));
</pre>
You need to adjust $args array. You should not pass post_title as meta key.
Use this one:
$upcoming_events = new WP_QUERY(array(
'post_type' =>'event',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_date',
'meta_value' => $today,
'meta_compare' => '>=',
's' => 'Michael Kay',
"posts_per_page" => 6
));
Update:
Here is the query which filters it by ACF subfield:
$upcoming_events = new WP_QUERY(array(
'post_type' =>'event',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_date',
'meta_value' => $today,
'meta_compare' => '>=',
'meta_query' => array(
array(
'key' => 'event_channel_0_post_title',
'value' => 'Michael Kay',
'compare' => 'LIKE',
),
)));
To know exact correct name of your event_channel_0_post_title key, go to PHPMYADMIN->wp_postmeta table, and search for "Michael Kay" there. You will see correct name there if event_channel_0_post_title is incorrect.

Filtering a WP_Query meta_query values date isn't working

When I use the acf plugin is normal, but when using acf pro plugin does not working ???
$wp_query_post = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' =>
array(
array(
'key' => 'timestart',
'compare' => '<=',
'meta_type' => 'DATE',
'value' => $datetime,
),
'relation' => 'AND',
array(
'key' => 'timeend',
'compare' => '>=',
'meta_type' => 'DATE',
'value' => $datetime,
)
)
)
);
Convert strtotime and try like below,
$wp_query_post = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' =>
array(
array(
'key' => 'timestart',
'compare' => '<=',
'meta_type' => 'DATE',
'value' => intval(strtotime($datetime)),
),
'relation' => 'AND',
array(
'key' => 'timeend',
'compare' => '>=',
'meta_type' => 'DATE',
'value' => intval(strtotime($datetime)),
)
)
)
);
Probably have done this already, but make sure the format of $datetime matches the format of the custom fields, timestart and timeend.
You may also need to change meta_type, depending on the time format:
'meta_type' => 'DATETIME'

get post base on latest date without comparision

I have custom post type having meta value include last update date.
I have to get post id in which meta value having latest date without compare other date.
below is my wp query to get post id but it is not working.
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'meta_value_num' => 'ASC','ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
If you are using WP 4.2+, you may try the following:
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'post_last_update_date' => 'ASC', 'ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
Some sources:
General WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
4.0+ orderby array: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/
4.2+ orderby meta data: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

wordpress meta_query of date of birth and date of death

How can I retrieve my posts and postmeta with the following two meta_key => 'date of birth' and meta_key => 'date of death' and its meta_value => 'YYYYMMDD', with compare with current date(Ymd) and month like 29 april or 29/4 or 29-4.
Something like the following:
$today = date('Ymd');
$query_args1 = array (
'post_type' => $post_types,
'post_status' => 'publish',
'post__not_in' => explode(',', $params['exclude']),
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'meta_key' => 'date_of_birth',
'meta_compare' => '=',
'type' => 'date',
'meta_value' => $today
),
array(
'meta_key' => 'date_of_death',
'meta_compare' => '=',
'type' => 'date',
'meta_value' => $today
)
),
);
I am not try this but I hope it will help you.
You could try the REGEXP version:
'meta_query' => array(
array(
'key' => 'date_of_birth',
'value' => '^hello#',
'compare' => 'REGEXP',
)
)
Or follow this link https://wordpress.stackexchange.com/questions/159426/meta-query-with-string-starting-like-pattern

WP_Query meta_query date range with 2 custom fields

I have a custom post type 'events' that contains custom fields for start_date and end_date. I'm trying to make a simple list of upcoming events. If I query only events with a start_date >= today it works fine. If I add in a meta_query to say "AND" all events with an end_date <= today - it doesn't return anything.
My custom fields (start_date, end_date) are stored as unix timestamps, which is why I'm using 'NUMERIC' and 'meta_value_num'. $today is a timestamp of the current date. Here are examples of what I'm trying to do... Pulling my hair out - any help would be appreciated!
THIS WORKS:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
)
);
When I add in a second array for the meta_query - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
)
);
When I use meta_compare in the main query, and a single meta_query - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $today,
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'date',
),
)
);
When I use meta_compare in the main query, and multiple meta_queries - DOES NOT WORK:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $today,
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
)
);
This is how I'm using it and its working fine for me.
$the_query = new WP_Query(array(
'post_type' => 'job',
'posts_per_page' => 25,
'meta_query' => array(
array(
'key' => 'published_date',
'value' => array('20140401','20140405'),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
)
));
if ($the_query->have_posts()) {
echo '<ul>';
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
echo 'Sorry! No Posts';
}
wp_reset_postdata();
The most important note on the codex.
The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format.
Here's what I ended up doing:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'meta_value_num' => $yesterday,
'meta_compare' => '<=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => array($first_minute,$last_minute),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $yesterday,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'start_date',
'value' => $yesterday,
'compare' => '>=',
'type' => 'NUMERIC',
),
)
)
This gets the current month's events. I input variables for first minute of month, last minute of month, and "yesterday" which is 11:59:59 of yesterday.
Not sure what are you trying but seems logical to not return anything as you are selecting events that starts today or in the future (start_date >= $today) AND ends today or in the past (end_date <= $today)...
Alexandru-Florentin Popescu is right! Use your code another following:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC',
),
array(
'key' => 'end_date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC',
),
));
You just need change key 'compare' in both arrays

Resources