Ticket #6772: test_get_posts.php

File test_get_posts.php, 6.8 kB (added by filosofo, 3 months ago)
Line 
1 <?php
2 /*
3 Template Name: Test get_posts
4 */
5
6 /*************************************************
7  *  Use as a page template to test the get_posts *
8  ************************************************/
9 ?><html><head><title>get_posts test page</title></head><body><?php
10 function get_wpdb_query($query) {
11     global $test_wpdb_query;
12     if ( ! empty( $query ) ) {
13         $test_wpdb_query[] = $query;
14     }
15     return $query;
16 }
17 add_filter('query', 'get_wpdb_query');
18
19 // define('SAVEQUERIES', true);
20
21 // the old function
22 function get_posts_old($args = null) {
23     global $wpdb;
24
25     $defaults = array(
26         'numberposts' => 5, 'offset' => 0,
27         'category' => 0, 'orderby' => 'post_date',
28         'order' => 'DESC', 'include' => '',
29         'exclude' => '', 'meta_key' => '',
30         'meta_value' =>'', 'post_type' => 'post',
31         'post_status' => 'publish', 'post_parent' => 0
32     );
33
34     $r = wp_parse_args( $args, $defaults );
35     extract( $r, EXTR_SKIP );
36
37     $numberposts = (int) $numberposts;
38     $offset = (int) $offset;
39     $category = (int) $category;
40     $post_parent = (int) $post_parent;
41
42     $inclusions = '';
43     if ( !empty($include) ) {
44         $offset = 0;    //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
45         $category = 0;
46         $exclude = '';
47         $meta_key = '';
48         $meta_value = '';
49         $post_parent = 0;
50         $incposts = preg_split('/[\s,]+/',$include);
51         $numberposts = count($incposts);  // only the number of posts included
52         if ( count($incposts) ) {
53             foreach ( $incposts as $incpost ) {
54                 if (empty($inclusions))
55                     $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpost);
56                 else
57                     $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpost);
58             }
59         }
60     }
61     if (!empty($inclusions))
62         $inclusions .= ')';
63
64     $exclusions = '';
65     if ( !empty($exclude) ) {
66         $exposts = preg_split('/[\s,]+/',$exclude);
67         if ( count($exposts) ) {
68             foreach ( $exposts as $expost ) {
69                 if (empty($exclusions))
70                     $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expost);
71                 else
72                     $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expost);
73             }
74         }
75     }
76     if (!empty($exclusions))
77         $exclusions .= ')';
78
79     // orderby
80     if ( preg_match( '/.+ +(ASC|DESC)/i', $orderby ) )
81         $order = ''; // orderby has its own order, so we'll use that
82
83     $query  = "SELECT DISTINCT * FROM $wpdb->posts ";
84     $query .= empty( $category ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy  ";
85     $query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
86     $query .= " WHERE 1=1 ";
87     $query .= empty( $post_type ) ? '' : $wpdb->prepare("AND post_type = %s ", $post_type);
88     $query .= empty( $post_status ) ? '' : $wpdb->prepare("AND post_status = %s ", $post_status);
89     $query .= "$exclusions $inclusions " ;
90     $query .= empty( $category ) ? '' : $wpdb->prepare("AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id = %d AND $wpdb->term_taxonomy.taxonomy = 'category')", $category);
91     $query .= empty( $post_parent ) ? '' : $wpdb->prepare("AND $wpdb->posts.post_parent = %d ", $post_parent);
92     // expected_slashed ($meta_key, $meta_value) -- Also, this looks really funky, doesn't seem like it works
93     $query .= empty( $meta_key ) | empty($meta_value)  ? '' : $wpdb->prepare(" AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = %s AND $wpdb->postmeta.meta_value = %s )", $meta_key, $meta_value);
94     $query .= empty( $post_mime_type ) ? '' : wp_post_mime_type_where($post_mime_type);
95     $query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
96     if ( 0 < $numberposts )
97         $query .= $wpdb->prepare(" LIMIT %d,%d", $offset, $numberposts);
98
99     $posts = $wpdb->get_results($query);
100
101     update_post_caches($posts);
102
103     return $posts;
104 }
105 // end the old get_posts function
106
107 // the new get_posts function
108 function get_posts_new($args = null) {
109     $defaults = array(
110         'numberposts' => 5, 'offset' => 0,
111         'category' => 0, 'orderby' => 'post_date',
112         'order' => 'DESC', 'include' => '',
113         'exclude' => '', 'meta_key' => '',
114         'meta_value' =>'', 'post_type' => 'post',
115         'post_status' => 'publish', 'post_parent' => 0
116     );
117
118     $r = wp_parse_args( $args, $defaults );
119     if ( ! empty($r['numberposts']) )
120         $r['posts_per_page'] = $r['numberposts'];
121     if ( ! empty($r['category']) )
122         $r['cat'] = $r['category'];
123     if ( ! empty($r['include']) ) {
124         $incposts = preg_split('/[\s,]+/',$r['include']);
125         $r['posts_per_page'] = count($incposts);  // only the number of posts included
126         $r['post__in'] = $incposts;
127     } elseif ( ! empty($r['exclude']) )
128         $r['post__not_in'] = preg_split('/[\s,]+/',$r['exclude']);
129     
130     $get_posts = new WP_Query;
131     return $get_posts->query($r);
132
133 }
134
135 global $wpdb;
136 $query = ( isset( $_REQUEST['get-posts-query'] ) ) ? stripslashes($_REQUEST['get-posts-query']) : '';
137 $use_cache = ( isset( $_REQUEST['use-cache'] ) ) ? (int) $_REQUEST['use-cache'] : false;
138 echo "<p>Query is: <code>$query</code><p>";
139 ?>
140 <form action="" method="post">
141 <label for="get-posts-query"><p>Enter query arguments to test out the functions. It's eval()'ed, so you can use arguments like <code>array('numberposts' => 3)</code> or <code>"numberposts=3"</code></p>
142 <input type="text" name="get-posts-query" id="get-posts-query" size="100" value="<?php echo htmlspecialchars($query) ?>" />
143 </label>
144 <p><label for="use-cache">Use Object Cache: <input type="checkbox" name="use-cache" value="1" <?php if ( $use_cache ) echo 'checked="checked"'; ?> /></label></p>
145 <input type="submit" value="Try Query" name="submit" />
146 </form>
147 <div style="width: 45%; float: left; border: 1px solid black; overflow: scroll;">
148 <h2>The old <code>get_posts</code> function:</h2>
149 <h3>Processing time: <?php
150 if ( $use_cache ) {
151     eval('$cache1 = get_posts_old(' . $query . ');');
152 } else {
153     $wpdb->flush();
154     wp_cache_flush();
155 }
156 timer_start();
157 eval('$old_posts = get_posts_old(' . $query . ');');
158 timer_stop(1); ?></h3>
159 <h3>Database queries: </h3><pre style="overflow: scroll; height: 10em;"><?php echo print_r($GLOBALS['test_wpdb_query']); ?></pre>
160 <?php if ( ! empty( $wpdb->queries ) ) : ?>
161 <h3>Saved db query info: </h3><pre style="overflow: scroll; height: 10em;"><?php echo print_r($wpdb->queries); ?></pre>
162 <?php endif; ?>
163 <pre>
164 <?php print_r($old_posts) ?>
165 </pre></div>
166
167 <div style="width: 45%; float: right; border: 1px solid black; overflow: scroll;">
168 <h2>The proposed <code>get_posts</code> function:</h2>
169 <h3>Processing time: <?php
170 unset($GLOBALS['test_wpdb_query']);
171 if ( $use_cache ) {
172     eval('$cache2 = get_posts_new(' . $query . ');');
173 } else {
174     $wpdb->flush();
175     wp_cache_flush();
176 }
177 timer_start();
178 eval('$new_posts = get_posts_new(' . $query . ');');
179 timer_stop(1); ?></h3>
180 <h3>Database queries: </h3><pre style="overflow: scroll; height: 10em;"><?php echo print_r($GLOBALS['test_wpdb_query']); ?></pre>
181 <?php if ( ! empty( $wpdb->queries ) ) : ?>
182 <h3>Saved db query info: </h3><pre style="overflow: scroll; height: 10em;"><?php echo print_r($wpdb->queries); ?></pre>
183 <?php endif; ?>
184 <pre>
185 <?php print_r($new_posts) ?>
186 </pre></div>
187 </body>
188 </html>
189