1

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>
4
  • Is there a good reason you can't call the date fields for the separate types the same thing, e.g. just 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.
    – Rup
    Jun 25, 2014 at 9:28
  • hmmm well actually the fields reside in different custom post types. It felt odd and somehow wrong to use identical field id names over post types. Secondly they slightly differ. Two custom post types have a start and an end date and two have just a single date so it wouldn't really work to use all the same post type name. I just simplified the demo by using 4 different post type names.
    – rkoller
    Jun 25, 2014 at 9:38
  • What's the date format of the dates (such as eventtype2_date)?
    – engelen
    Jun 25, 2014 at 9:59
  • The date format is yymmdd
    – rkoller
    Jun 27, 2014 at 21:55

1 Answer 1

2

There is no output for this sample code because the IF statement you used will never return true, since the string 'post_type' will never be equal to any other string but 'post_type'. Use get_post_type():

if ( get_post_type() == 'eventtype1' ) {

To order by a date custom field, you will need to use the same field id for all the post types if you wish to query all of them together.

$args = array(
    'post_type' => array( 'eventtype1', 'eventtype2', 'eventtype3', 'eventtype4' ),
    'posts_per_page' => -1,
    'meta_key'      => 'start_date',
    'orderby'       => 'meta_value_num',
    'order'         => 'DESC'
);

http://www.advancedcustomfields.com/resources/how-to/orde-posts-by-custom-fields/

1
  • Thanks for the explanation, understand the issue with the if statement now. The get_post_type thing works. And about the order of the custom field, it is in fact possible to get it going with multiple custom fields (even with acf repeater fields) via meta_query. Got it working one minute ago. I post an update to my initial question later showing the answer. Have to test and clean a little further.
    – rkoller
    Jun 28, 2014 at 9:18

Your Answer

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

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