I have four custom post types created with the custom post types ui plugin; each stands for an event type with a different set of custom fields created with the advanced custom fields plugin.
My goal is to show all events out of the pool of the four chronological. To query the four with WP_Query is no problem but so far to distinguish the output and to sort it gives me worries.
The sample code queries the custom post types and outputs the date of the event as well as an echo to figure out the type of the event for debugging purpose.
<article>
<?php $args = array(
'post_type' => array( 'eventtype1', 'eventtype2', 'eventtype3', 'eventtype4' ),
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
if ( 'post_type' == 'eventtype1' ) {
?><p><?php the_field( 'eventtype1_date' ); ?></p><?php
echo "event type 1";
}
elseif ( 'post_type' == 'eventtype2') {
?><p><?php the_field( 'eventtype2_date' ); ?></p><?php
echo "event type 2";
}
elseif ( 'post_type' == 'eventtype3' ) {
?><p><?php the_field( 'eventtype3_date' ); ?></p><?php
echo "event type 3";
}
elseif ( 'post_type' == 'eventtype4' ) {
?><p><?php the_field( 'eventtype4_date' ); ?></p><?php
echo "event type 4";
} ?>
</article>
One problem is i am unsure if it is possible to distinguish outside the scope of the WP_Query array by post_type. Maybe the reason why there is no output yet with the sample code.
Second i am uncertain how to sort the results. If i use the 'orderby' => 'date'
in the WP_query array i sort by the publish date and not the actual event date. About the sort i am unsure if it is possible at all in this setup?
Any hints are more than welcome. Best regards Ralf
Update: Ok i've solved the issue. With the following code it is possible to sort acf date picker fields and acf date picker fields in repeater fields (the date picker queried has to be placed at first in the repeater field) in more than one custom post type by date:
<article>
<?php $args = array(
'post_type' => array( 'event_type1', 'event_type2', 'event_type3', 'event_type4' ),
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array('key' => 'event1_date_0_event1_date_day'),
array('key' => 'event2_date'),
array('key' => 'event3_date_0_event3_date_day'),
array('key' => 'event4_date' )
),
'meta_key' => 'meta_value',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
if ( get_post_type() == 'event_typ1' ) {
$e1 = get_field( 'event1_date' );
echo "Event Type 1"; ?>
<p><?php echo $e1[0]['event1_date_day']; ?></p><?php
}
elseif ( get_post_type() == 'event_typ2') {
echo "Event Type 2:"; ?>
<p><?php the_field( 'event2_date' ); ?></p><?php
}
elseif ( get_post_type() == 'event_type3' ) {
$e3 = get_field( 'event3_date' );
echo "Event Type 3:"; ?>
<p><?php echo $e3[0]['event3_date_day']; ?></p><?php
}
elseif ( get_post_type() == 'event_type4' ) {
echo "Event Type 4:";
?><p><?php the_field( 'event4_date' ); ?></p><?php
}
endwhile;
wp_reset_postdata();
else: ?>
<p>Error</p>
<?php endif; ?>
</article>
event_date
and then maybe'orderby' => 'event_date'
might work? (I'm guessing, sorry.) Otherwise you'd have to do a SQL-style coalesce to combine the four fields to sort on, and I don't think that's possible in the wp query language.eventtype2_date
)?