Wordpress Query by custom date filed - wordpress

Can someone show me how to do this, I have a query I'm using to display custom posts and using WP Facet for the query, by a custom field date, I got this far to order the posts by date in Ascending order, but the last thing I want to do is only show the posts that equal today's date or future dates;
I've tried a few things but no joy as of yet, thanks
<?php
return array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 15,
'orderby' => 'event_date',
'order' => 'ASC',
'sort_custom' => true,
'meta_query' => array(
array(
'key' => 'event_date',
),
)

if this helps anyone - i worked it out:
<?php
return array(
'post_type' => 'event',
'post_status' => 'publish',
'meta_key' => 'event_date',
'posts_per_page' => 15,
'orderby' => 'event_date',
'order' => 'ASC',
'sort_custom' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_date',
'compare' => '>=',
'value' => date("Y-m-d"),
'type' => 'DATE'
),
)
);

Related

order posts by meta query, if featured show first then verified then normal

i want to order posts by meta query, if featured & verified show first then verified and then normal without those meta keys
i tried this code its showing what i need but the order is wrong.
<?php
$sports = get_field('sport', $post_id);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'meta_key' => 'sport',
'meta_value' => $sports,
'post__not_in' => array ($post->ID),
'meta_query' => array(
'relation' => 'OR',
'featuredmet' => array(
'key' => 'featured',
'compare' => 'EXISTS',
),
'verified_athletemet' => array(
'key' => 'verified_athlete',
'compare' => 'EXISTS',
),
'commitedmet' => array(
'key' => 'commited',
'compare' => 'NOT EXISTS',
),
),
'orderby' => array(
'featuredmet' => 'DESC',
'verified_athletemet' => 'DESC'
)
);
//the query
$relatedPosts = new WP_Query( $args );
if anyone can help me with this super fast that would be great!

I want to query all posts with ACF date field today and beyond

I'm banging my head against the wall with this, is there anything wrong with my code?
I want to query all posts that have 'launch_date' today and beyond.
<?php
// find date time now
$date_now = date('M, d');
// query events
$args = array(
'posts_per_page' => 20,
'post_type' => 'project',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'launch_date',
'compare' => '<=',
'value' => $date_now,
'type' => 'DATETIME'
)
),
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'launch_date',
'meta_type' => 'DATE'
);
echo build_query( $args );
?>

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.

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