0

I need to query two custom post types,

like this:

'post_type' => array ('single_events', 'group_of_events'),

and i need to select certain posts in a certain order, to do this i wrote a meta query to select posts between two dates stored in a custom field.

my problem is now: for my first custom post type this works well, these posts only have the field for start-date.

but for my second custom post type i need to use another key for the meta_query, because these posts are events with an start-date and an end-date,

so my question is how is it possible to modify the meta query to select the posts

for single_events

by meta_key =>'start_date'

and for group_of_events

by meta_key =>'end_date'

at the moment it looks like this:

'meta_query' => array(
            'relation' => 'AND',
            'time' => array(
                'key' => 'start_date', /// or 'end_date' for cpt 'group_of_events??
                'compare' => 'BETWEEN',
                'type' => 'DATE',
                'value' => array($filter_start_date, $filter_end_date),
                ),
            'type' => array(
                'key' => 'type',
                'compare' => 'IN',
                'value' => $filter_type,
                ), 
            ),
'orderby' => 'time', 

i probably could give the single_events also an enddate just by cloning the start_date but maybe there is a more elegant way to do this?

thanks alot!

1 Answer 1

0

Have you tried this?

 'meta_query' => array(
        'relation' => 'AND',
        array(
        'relation' => 'OR',
        array(
            'key' => 'start_date', /// or 'end_date' for cpt 'group_of_events??
            'compare' => 'BETWEEN',
            'type' => 'DATE',
            'value' => array($filter_start_date, $filter_end_date),
            ),
        array(
            'key' => 'end_date', /// or 'end_date' for cpt 'group_of_events??
            'compare' => 'BETWEEN',
            'type' => 'DATE',
            'value' => array($filter_start_date, $filter_end_date),
            ),
        ),
        array(
            'key' => 'type',
            'compare' => 'IN',
            'value' => $filter_type,
            ), 
        ),

then you can change the start_date of the events to another key.

1
  • thank you! but I cant change the key fields, it is not possible to include the post type in the meta query, right? like ` 'meta_query' => array('relation' => 'OR', array( 'post_type'=>'FIELDKEY' 'key' => 'start_date', ` …
    – buckdanny
    Commented Jan 30, 2016 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.