Page ordering based on custom field of sub pages

I have a custom page type of events that allows sub pages and I want to sort the events pages in order of date of the sub pages.

so my structure is something like this:

Camping trips:

  • texas camping trip (custom field of date: 2/12/2014)
  • utah camping trip (custom field of date: 3/10/2014)

Fundraisers:

  • bake sale (custom field of date: 1/30/2014)
  • silent auction (custom field of date: 5/1/2014)

etc

When querying the pages I would like them to return in order of Fundraisers then Camping trips .

Answers


I'm assuming you're using a category to separate camping trips & fundraisers, this method should be close for other methods of separating the trips/fundraisers however. I am also assuming that you want to do this outside of the loop, or as a secondary loop. I haven't tested this, but something like this should do the trick.

Basically here is what were going to do:

1) Query for both the Fundraisers and the Camping Trips, ordered by date.

2) Merge the two queries together, so that the fundraisers are first, and the camping trips second.

3) Loop through each returned post, and mark up the information however you like.

Code:

    <?php //Enter your information for each variable:
     $post_type = 'enter_your_custom_post_type_slug';
     $fundraiserCatID = 'enter_your_fundaiser_category_id';
     $campingCatID = 'enter_your_camping_category_id';
     $acfDateFieldName = 'enter_your_date_acf_field_slug';

     //Setup each Query args
    $fundraiserAgrs = array('post_type' => $post_type, 'cat' => $fundraiserCatID, 'orderby' => 'meta_value', 'meta_key' => $acfDateFieldName, 'order' => 'ASC');
    $campingAgrs = array('post_type' => $post_type, 'cat' => $fundraiserCatID, 'orderby' => 'meta_value', 'meta_key' => $acfDateFieldName, 'order' => 'ASC');

    $fundraisers = get_posts($fundraiserArgs);
    $campingTrips = get_posts($campingArgs);

    $events = array_push($fundraisers, $campingTrips); //merge the two queries, with fundraisers first, then camping trips

    if($events) : foreach($events as $event): //If we have $events, loop through each event


      //Do what you will with the $event content ?>

         <h1><?php echo $event->post_title; ?></h1>
         <?php echo $event->post_content; ?>
         <h6>Date: <?php the_field($acfDateFieldName, $event->ID); ?></h6> 

    <?php endforeach; endif; //End the loop and the conditional ?>

Need Your Help

Rails 4 - destroying a HABTM record results in UndefinedTable

ruby-on-rails ruby has-and-belongs-to-many

I have a has_and_belongs_to_many relationship between Events and Users.

Google Maps API enableScrollWheelZoom() - why does it not zoom out correctly?

javascript html google-maps zooming

So I've got an issue with a Google map using custom tiles, but I've managed to simplify it to the example below.