I want to order or group all the posts with meta_value with same meta_key.
All posts with meta_value 'CCC' and with 'AAA' and with 'BBB',...
I try with this :
'meta_query' => array(
array(
'key'=>'a_post_meta_key',
'value'=> array('CCC','AAA','BBB','DDD')
)
),
'orderby' => 'meta_value',
'order' => 'DESC',
And this :
'meta_query' => array(
'relation' => 'OR',
'ccc_clause' => array(
'key' => 'a_post_meta_key',
'value' => 'CCC',
'compare' => '='
),
'aaa_clause' => array(
'key' => 'a_post_meta_key',
'value' => 'AAA',
'compare' => '='
),
'bbb_clause' => array(
'key' => 'a_post_meta_key',
'value' => 'BBB',
'compare' => '='
),
),
'orderby' => array(
'ccc_clause' => 'DESC',
'aaa_clause' => 'DESC',
'bbb_clause' => 'DESC',
),
But no good results. Do you have any ideas ?
Thank you
Related
The order by isn't working correctly.
Here is the query:
$args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'post_status' => 'publish',
/*'meta_key' => 'order',
'orderby' => 'meta_value',
'order' => 'ASC',*/
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'active',
'value' => 'yes',
'compare' => '=',
),
array(
'key' => 'category',
'value' => 'data',
'compare' => '=',
),
),
'orderby' => array(
'order' => 'ASC'
)
);
I have ACF field Order (order) which select with choices from 1 to 15
When the result is displayed they aren't from 1 to 15 but randomly displayed.
I am trying to query custom post type but I keep getting no result. Is my meta_query the culprit? What is wrong with this code?
I am trying to spot the issues but i cannot find anything.
$catname = 'travel';
$priority ='high';
$status = 'incomplete';
$args = array(
'post_type' => 'my_gallery_post',
'orderby' => 'id',
//'fields' => 'ids',
'sort_order' => 'asc',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'category_it_belongs',
'value' => $catname,
'compare' => '='
),
array(
'key' => 'levelof_priority',
'value' => $priority,
'compare' => '='
),
array(
'key' => 'progress',
'value' => $status,
'compare' => '='
),
)
);
$mypost = get_posts($args);
print_r($mypost);
Output
Array()
I solved the problem by using array for 'levelof_priority'.
The field type for 'levelof_priority' is radio button whereas the other 2 are text field.
Below are my final code that's working
$catname = 'travel';
$priority ='high';
$status = 'incomplete';
$args = array(
'post_type' => 'my_gallery_post',
'orderby' => 'id',
//'fields' => 'ids',
'sort_order' => 'asc',
'post_status' => 'publish',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'category_it_belongs',
'value' => $catname,
'compare' => '='
),
array(
'key' => 'levelof_priority',
'value' => array($priority),
'compare' => '='
),
array(
'key' => 'progress',
'value' => $status,
'compare' => '='
),
)
);
$mypost = get_posts($args);
print_r($mypost);
I set following meta_query.
I like to add one more condition.
My problem is that the article which matches both meta_query conditions are double counted by count(get_posts on the end of line.
Is it possible to add condition like this? If both 1bb and 1aa matches, count it as if only 1bb matches.
<?php
$args = array(
'category_name' => $cat,
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'or',
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
echo count( get_posts( $args ) );
?>
Thank you.
The following is additional code that I tried to prevent from double counting.
Its too long time to load, and can not use it.
'meta_query' => array(
'relation' => 'or',
array(
'relation' => 'AND',
array(
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1bb',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'EXISTS',
),
array(
'key' => '1bb',
'compare' => 'EXISTS',
),
),
),
),
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/
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