I’m working on a new project which is mostly focused on displaying all kind of courses (CPT) on the site.
These courses can have multiple starting days. These days can be in past or in the future depending on the current date. These starting days should only be visible in the front-end if the starting date is in the future and not today or in the past.
For example I have the following starting days:
Februari 15th, April 7th and June 22nd. For today (April 1st) only the dates of April 7th and June 22nd should be shown on the front-end.
I’ve already worked on a project that shows a CPT with a single date if this date is not the current date or in the past. But is this also possible if multiple dates needs to be checked?
Also, is it possible to query these CPT’s by starting day and show the next date that the course will start in ascending order? Even if they have multiple starting days?
Any tips on how to get this done are much appreciated.
Does each Course have a repeater field with multiple dates in it or have you created multiple courses with the same name that have a different date assigned to them?
You could do something like this to get all courses after today:
$today = date( 'Y-m-d' ); $args = array( 'post_type' => 'course', 'meta_query' => array( array( 'key' => 'course_date', 'value' => $today, 'compare' => '>=', 'type' => 'DATE' ) ) ): $query = new WP_Query( $args );
Note: the ‘key’ would be your ACF field name that contains the date. You may need to adjust the format of the date.
I’m not working with the repeater field, haven’t even the Pro version. Right now every course has multiple date fields (date picker) assigned. Duplicates of the same course with only a different date is out of the question.
But if the repeater field option is the way to go I’m happy to purchase the pro version.
Okay, so if you have multiple datepicker fields on a Course, try something like this on your single-course.php to only show dates that are in the future:
$today = date( 'Y-m-d' ); if(get_field('date_1') > $today): echo '<p>' . get_field('date_1') . '</p>'; endif; if(get_field('date_2') > $today): echo '<p>' . get_field('date_2') . '</p>'; endif; etc...
Again, just make sure that the date format for $today matches what you specified in your datepicker field in the ACF settings. If that doesn’t work, you may need to do a php strtotime() with the dates.
Querying Courses by multiple ACF field dates would be a little trickery.
$today = date( 'Y-m-d' ); $args = array( 'post_type' => 'course', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'date_1', 'value' => $today, 'compare' => '>', ), array( 'key' => 'date_2', 'value' => $today, 'compare' => '>', ), ), ); $query = new WP_Query( $args );
I just don’t think your can order a WP Query by multiple fields.
The topic ‘Sort CPT with multiple dates’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!