Support

Account

Home Forums ACF PRO Sort by date (sooner first)

Unread

Sort by date (sooner first)

  • I have lesson custom post types which have 4 date sub fields (among others): period_a_start, period_b_start, period_c_start, period_d_start.

    I want to create a query to get the 8 first lessons that start sooner (ordered) (no matter which period starts sooner).

    So if A lesson (period_c_start = 13.12.2018), B lesson (period_a_start = 20.12.2015), C lesson (period_d_start = 14.12.2018) …..

    I want to get the lessons back ordered like this: A lesson, C lesson, B lesson …

    So this is what I have tried so far:

        $args  = ['post_type'=> 'cp_course', 'numberposts' => 8,'orderby' => 'menuorder', 'order' => 'ASC',
        		 'meta_query' =>
        		 	array(
        				'relation' => 'OR',
        				array(
        					'key' => 'period_a_start',
        					'compare' => '>=',
        					'type' => 'numeric',
        					'value' => date("m/d/Y"),
        				),
        				array(
        					'key' => 'period_b_start',
        					'compare' => '>=',
        					'type' => 'numeric',
        					'value' => date("m/d/Y"),
        				),
        				array(
        					'key' => 'period_c_start',
        					'compare' => '>=',
        					'type' => 'numeric',
        					'value' => date("m/d/Y"),
        				),
        				array(
        					'key' => 'period_d_start',
        					'compare' => '>=',
        					'type' => 'numeric',
        					'value' => date("m/d/Y"),
        				)
        			)];
        $course = get_posts($args);

    But this approach doesn’t guarantee that it will return the 8 lessons that start sooner ordered by date

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.