$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Custom post types - meta_query: search lesson which starts sooner|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Custom post types - meta_query: search lesson which starts sooner

matteradmin9PV0评论

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

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

Share Improve this question edited Dec 12, 2018 at 10:19 dvn22 asked Dec 12, 2018 at 8:17 dvn22dvn22 13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I would advice you to store the time as a string instead of a d-m-Y format. Use strtotime() to format it as a long integer. This will help you comparing the strings. I'm not 100% sure if PHP is able to compare string dates. It is however able to compare timetamps as this user mentioned as well: How to compare dates and strings in PHP

Post a comment

comment list (0)

  1. No comments so far