1 | <?php |
---|
2 | |
---|
3 | require( 'wordpress-dev/wp-load.php' ); |
---|
4 | |
---|
5 | // An array of "date_query" arrays |
---|
6 | $tests = array( |
---|
7 | |
---|
8 | // Between 8:45 AM and 5:12 PM on weekdays |
---|
9 | array( |
---|
10 | array( |
---|
11 | 'hour' => 8, |
---|
12 | 'minute' => 45, |
---|
13 | 'compare' => '>=', |
---|
14 | ), |
---|
15 | array( |
---|
16 | 'hour' => 17, |
---|
17 | 'minute' => 12, |
---|
18 | 'compare' => '<=', |
---|
19 | ), |
---|
20 | array( |
---|
21 | 'dayofweek' => array( 2, 6 ), |
---|
22 | 'compare' => 'BETWEEN', |
---|
23 | ), |
---|
24 | ), |
---|
25 | |
---|
26 | // Posted at least 30 minutes after the hour on a Tuesday |
---|
27 | array( |
---|
28 | array( |
---|
29 | 'minute' => 30, |
---|
30 | 'compare' => '>=', |
---|
31 | ), |
---|
32 | array( |
---|
33 | 'dayofweek' => 3, |
---|
34 | //'compare' => '=', // Default |
---|
35 | ), |
---|
36 | ), |
---|
37 | |
---|
38 | array( |
---|
39 | 'column' => 'post_date_gmt', |
---|
40 | 'relation' => 'OR', |
---|
41 | array( |
---|
42 | 'hour' => array( 2, 12 ), |
---|
43 | 'minute' => array( 5, 16 ), |
---|
44 | 'compare' => 'BETWEEN', |
---|
45 | ), |
---|
46 | array( |
---|
47 | 'minute' => array( 45, 55 ), |
---|
48 | 'second' => 12, |
---|
49 | 'compare' => 'BETWEEN', |
---|
50 | ), |
---|
51 | array( |
---|
52 | 'dayofweek' => array( 2, 4, 7, 'foobar' ), |
---|
53 | 'compare' => 'IN', |
---|
54 | ), |
---|
55 | ), |
---|
56 | |
---|
57 | array( |
---|
58 | array( |
---|
59 | 'after' => '3 weeks ago', |
---|
60 | 'before' => '2 weeks ago', |
---|
61 | ), |
---|
62 | ), |
---|
63 | |
---|
64 | array( |
---|
65 | array( |
---|
66 | 'before' => array( |
---|
67 | 'year' => 2010, |
---|
68 | 'month' => 3, |
---|
69 | 'day' => 1, |
---|
70 | ), |
---|
71 | ), |
---|
72 | ), |
---|
73 | |
---|
74 | array( |
---|
75 | 'relation' => 'OR', |
---|
76 | array( |
---|
77 | 'hour' => 14, |
---|
78 | 'dayofweek' => 1, |
---|
79 | ), |
---|
80 | array( |
---|
81 | 'hour' => 18, // Will be cast to an array |
---|
82 | 'dayofweek' => array( 2, 3 ), |
---|
83 | 'compare' => 'IN', |
---|
84 | ), |
---|
85 | ), |
---|
86 | |
---|
87 | array( |
---|
88 | array( |
---|
89 | 'minute' => 0, |
---|
90 | ) |
---|
91 | ), |
---|
92 | |
---|
93 | // Non-integer test |
---|
94 | array( |
---|
95 | array( |
---|
96 | 'hour' => 'foo', |
---|
97 | ), |
---|
98 | ), |
---|
99 | |
---|
100 | // Wrong format test (not an array of arrays) |
---|
101 | array( |
---|
102 | 'w' => date( 'W' ), |
---|
103 | ), |
---|
104 | ); |
---|
105 | |
---|
106 | |
---|
107 | foreach ( $tests as $date_query ) { |
---|
108 | // Dump the "date_query" array |
---|
109 | var_dump( $date_query ); |
---|
110 | echo '<hr>'; |
---|
111 | |
---|
112 | // Dump the SQL |
---|
113 | $wp_date_query = new WP_Date_Query( $date_query ); |
---|
114 | var_dump( $wp_date_query->get_sql() ); |
---|
115 | echo '<hr>'; |
---|
116 | |
---|
117 | /* |
---|
118 | // How many posts did we get? |
---|
119 | $query = new WP_Query( array( |
---|
120 | 'date_query' => $date_query, |
---|
121 | ) ); |
---|
122 | |
---|
123 | var_dump( count( $query->posts ) ); |
---|
124 | echo '<hr>'; |
---|
125 | /**/ |
---|
126 | } |
---|
127 | |
---|
128 | ?> |
---|