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