WordPress Query order by two keys in dependency - php

I sort a query by a customfield start_date (form type Ymd). The sorting should always be descending from new to old. If there are multiple entries for the date on one day, the entries should be sorted by the order of publication.
I tried it with the (post) date, ID, menu_order but it never works in all cases.
The start_date may be a different day than the post date.
Is there any query way?
Or do I have to add hours and minutes to the news_start_date field?
Or maybe switch to the post date over all, but this means in this backend flow a big lost of elegance.
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'news_start_date',
'value' => $today,
'compare' => '<=',
'type' => 'DATE'
),
array(
'relation' => 'OR',
array (
'relation' => 'AND',
array(
'key' => 'news_start_date',
'value' => $expire_date,
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => 'news_end_date',
'compare' => 'NOT EXISTS'
),
),
array (
'relation' => 'AND',
array(
'key' => 'news_end_date',
'compare' => 'EXISTS'
),
array(
'key' => 'news_end_date',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
),
),
),
);
$args = array(
'post_type' => 'news',
'meta_query' => $meta_query,
'posts_per_page' => $news_count,
'meta_key' => 'news_start_date',
'orderby' => 'meta_value',
'orderby' => array(
'meta_value' => 'DESC',
'ID' => 'DESC',
//'menu_order' => 'DESC',
//'date' => 'DESC',
//'publish_date' => 'DESC',
),
'order' => 'DESC',
'post__not_in' => $sticky_posts,
'ignore_sticky_posts' => true,
'cache_results' => true,
'update_post_meta_cache' => true,
'no_found_rows' => true,
'suppress_filters' => false,
);
I tried the others fields but there was never a 100% correct order

Related

wc_order_query with meta_query not working when using relation

I am trying to use WC_Order_Query, to get all orders where a custom meta_key doesn't exist, is empty or equal to 0
I've tried like a lot of the stuff documented on this site, but nothing seems to work. It just returns all content, which is the opposite of what i'm trying to do.
This is what most people have recommended so far, but it doesn't seem to work as intended or maybe I am not seeing the issue here
$args = array(
'limit' => 9999,
'return' => 'ids',
'orderby' => 'date',
'order' => 'DESC',
'status' => 'processing',
'date_created' => '>='.$startdate,
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
This ended up being the solution:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'post_status' => 'any',
'orderby' => 'the_date',
'order' => 'DESC',
'date_query' => array(
array(
'after' => $startdate . $starttime,
'inclusive' => true
)
),
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'order_printed',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => ''
),
array(
'key' => 'order_printed',
'compare' => '=',
'value' => 0
)
)
)
);
$query = new WP_Query( $args );
$query_posts = $query->get_posts();
Thanks a lot to Boris for the assistance.

how to display popular posts by multiple meta keys in wordpress?

I want to display popular post in custom page , I have added post views and post like option in wordpress and I want to show popular posts based views and likes,
I tried this code but it's ranking only for views counter ;
$args = array(
'posts_per_page'=>5,
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
'_post_views' => array(
'key' => '_post_views',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
'_post_like_count' => array(
'key' => '_post_like_count',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
),
);
thanks for answers
Try new orderby feature:
$args = array(
'posts_per_page' => 5,
'meta_query' => array(
'relation' => 'AND',
'_post_views' => array(
'key' => '_post_views',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
'_post_like_count' => array(
'key' => '_post_like_count',
'type' => 'NUMERIC',
'compare' => 'LIKE'
),
),
'orderby' => array(
'_post_views' => 'DESC',
'_post_like_count' => 'DESC'
)
);

Sort Posts By Multiple Meta values then show other posts Wordpress

I am working a nested wordpress query, I want sort posts by featured_article first then show another which not featured.
Then it should show up featured_articles plus another custom field order_value which will be bus, road, air etc
Here is my myquery
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'meta_value_num',
'meta_key' => 'featured_article',
'cat' => get_query_var('cat'),
'meta_query' => array(
'relation' => 'AND',
array('key' => 'order_value',
'value' => 'bus',
'compare' => 'LIKE'
),
array('key' => 'featured_article',
'compare' => 'EXISTS'
),
)
Its working but its showing posts contain key value bus and fetured_article not other posts :(
Then i changed relation
'relation' => 'OR',
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'meta_value_num',
'meta_key' => 'featured_article',
'cat' => get_query_var('cat'),
'meta_query' => array(
'relation' => 'OR',
array('key' => 'order_value',
'value' => 'bus',
'compare' => 'LIKE'
),
array('key' => 'featured_article',
'compare' => 'EXISTS'
),
array('key' => 'order_value',
'value' => 'bus',
'compare' => 'NOT LIKE'
),
)
Its working but not order by bus just order by featured_article

Date issue in custom post type loop (wordpress)

I am using a custom post type with custom date fields to display tour dates on a page. Some of the posts have both a "from" and "to" date (because they last for more than one day) and some only have "from" (because they're only one day), and I want all future or current tour dates to be displayed.
It's all working perfectly, except for some reason the posts that only have a "from" date stop showing if that date is in 2016 or later. It doesn't make any sense to me!
Here are the args:
$today = date('Ymd');
$tour_args = array(
'post_type' => 'tour_date',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tour-date-from',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'tour-date-from',
'value' => $today,
'compare' => '<=',
),
array(
'key' => 'tour-date-to',
'value' => $today,
'compare' => '>=',
),
),
);
This is now solved. The problem was that I needed to specify the meta type. I also needed to take out the 'meta_key' line and therefore amend the 'orderby' line to look for the right value.
$today = date('Ymd');
$tour_args = array(
'post_type' => 'tour_date',
'posts_per_page' => -1,
'orderby' => 'tour-date-from',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'tour-date-to',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC'
),
array(
'key' => 'tour-date-from',
'value' => $today,
'compare' => '<=',
'type' => 'NUMERIC'
),
),
);

Date range find in wordpress

In wordpress I have only publish_date(not meta_key) and meta_key "end_date"
Example: event from 2015-06-22(publish_date) to 2015-08-24(meta_ket = "end_date")
now I want search all events
(from 2015-07-05 to 2015-07-24)
or
(from 2015-08-05 to 2015-09-25)
and must show event in example
This code not work:
$opt = array(
'post_type' => 'event_info',
'posts_per_page' => '40',
'paged' => $page_num,
'orderby' => 'post_date ID',
'order' => 'DESC',
'relation' => 'OR',
'meta_query' => array(
'key' => 'end_date',
'value' => array('$date_start','$date_end'),
'compare' => 'BETWEEN',
'type' => 'numeric'),
'date_query' => array(
array(
......
'compare' => 'BETWEEN',
),
),
)
);
relation between meta key and data_query not work,
current idea is create 2 query but I dont want do this
try this : here I added timestamp in startdate and enadate meta key
$chkdt = current_time('timestamp');
$args = array('post_type'=>'event_info', 'post_status' => 'publish','posts_per_page'=>-1,'orderby' => 'ID','order' => 'DESC', 'meta_query' =>
array(
'relation' => 'AND',
/******filter start date and End date****************/
array(
'key' => 'end_date',
'value' => $chkdt,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'start_date',
'value' => $chkdt,
'compare' => 'NUMERIC',
),
/******END filter start date and End date****************/
),
);

Categories