Upcoming Events Ordered by ACF Date Picker Field in Oxygen

This members-only tutorial provides the steps to display x number of entries of event custom post type where event_date custom field (of Date Picker or Date Time Picker type) is greater than today i.e., future events ordered by the latest (event date) entries in descending order.

We shall use pre_get_posts in a Code Block above Oxygen‘s Repeater component which is set to use a manual query.

Note: This tutorial is primarily meant for secondary queries on static Pages and not on the events CPT archive. You can still use the relevant pre_get_posts code but that needs to go in a Code Snippet or a custom functionality plugin. Plus more importantly, the Repeater’s query should be left at Default in that case i.e., for the main query.

Step 1

In the Oxygen editor add a Section and inside that a Heading with the text “Upcoming Events”.

Add a Repeater and set the query to manual having:

post_type=event&posts_per_page=10&no_found_rows=true

Click the Div inside the Repeater and add the linked event title, event date and any other relevant info as needed.

This is a premium members-only content.

To view the rest of the content, please sign up for membership ($47/month or $599 one-time).

Already a member? Log in below or here.


Posted

in

, ,

by

Comments

5 responses to “Upcoming Events Ordered by ACF Date Picker Field in Oxygen”

  1. Kyle Homstead Avatar
    Kyle Homstead

    I’ve been trying to make this scenario work, but it keeps causing the page to crash and generate a 500 error. No console errors and nothing in WP_DEBUG. It seem pretty straightforward. Any suggestions on how to debug?

    1. Sridhar Katakam Avatar

      Check that the name of your custom field (event_date in this example) is indeed correctly entered.

      If still no dice, we can take a look. Send WP login via the support form at https://wpdevdesign.com/account/?action=support.

  2. randygreene Avatar
    randygreene

    You have a note about this being not intended for an archive page. How would I adapt this for use on an archive page?

    1. Sridhar Katakam Avatar

      Add this sample code using the Code Snippets plugin:

      add_action( 'pre_get_posts', 'wpdd_sort_by_event_date' );
      /**
      * Sort events by event date custom field with the latest at top.
      *
      * @param $query WP_Query
      */
      function wpdd_sort_by_event_date( $query ) {

      if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive( 'event' )  ) {
          $query->set(
              'meta_query',
              array(
                  array(
                      'key'     => 'event_date',
                      'compare' => '>',
                      'value'   => date( 'Ymd' ),
                      'type'    => 'DATETIME',
                  ),
              )
          );
      
          $query->set( 'orderby', 'meta_value' );
          $query->set( 'meta_key', 'event_date' );
          $query->set( 'order', 'DESC' );
      }
      

      }

      1. randygreene Avatar
        randygreene

        You. Rock.

Leave a Reply to Sridhar Katakam Cancel reply

Your email address will not be published. Required fields are marked *