Oder By Date In post Meta - wordpress

I am using wp_query to get post but i want it's order by date saved in my post meta
I try it using meta_key and order_by but it's not working
$args = array(
'post_type' => 'obituary',
'posts_per_page' => 10,
'paged' => $paged,
'meta_key' => 'user_date_of_death',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
and date saved format in post meta is 06/28/2016

Use below argument in the post query.
$today_date = date('m/d/Y', strtotime('+2 hours'));
$args = array( 'post_type' => 'obituary',
'order' => 'ASC',
'meta_key' => 'user_date_of_death',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'user_date_of_death',
'value' => $today_date ,
'compare' => '>=',
'type' => 'DATE'
)
)
);

Related

ACF - don't show passed events and filter with start date

I have a list of events and I'd like to do 2 things :
order posts according to starting date
don't include passed events in the query
I did some research and builded a query but it doesn't work.
'''
$current_date = date_i18n('d.m.y');
$the_query = new WP_Query( array(
'post_type' => 'spectacles',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'type' => 'DATE'
),
array(
'key' => 'header_spec_period_end',
'value' => '$current_date',
'compare' => '>',
'type' => 'DATE'
),
),
'order' => 'ASC' ) );
?>
'''
All the events are showing, I can't get the passed events filtered.
$date_now = date('Ymd');
$args = array(
'post_type' => 'spectacles',
'post_status' => 'publish',
'meta_key' => 'header_spec_period_start',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'compare' => '>',
'value' => $date_now,
),
),
);
$the_query = new WP_Query( $args );
You can try this ...

Query for getting sorted data using post meta field

I need sorted events using postmeta field which have value of date in format of 18 May 2017.This is my code but i dont get solution for sorting.
$args = array
(
'posts_per_page' => -1,
'post_type' => 'movies',
'meta_key' => 'event_release',
'orderby' => 'meta_value',
'order' => 'ASC'
);
Try specifying the meta type:
$args = array
(
'posts_per_page' => -1,
'post_type' => 'movies',
'meta_key' => 'event_release',
'meta_type' => 'DATE',
'orderby' => 'meta_value_date',
'order' => 'ASC'
);

order by two value in WP_Query - wordpress

I want to order posts first by date and second by views
New update :
$trending_songs = new WP_Query(array(
'post_type' => 'songs',
'showposts' => $songs_count,
'cat' => 'songs_cat-689',
'orderby' => 'meta_value_num',
'meta_query' => array(
array(
'orderby' => 'date',
'order' => 'DESC',
),array(
'key' => 'views',
'order' => 'DESC',
)
),
));
the date is not working in the post (they just order by views)
You can use something like below code (this is just for sample you need to change according to your requirement)
<?php
$args = array(
'post_type' => 'words',
'meta_query' => array(
array(
'key' => 'word_count',
'orderby' => 'date',
'order' => DESC
,
),
array(
'key' => 'title',
'orderby' => 'date',
'order' => DESC
),
array(
'key' => 'word_type',
'orderby' => 'date',
'order' => DESC
),
),
);
$query = new WP_Query( $args );
?>

How to use custom fields in wp_query for ordering and filtering the result at the same time?

I want to query the posts that have been set as "featured" and order them by their "priority" field at the same time.
featured field is a true/false type and priority field is a number, and they are created by ACF plugin.
here is my code, but it's not working...
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num date',
'meta_key' => 'priority',
'meta_query' => array(
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
try this :
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num',
'order'=>'DESC',
'meta_key' => 'priority',
'meta_query' => array(
relation=>'AND',
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
array(
'key' => 'priority',
'value' => array(1,6), //YOUR VALUES
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
),
);
$query = new WP_Query( $args );

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