Make WordPress Core

Ticket #18694: test-wordpress-dates.2.php

File test-wordpress-dates.2.php, 2.6 KB (added by Viper007Bond, 14 years ago)

Generates some sample queries and provides some examples

Line 
1<?php
2
3require( '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 // Posted over a year ago but modified in the past month
65 array(
66 array(
67 'column' => 'post_date_gmt',
68 'before' => '1 year ago',
69 ),
70 array(
71 'column' => 'post_modified_gmt',
72 'after' => '1 month ago',
73 )
74 ),
75
76 array(
77 array(
78 'before' => array(
79 'year' => 2010,
80 'month' => 3,
81 'day' => 1,
82 ),
83 ),
84 ),
85
86 array(
87 'relation' => 'OR',
88 array(
89 'hour' => 14,
90 'dayofweek' => 1,
91 ),
92 array(
93 'hour' => 18, // Will be cast to an array
94 'dayofweek' => array( 2, 3 ),
95 'compare' => 'IN',
96 ),
97 ),
98
99 array(
100 array(
101 'minute' => 0,
102 )
103 ),
104
105 // Non-integer test
106 array(
107 array(
108 'hour' => 'foo',
109 ),
110 ),
111
112 // Wrong format test (not an array of arrays)
113 array(
114 'w' => date( 'W' ),
115 ),
116);
117
118
119// var_dump() the full SQL if WP_Query is called
120add_filter( 'posts_request', 'debug_post_date_query' );
121function debug_post_date_query( $var ) {
122 var_dump( $var );
123 return $var;
124}
125
126foreach ( $tests as $date_query ) {
127 // Dump the "date_query" array
128 var_dump( $date_query );
129 echo '<hr>';
130
131 // Dump just the date SQL
132 $wp_date_query = new WP_Date_Query( $date_query );
133 var_dump( $wp_date_query->get_sql() );
134 echo '<hr>';
135
136 /*
137 $query = new WP_Query( array(
138 'date_query' => $date_query,
139 ) );
140 echo '<hr>';
141 /**/
142}
143
144
145
146// var_dump() the full SQL if WP_Comment_Query is called
147add_filter( 'comments_clauses', 'debug_comment_date_query' );
148function debug_comment_date_query( $pieces ) {
149 global $wpdb;
150
151 extract( $pieces );
152 var_dump( "SELECT $fields FROM $wpdb->comments $join WHERE $where ORDER BY $orderby $order $limits" );
153
154 return $pieces;
155}
156
157$comments = get_comments( array(
158 'date_query' => array(
159 array(
160 'before' => '1 week ago',
161 ),
162 ),
163) );
164
165?>