5 * The query API attempts to get which part of WordPress the user is on. It
6 * also provides functionality for getting URL query information.
8 * @link https://codex.wordpress.org/The_Loop More information on The Loop.
15 * Retrieve variable in the WP_Query class.
17 * @see WP_Query::get()
21 * @param string $var The variable key to retrieve.
22 * @param mixed $default Value to return if the query variable is not set. Default ''.
25 function get_query_var( $var, $default = '' ) {
28 return $wp_query->get( $var, $default );
32 * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
34 * @uses WP_Query::get_queried_object
41 function get_queried_object() {
43 return $wp_query->get_queried_object();
47 * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
49 * @uses WP_Query::get_queried_object_id()
56 function get_queried_object_id() {
58 return $wp_query->get_queried_object_id();
64 * @see WP_Query::set()
68 * @param string $var Query variable key.
72 function set_query_var($var, $value) {
75 return $wp_query->set($var, $value);
79 * Set up The Loop with query parameters.
81 * This will override the current WordPress Loop and shouldn't be used more than
82 * once. This must not be used within the WordPress Loop.
87 * @param string $query
88 * @return array List of posts
90 function query_posts($query) {
91 $GLOBALS['wp_query'] = new WP_Query();
92 return $GLOBALS['wp_query']->query($query);
96 * Destroy the previous query and set up a new query.
98 * This should be used after {@link query_posts()} and before another {@link
99 * query_posts()}. This will remove obscure bugs that occur when the previous
100 * wp_query object is not destroyed properly before another is set up.
105 function wp_reset_query() {
106 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
111 * After looping through a separate query, this function restores
112 * the $post global to the current post in the main query.
117 function wp_reset_postdata() {
120 if ( isset( $wp_query ) ) {
121 $wp_query->reset_postdata();
130 * Is the query for an existing archive page?
132 * Month, Year, Category, Author, Post Type archive...
134 * @see WP_Query::is_archive()
140 function is_archive() {
143 if ( ! isset( $wp_query ) ) {
144 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
148 return $wp_query->is_archive();
152 * Is the query for an existing post type archive page?
154 * @see WP_Query::is_post_type_archive()
158 * @param mixed $post_types Optional. Post type or array of posts types to check against.
161 function is_post_type_archive( $post_types = '' ) {
164 if ( ! isset( $wp_query ) ) {
165 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
169 return $wp_query->is_post_type_archive( $post_types );
173 * Is the query for an existing attachment page?
175 * @see WP_Query::is_attachment()
179 * @param int|string|array|object $attachment Attachment ID, title, slug, or array of such.
182 function is_attachment( $attachment = '' ) {
185 if ( ! isset( $wp_query ) ) {
186 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
190 return $wp_query->is_attachment( $attachment );
194 * Is the query for an existing author archive page?
196 * If the $author parameter is specified, this function will additionally
197 * check if the query is for one of the authors specified.
199 * @see WP_Query::is_author()
203 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
206 function is_author( $author = '' ) {
209 if ( ! isset( $wp_query ) ) {
210 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
214 return $wp_query->is_author( $author );
218 * Is the query for an existing category archive page?
220 * If the $category parameter is specified, this function will additionally
221 * check if the query is for one of the categories specified.
223 * @see WP_Query::is_category()
227 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
230 function is_category( $category = '' ) {
233 if ( ! isset( $wp_query ) ) {
234 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
238 return $wp_query->is_category( $category );
242 * Is the query for an existing tag archive page?
244 * If the $tag parameter is specified, this function will additionally
245 * check if the query is for one of the tags specified.
247 * @see WP_Query::is_tag()
251 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
254 function is_tag( $tag = '' ) {
257 if ( ! isset( $wp_query ) ) {
258 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
262 return $wp_query->is_tag( $tag );
266 * Is the query for an existing taxonomy archive page?
268 * If the $taxonomy parameter is specified, this function will additionally
269 * check if the query is for that specific $taxonomy.
271 * If the $term parameter is specified in addition to the $taxonomy parameter,
272 * this function will additionally check if the query is for one of the terms
275 * @see WP_Query::is_tax()
279 * @param string|array $taxonomy Optional. Taxonomy slug or slugs.
280 * @param int|string|array $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
283 function is_tax( $taxonomy = '', $term = '' ) {
286 if ( ! isset( $wp_query ) ) {
287 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
291 return $wp_query->is_tax( $taxonomy, $term );
295 * Whether the current URL is within the comments popup window.
297 * @see WP_Query::is_comments_popup()
303 function is_comments_popup() {
306 if ( ! isset( $wp_query ) ) {
307 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
311 return $wp_query->is_comments_popup();
315 * Is the query for an existing date archive?
317 * @see WP_Query::is_date()
326 if ( ! isset( $wp_query ) ) {
327 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
331 return $wp_query->is_date();
335 * Is the query for an existing day archive?
337 * @see WP_Query::is_day()
346 if ( ! isset( $wp_query ) ) {
347 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
351 return $wp_query->is_day();
355 * Is the query for a feed?
357 * @see WP_Query::is_feed()
361 * @param string|array $feeds Optional feed types to check.
364 function is_feed( $feeds = '' ) {
367 if ( ! isset( $wp_query ) ) {
368 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
372 return $wp_query->is_feed( $feeds );
376 * Is the query for a comments feed?
378 * @see WP_Query::is_comments_feed()
384 function is_comment_feed() {
387 if ( ! isset( $wp_query ) ) {
388 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
392 return $wp_query->is_comment_feed();
396 * Is the query for the front page of the site?
398 * This is for what is displayed at your site's main URL.
400 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
402 * If you set a static page for the front page of your site, this function will return
403 * true when viewing that page.
405 * Otherwise the same as @see is_home()
407 * @see WP_Query::is_front_page()
410 * @return bool True, if front of site.
412 function is_front_page() {
415 if ( ! isset( $wp_query ) ) {
416 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
420 return $wp_query->is_front_page();
424 * Is the query for the blog homepage?
426 * This is the page which shows the time based blog content of your site.
428 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
430 * If you set a static page for the front page of your site, this function will return
431 * true only on the page you set as the "Posts page".
433 * @see is_front_page()
435 * @see WP_Query::is_home()
439 * @return bool True if blog view homepage.
444 if ( ! isset( $wp_query ) ) {
445 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
449 return $wp_query->is_home();
453 * Is the query for an existing month archive?
455 * @see WP_Query::is_month()
461 function is_month() {
464 if ( ! isset( $wp_query ) ) {
465 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
469 return $wp_query->is_month();
473 * Is the query for an existing single page?
475 * If the $page parameter is specified, this function will additionally
476 * check if the query is for one of the pages specified.
481 * @see WP_Query::is_page()
485 * @param mixed $page Page ID, title, slug, or array of such.
488 function is_page( $page = '' ) {
491 if ( ! isset( $wp_query ) ) {
492 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
496 return $wp_query->is_page( $page );
500 * Is the query for paged result and not for the first page?
502 * @see WP_Query::is_paged()
508 function is_paged() {
511 if ( ! isset( $wp_query ) ) {
512 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
516 return $wp_query->is_paged();
520 * Is the query for a post or page preview?
522 * @see WP_Query::is_preview()
528 function is_preview() {
531 if ( ! isset( $wp_query ) ) {
532 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
536 return $wp_query->is_preview();
540 * Is the query for the robots file?
542 * @see WP_Query::is_robots()
548 function is_robots() {
551 if ( ! isset( $wp_query ) ) {
552 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
556 return $wp_query->is_robots();
560 * Is the query for a search?
562 * @see WP_Query::is_search()
568 function is_search() {
571 if ( ! isset( $wp_query ) ) {
572 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
576 return $wp_query->is_search();
580 * Is the query for an existing single post?
582 * Works for any post type, except attachments and pages
584 * If the $post parameter is specified, this function will additionally
585 * check if the query is for one of the Posts specified.
590 * @see WP_Query::is_single()
594 * @param mixed $post Post ID, title, slug, or array of such.
597 function is_single( $post = '' ) {
600 if ( ! isset( $wp_query ) ) {
601 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
605 return $wp_query->is_single( $post );
609 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
611 * If the $post_types parameter is specified, this function will additionally
612 * check if the query is for one of the Posts Types specified.
617 * @see WP_Query::is_singular()
621 * @param mixed $post_types Optional. Post Type or array of Post Types
624 function is_singular( $post_types = '' ) {
627 if ( ! isset( $wp_query ) ) {
628 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
632 return $wp_query->is_singular( $post_types );
636 * Is the query for a specific time?
638 * @see WP_Query::is_time()
647 if ( ! isset( $wp_query ) ) {
648 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
652 return $wp_query->is_time();
656 * Is the query for a trackback endpoint call?
658 * @see WP_Query::is_trackback()
664 function is_trackback() {
667 if ( ! isset( $wp_query ) ) {
668 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
672 return $wp_query->is_trackback();
676 * Is the query for an existing year archive?
678 * @see WP_Query::is_year()
687 if ( ! isset( $wp_query ) ) {
688 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
692 return $wp_query->is_year();
696 * Is the query a 404 (returns no results)?
698 * @see WP_Query::is_404()
707 if ( ! isset( $wp_query ) ) {
708 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
712 return $wp_query->is_404();
716 * Is the query the main query?
722 function is_main_query() {
723 if ( 'pre_get_posts' === current_filter() ) {
724 $message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
725 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'https://codex.wordpress.org/Function_Reference/is_main_query' ) );
726 _doing_it_wrong( __FUNCTION__, $message, '3.7' );
730 return $wp_query->is_main_query();
734 * The Loop. Post loop control.
738 * Whether current WordPress query has results to loop over.
740 * @see WP_Query::have_posts()
746 function have_posts() {
749 return $wp_query->have_posts();
753 * Whether the caller is in the Loop.
758 * @return bool True if caller is within loop, false if loop hasn't started or ended.
760 function in_the_loop() {
763 return $wp_query->in_the_loop;
767 * Rewind the loop posts.
769 * @see WP_Query::rewind_posts()
775 function rewind_posts() {
778 return $wp_query->rewind_posts();
782 * Iterate the post index in the loop.
784 * @see WP_Query::the_post()
788 function the_post() {
791 $wp_query->the_post();
799 * Whether there are comments to loop over.
801 * @see WP_Query::have_comments()
807 function have_comments() {
809 return $wp_query->have_comments();
813 * Iterate comment index in the comment loop.
815 * @see WP_Query::the_comment()
821 function the_comment() {
823 return $wp_query->the_comment();
831 * The WordPress Query class.
833 * @link https://codex.wordpress.org/Function_Reference/WP_Query Codex page.
840 * Query vars set by the user
849 * Query vars, after parsing
855 public $query_vars = array();
858 * Taxonomy query, as passed to get_tax_sql()
862 * @var object WP_Tax_Query
867 * Metadata query container
871 * @var object WP_Meta_Query
873 public $meta_query = false;
876 * Date query container
880 * @var object WP_Date_Query
882 public $date_query = false;
885 * Holds the data for a single object that is queried.
887 * Holds the contents of a post, page, category, attachment.
893 public $queried_object;
896 * The ID of the queried object.
902 public $queried_object_id;
905 * Get post database query.
923 * The amount of posts for the current query.
929 public $post_count = 0;
932 * Index of the current item in the loop.
938 public $current_post = -1;
941 * Whether the loop has started and the caller is in the loop.
947 public $in_the_loop = false;
959 * The list of comments for current post.
968 * The amount of comments for the posts.
974 public $comment_count = 0;
977 * The index of the comment in the comment loop.
983 public $current_comment = -1;
986 * Current comment ID.
995 * The amount of found posts for the current query.
997 * If limit clause was not used, equals $post_count.
1003 public $found_posts = 0;
1006 * The amount of pages.
1012 public $max_num_pages = 0;
1015 * The amount of comment pages.
1021 public $max_num_comment_pages = 0;
1024 * Set if query is single post.
1030 public $is_single = false;
1033 * Set if query is preview of blog.
1039 public $is_preview = false;
1042 * Set if query returns a page.
1048 public $is_page = false;
1051 * Set if query is an archive list.
1057 public $is_archive = false;
1060 * Set if query is part of a date.
1066 public $is_date = false;
1069 * Set if query contains a year.
1075 public $is_year = false;
1078 * Set if query contains a month.
1084 public $is_month = false;
1087 * Set if query contains a day.
1093 public $is_day = false;
1096 * Set if query contains time.
1102 public $is_time = false;
1105 * Set if query contains an author.
1111 public $is_author = false;
1114 * Set if query contains category.
1120 public $is_category = false;
1123 * Set if query contains tag.
1129 public $is_tag = false;
1132 * Set if query contains taxonomy.
1138 public $is_tax = false;
1141 * Set if query was part of a search result.
1147 public $is_search = false;
1150 * Set if query is feed display.
1156 public $is_feed = false;
1159 * Set if query is comment feed display.
1165 public $is_comment_feed = false;
1168 * Set if query is trackback.
1174 public $is_trackback = false;
1177 * Set if query is blog homepage.
1183 public $is_home = false;
1186 * Set if query couldn't found anything.
1192 public $is_404 = false;
1195 * Set if query is within comments popup window.
1201 public $is_comments_popup = false;
1204 * Set if query is paged
1210 public $is_paged = false;
1213 * Set if query is part of administration page.
1219 public $is_admin = false;
1222 * Set if query is an attachment.
1228 public $is_attachment = false;
1231 * Set if is single, is a page, or is an attachment.
1237 public $is_singular = false;
1240 * Set if query is for robots.
1246 public $is_robots = false;
1249 * Set if query contains posts.
1251 * Basically, the homepage if the option isn't set for the static homepage.
1257 public $is_posts_page = false;
1260 * Set if query is for a post type archive.
1266 public $is_post_type_archive = false;
1269 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1270 * whether we have to re-parse because something has changed
1276 private $query_vars_hash = false;
1279 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1280 * via pre_get_posts hooks.
1285 private $query_vars_changed = true;
1288 * Set if post thumbnails are cached
1294 public $thumbnails_cached = false;
1297 * Cached list of search stopwords.
1304 private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' );
1306 private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
1309 * Resets query flags to false.
1311 * The query flags are what page info WordPress was able to figure out.
1316 private function init_query_flags() {
1317 $this->is_single = false;
1318 $this->is_preview = false;
1319 $this->is_page = false;
1320 $this->is_archive = false;
1321 $this->is_date = false;
1322 $this->is_year = false;
1323 $this->is_month = false;
1324 $this->is_day = false;
1325 $this->is_time = false;
1326 $this->is_author = false;
1327 $this->is_category = false;
1328 $this->is_tag = false;
1329 $this->is_tax = false;
1330 $this->is_search = false;
1331 $this->is_feed = false;
1332 $this->is_comment_feed = false;
1333 $this->is_trackback = false;
1334 $this->is_home = false;
1335 $this->is_404 = false;
1336 $this->is_comments_popup = false;
1337 $this->is_paged = false;
1338 $this->is_admin = false;
1339 $this->is_attachment = false;
1340 $this->is_singular = false;
1341 $this->is_robots = false;
1342 $this->is_posts_page = false;
1343 $this->is_post_type_archive = false;
1347 * Initiates object properties and sets default values.
1352 public function init() {
1353 unset($this->posts);
1354 unset($this->query);
1355 $this->query_vars = array();
1356 unset($this->queried_object);
1357 unset($this->queried_object_id);
1358 $this->post_count = 0;
1359 $this->current_post = -1;
1360 $this->in_the_loop = false;
1361 unset( $this->request );
1362 unset( $this->post );
1363 unset( $this->comments );
1364 unset( $this->comment );
1365 $this->comment_count = 0;
1366 $this->current_comment = -1;
1367 $this->found_posts = 0;
1368 $this->max_num_pages = 0;
1369 $this->max_num_comment_pages = 0;
1371 $this->init_query_flags();
1375 * Reparse the query vars.
1380 public function parse_query_vars() {
1381 $this->parse_query();
1385 * Fills in the query variables, which do not exist within the parameter.
1390 * @param array $array Defined query variables.
1391 * @return array Complete query variables with undefined ones filled in empty.
1393 public function fill_query_vars($array) {
1433 foreach ( $keys as $key ) {
1434 if ( !isset($array[$key]) )
1438 $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1439 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
1440 'author__in', 'author__not_in' );
1442 foreach ( $array_keys as $key ) {
1443 if ( !isset($array[$key]) )
1444 $array[$key] = array();
1450 * Parse a query string and set query type booleans.
1453 * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
1454 * array key to `$orderby`.
1457 * @param string|array $query {
1458 * Optional. Array or string of Query parameters.
1460 * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type.
1461 * @type int|string $author Author ID, or comma-separated list of IDs.
1462 * @type string $author_name User 'user_nicename'.
1463 * @type array $author__in An array of author IDs to query from.
1464 * @type array $author__not_in An array of author IDs not to query from.
1465 * @type bool $cache_results Whether to cache post information. Default true.
1466 * @type int|string $cat Category ID or comma-separated list of IDs (this or any children).
1467 * @type array $category__and An array of category IDs (AND in).
1468 * @type array $category__in An array of category IDs (OR in, no children).
1469 * @type array $category__not_in An array of category IDs (NOT in).
1470 * @type string $category_name Use category slug (not name, this or any children).
1471 * @type int $comments_per_page The number of comments to return per page.
1472 * Default 'comments_per_page' option.
1473 * @type int|string $comments_popup Whether the query is within the comments popup. Default empty.
1474 * @type array $date_query An associative array of WP_Date_Query arguments.
1475 * {@see WP_Date_Query::__construct()}
1476 * @type int $day Day of the month. Default empty. Accepts numbers 1-31.
1477 * @type bool $exact Whether to search by exact keyword. Default false.
1478 * @type string|array $fields Which fields to return. Single field or all fields (string),
1479 * or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
1480 * Default all fields. Accepts 'ids', 'id=>parent'.
1481 * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23.
1482 * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
1483 * excludes stickies from 'post__in'. Accepts 1|true, 0|false.
1485 * @type int $m Combination YearMonth. Accepts any four-digit year and month
1486 * numbers 1-12. Default empty.
1487 * @type string $meta_compare Comparison operator to test the 'meta_value'.
1488 * @type string $meta_key Custom field key.
1489 * @type array $meta_query An associative array of WP_Meta_Query arguments.
1490 * {@see WP_Meta_Query->queries}
1491 * @type string $meta_value Custom field value.
1492 * @type int $meta_value_num Custom field value number.
1493 * @type int $menu_order The menu order of the posts.
1494 * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12.
1495 * @type string $name Post slug.
1496 * @type bool $nopaging Show all posts (true) or paginate (false). Default false.
1497 * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve
1498 * performance. Default false.
1499 * @type int $offset The number of posts to offset before retrieval.
1500 * @type string $order Designates ascending or descending order of posts. Default 'DESC'.
1501 * Accepts 'ASC', 'DESC'.
1502 * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be
1503 * passed. To use 'meta_value', or 'meta_value_num',
1504 * 'meta_key=keyname' must be also be defined. To sort by a
1505 * specific `$meta_query` clause, use that clause's array key.
1506 * Default 'date'. Accepts 'none', 'name', 'author', 'date',
1507 * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',
1508 * 'comment_count', 'meta_value', 'meta_value_num', and the
1509 * array keys of `$meta_query`.
1510 * @type int $p Post ID.
1511 * @type int $page Show the number of posts that would show up on page X of a
1512 * static front page.
1513 * @type int $paged The number of the current page.
1514 * @type int $page_id Page ID.
1515 * @type string $pagename Page slug.
1516 * @type string $perm Show posts if user has the appropriate capability.
1517 * @type array $post__in An array of post IDs to retrieve, sticky posts will be included
1518 * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type.
1519 * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma-
1520 * separated IDs will NOT work.
1521 * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve
1523 * @type array $post_parent__in An array containing parent page IDs to query child pages from.
1524 * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from.
1525 * @type string|array $post_type A post type slug (string) or array of post type slugs.
1526 * Default 'any' if using 'tax_query'.
1527 * @type string|array $post_status A post status (string) or array of post statuses.
1528 * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts.
1529 * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
1530 * 'posts_per_page' when is_archive(), or is_search() are true.
1531 * @type string $s Search keyword.
1532 * @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
1533 * @type array $search_terms Array of search terms.
1534 * @type bool $sentence Whether to search by phrase. Default false.
1535 * @type bool $suppress_filters Whether to suppress filters. Default false.
1536 * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all).
1537 * @type array $tag__and An array of tag ids (AND in).
1538 * @type array $tag__in An array of tag ids (OR in).
1539 * @type array $tag__not_in An array of tag ids (NOT in).
1540 * @type int $tag_id Tag id or comma-separated list of IDs.
1541 * @type array $tag_slug__and An array of tag slugs (AND in).
1542 * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is
1543 * true. Note: a string of comma-separated IDs will NOT work.
1544 * @type array $tax_query An associative array of WP_Tax_Query arguments.
1545 * {@see WP_Tax_Query->queries}
1546 * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
1547 * @type bool $update_post_term_cache Whether to update the post term cache. Default true.
1548 * @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
1549 * @type int $year The four-digit year. Default empty. Accepts any four-digit year.
1552 public function parse_query( $query = '' ) {
1553 if ( ! empty( $query ) ) {
1555 $this->query = $this->query_vars = wp_parse_args( $query );
1556 } elseif ( ! isset( $this->query ) ) {
1557 $this->query = $this->query_vars;
1560 $this->query_vars = $this->fill_query_vars($this->query_vars);
1561 $qv = &$this->query_vars;
1562 $this->query_vars_changed = true;
1564 if ( ! empty($qv['robots']) )
1565 $this->is_robots = true;
1567 $qv['p'] = absint($qv['p']);
1568 $qv['page_id'] = absint($qv['page_id']);
1569 $qv['year'] = absint($qv['year']);
1570 $qv['monthnum'] = absint($qv['monthnum']);
1571 $qv['day'] = absint($qv['day']);
1572 $qv['w'] = absint($qv['w']);
1573 $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
1574 $qv['paged'] = absint($qv['paged']);
1575 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1576 $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
1577 $qv['pagename'] = trim( $qv['pagename'] );
1578 $qv['name'] = trim( $qv['name'] );
1579 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1580 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1581 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1582 if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
1584 // Fairly insane upper bound for search string lengths.
1585 if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
1589 // Compat. Map subpost to attachment.
1590 if ( '' != $qv['subpost'] )
1591 $qv['attachment'] = $qv['subpost'];
1592 if ( '' != $qv['subpost_id'] )
1593 $qv['attachment_id'] = $qv['subpost_id'];
1595 $qv['attachment_id'] = absint($qv['attachment_id']);
1597 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1598 $this->is_single = true;
1599 $this->is_attachment = true;
1600 } elseif ( '' != $qv['name'] ) {
1601 $this->is_single = true;
1602 } elseif ( $qv['p'] ) {
1603 $this->is_single = true;
1604 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1605 // If year, month, day, hour, minute, and second are set, a single
1606 // post is being queried.
1607 $this->is_single = true;
1608 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1609 $this->is_page = true;
1610 $this->is_single = false;
1612 // Look for archive queries. Dates, categories, authors, search, post type archives.
1614 if ( isset( $this->query['s'] ) ) {
1615 $this->is_search = true;
1618 if ( '' !== $qv['second'] ) {
1619 $this->is_time = true;
1620 $this->is_date = true;
1623 if ( '' !== $qv['minute'] ) {
1624 $this->is_time = true;
1625 $this->is_date = true;
1628 if ( '' !== $qv['hour'] ) {
1629 $this->is_time = true;
1630 $this->is_date = true;
1634 if ( ! $this->is_date ) {
1635 $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
1636 if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
1637 $qv['error'] = '404';
1639 $this->is_day = true;
1640 $this->is_date = true;
1645 if ( $qv['monthnum'] ) {
1646 if ( ! $this->is_date ) {
1647 if ( 12 < $qv['monthnum'] ) {
1648 $qv['error'] = '404';
1650 $this->is_month = true;
1651 $this->is_date = true;
1656 if ( $qv['year'] ) {
1657 if ( ! $this->is_date ) {
1658 $this->is_year = true;
1659 $this->is_date = true;
1664 $this->is_date = true;
1665 if ( strlen($qv['m']) > 9 ) {
1666 $this->is_time = true;
1667 } elseif ( strlen( $qv['m'] ) > 7 ) {
1668 $this->is_day = true;
1669 } elseif ( strlen( $qv['m'] ) > 5 ) {
1670 $this->is_month = true;
1672 $this->is_year = true;
1676 if ( '' != $qv['w'] ) {
1677 $this->is_date = true;
1680 $this->query_vars_hash = false;
1681 $this->parse_tax_query( $qv );
1683 foreach ( $this->tax_query->queries as $tax_query ) {
1684 if ( ! is_array( $tax_query ) ) {
1688 if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
1689 switch ( $tax_query['taxonomy'] ) {
1691 $this->is_category = true;
1694 $this->is_tag = true;
1697 $this->is_tax = true;
1701 unset( $tax_query );
1703 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1704 $this->is_author = false;
1706 $this->is_author = true;
1709 if ( '' != $qv['author_name'] )
1710 $this->is_author = true;
1712 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1713 $post_type_obj = get_post_type_object( $qv['post_type'] );
1714 if ( ! empty( $post_type_obj->has_archive ) )
1715 $this->is_post_type_archive = true;
1718 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1719 $this->is_archive = true;
1722 if ( '' != $qv['feed'] )
1723 $this->is_feed = true;
1725 if ( '' != $qv['tb'] )
1726 $this->is_trackback = true;
1728 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1729 $this->is_paged = true;
1731 if ( '' != $qv['comments_popup'] )
1732 $this->is_comments_popup = true;
1734 // if we're previewing inside the write screen
1735 if ( '' != $qv['preview'] )
1736 $this->is_preview = true;
1739 $this->is_admin = true;
1741 if ( false !== strpos($qv['feed'], 'comments-') ) {
1742 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1743 $qv['withcomments'] = 1;
1746 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1748 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1749 $this->is_comment_feed = true;
1751 if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup || $this->is_robots ) )
1752 $this->is_home = true;
1754 // Correct is_* for page_on_front and page_for_posts
1755 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1756 $_query = wp_parse_args($this->query);
1757 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1758 if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1759 unset($_query['pagename']);
1760 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1761 $this->is_page = true;
1762 $this->is_home = false;
1763 $qv['page_id'] = get_option('page_on_front');
1764 // Correct <!--nextpage--> for page_on_front
1765 if ( !empty($qv['paged']) ) {
1766 $qv['page'] = $qv['paged'];
1767 unset($qv['paged']);
1772 if ( '' != $qv['pagename'] ) {
1773 $this->queried_object = get_page_by_path($qv['pagename']);
1774 if ( !empty($this->queried_object) )
1775 $this->queried_object_id = (int) $this->queried_object->ID;
1777 unset($this->queried_object);
1779 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1780 $this->is_page = false;
1781 $this->is_home = true;
1782 $this->is_posts_page = true;
1786 if ( $qv['page_id'] ) {
1787 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1788 $this->is_page = false;
1789 $this->is_home = true;
1790 $this->is_posts_page = true;
1794 if ( !empty($qv['post_type']) ) {
1795 if ( is_array($qv['post_type']) )
1796 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1798 $qv['post_type'] = sanitize_key($qv['post_type']);
1801 if ( ! empty( $qv['post_status'] ) ) {
1802 if ( is_array( $qv['post_status'] ) )
1803 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1805 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1808 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1809 $this->is_comment_feed = false;
1811 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1812 // Done correcting is_* for page_on_front and page_for_posts
1814 if ( '404' == $qv['error'] )
1817 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1818 $this->query_vars_changed = false;
1821 * Fires after the main query vars have been parsed.
1825 * @param WP_Query &$this The WP_Query instance (passed by reference).
1827 do_action_ref_array( 'parse_query', array( &$this ) );
1831 * Parses various taxonomy related query vars.
1833 * For BC, this method is not marked as protected. See [28987].
1838 * @param array &$q The query variables
1840 public function parse_tax_query( &$q ) {
1841 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1842 $tax_query = $q['tax_query'];
1844 $tax_query = array();
1847 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1848 $tax_query[] = array(
1849 'taxonomy' => $q['taxonomy'],
1850 'terms' => array( $q['term'] ),
1855 foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
1856 if ( 'post_tag' == $taxonomy )
1857 continue; // Handled further down in the $q['tag'] block
1859 if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1860 $tax_query_defaults = array(
1861 'taxonomy' => $taxonomy,
1865 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1866 $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1869 $term = $q[$t->query_var];
1871 if ( strpos($term, '+') !== false ) {
1872 $terms = preg_split( '/[+]+/', $term );
1873 foreach ( $terms as $term ) {
1874 $tax_query[] = array_merge( $tax_query_defaults, array(
1875 'terms' => array( $term )
1879 $tax_query[] = array_merge( $tax_query_defaults, array(
1880 'terms' => preg_split( '/[,]+/', $term )
1887 if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
1888 $cat_in = $cat_not_in = array();
1890 $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
1891 $cat_array = array_map( 'intval', $cat_array );
1892 $q['cat'] = implode( ',', $cat_array );
1894 foreach ( $cat_array as $cat ) {
1898 $cat_not_in[] = abs( $cat );
1901 if ( ! empty( $cat_in ) ) {
1902 $tax_query[] = array(
1903 'taxonomy' => 'category',
1905 'field' => 'term_id',
1906 'include_children' => true
1910 if ( ! empty( $cat_not_in ) ) {
1911 $tax_query[] = array(
1912 'taxonomy' => 'category',
1913 'terms' => $cat_not_in,
1914 'field' => 'term_id',
1915 'operator' => 'NOT IN',
1916 'include_children' => true
1919 unset( $cat_array, $cat_in, $cat_not_in );
1922 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
1923 $q['category__and'] = (array) $q['category__and'];
1924 if ( ! isset( $q['category__in'] ) )
1925 $q['category__in'] = array();
1926 $q['category__in'][] = absint( reset( $q['category__and'] ) );
1927 unset( $q['category__and'] );
1930 if ( ! empty( $q['category__in'] ) ) {
1931 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
1932 $tax_query[] = array(
1933 'taxonomy' => 'category',
1934 'terms' => $q['category__in'],
1935 'field' => 'term_id',
1936 'include_children' => false
1940 if ( ! empty($q['category__not_in']) ) {
1941 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
1942 $tax_query[] = array(
1943 'taxonomy' => 'category',
1944 'terms' => $q['category__not_in'],
1945 'operator' => 'NOT IN',
1946 'include_children' => false
1950 if ( ! empty($q['category__and']) ) {
1951 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
1952 $tax_query[] = array(
1953 'taxonomy' => 'category',
1954 'terms' => $q['category__and'],
1955 'field' => 'term_id',
1956 'operator' => 'AND',
1957 'include_children' => false
1962 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1963 if ( strpos($q['tag'], ',') !== false ) {
1964 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
1965 foreach ( (array) $tags as $tag ) {
1966 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1967 $q['tag_slug__in'][] = $tag;
1969 } elseif ( preg_match('/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) {
1970 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
1971 foreach ( (array) $tags as $tag ) {
1972 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1973 $q['tag_slug__and'][] = $tag;
1976 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1977 $q['tag_slug__in'][] = $q['tag'];
1981 if ( !empty($q['tag_id']) ) {
1982 $q['tag_id'] = absint( $q['tag_id'] );
1983 $tax_query[] = array(
1984 'taxonomy' => 'post_tag',
1985 'terms' => $q['tag_id']
1989 if ( !empty($q['tag__in']) ) {
1990 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1991 $tax_query[] = array(
1992 'taxonomy' => 'post_tag',
1993 'terms' => $q['tag__in']
1997 if ( !empty($q['tag__not_in']) ) {
1998 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1999 $tax_query[] = array(
2000 'taxonomy' => 'post_tag',
2001 'terms' => $q['tag__not_in'],
2002 'operator' => 'NOT IN'
2006 if ( !empty($q['tag__and']) ) {
2007 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
2008 $tax_query[] = array(
2009 'taxonomy' => 'post_tag',
2010 'terms' => $q['tag__and'],
2015 if ( !empty($q['tag_slug__in']) ) {
2016 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
2017 $tax_query[] = array(
2018 'taxonomy' => 'post_tag',
2019 'terms' => $q['tag_slug__in'],
2024 if ( !empty($q['tag_slug__and']) ) {
2025 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
2026 $tax_query[] = array(
2027 'taxonomy' => 'post_tag',
2028 'terms' => $q['tag_slug__and'],
2034 $this->tax_query = new WP_Tax_Query( $tax_query );
2037 * Fires after taxonomy-related query vars have been parsed.
2041 * @param WP_Query $this The WP_Query instance.
2043 do_action( 'parse_tax_query', $this );
2047 * Generate SQL for the WHERE clause based on passed search terms.
2051 * @global wpdb $wpdb
2052 * @param array $q Query variables.
2053 * @return string WHERE clause.
2055 protected function parse_search( &$q ) {
2060 // added slashes screw with quote grouping when done early, so done later
2061 $q['s'] = stripslashes( $q['s'] );
2062 if ( empty( $_GET['s'] ) && $this->is_main_query() )
2063 $q['s'] = urldecode( $q['s'] );
2064 // there are no line breaks in <input /> fields
2065 $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
2066 $q['search_terms_count'] = 1;
2067 if ( ! empty( $q['sentence'] ) ) {
2068 $q['search_terms'] = array( $q['s'] );
2070 if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
2071 $q['search_terms_count'] = count( $matches[0] );
2072 $q['search_terms'] = $this->parse_search_terms( $matches[0] );
2073 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
2074 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
2075 $q['search_terms'] = array( $q['s'] );
2077 $q['search_terms'] = array( $q['s'] );
2081 $n = ! empty( $q['exact'] ) ? '' : '%';
2083 $q['search_orderby_title'] = array();
2084 foreach ( $q['search_terms'] as $term ) {
2086 $like = '%' . $wpdb->esc_like( $term ) . '%';
2087 $q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );
2090 $like = $n . $wpdb->esc_like( $term ) . $n;
2091 $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title LIKE %s) OR ($wpdb->posts.post_content LIKE %s))", $like, $like );
2092 $searchand = ' AND ';
2095 if ( ! empty( $search ) ) {
2096 $search = " AND ({$search}) ";
2097 if ( ! is_user_logged_in() )
2098 $search .= " AND ($wpdb->posts.post_password = '') ";
2105 * Check if the terms are suitable for searching.
2107 * Uses an array of stopwords (terms) that are excluded from the separate
2108 * term matching when searching for posts. The list of English stopwords is
2109 * the approximate search engines list, and is translatable.
2113 * @param array $terms Terms to check.
2114 * @return array Terms that are not stopwords.
2116 protected function parse_search_terms( $terms ) {
2117 $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
2120 $stopwords = $this->get_search_stopwords();
2122 foreach ( $terms as $term ) {
2123 // keep before/after spaces when term is for exact match
2124 if ( preg_match( '/^".+"$/', $term ) )
2125 $term = trim( $term, "\"'" );
2127 $term = trim( $term, "\"' " );
2129 // Avoid single A-Z.
2130 if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
2133 if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
2143 * Retrieve stopwords used when parsing search terms.
2147 * @return array Stopwords.
2149 protected function get_search_stopwords() {
2150 if ( isset( $this->stopwords ) )
2151 return $this->stopwords;
2153 /* translators: This is a comma-separated list of very common words that should be excluded from a search,
2154 * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
2155 * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
2157 $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
2158 'Comma-separated list of search stopwords in your language' ) );
2160 $stopwords = array();
2161 foreach( $words as $word ) {
2162 $word = trim( $word, "\r\n\t " );
2164 $stopwords[] = $word;
2168 * Filter stopwords used when parsing search terms.
2172 * @param array $stopwords Stopwords.
2174 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
2175 return $this->stopwords;
2179 * Generate SQL for the ORDER BY condition based on passed search terms.
2181 * @global wpdb $wpdb
2182 * @param array $q Query variables.
2183 * @return string ORDER BY clause.
2185 protected function parse_search_order( &$q ) {
2188 if ( $q['search_terms_count'] > 1 ) {
2189 $num_terms = count( $q['search_orderby_title'] );
2190 $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
2192 $search_orderby = '(CASE ';
2193 // sentence match in 'post_title'
2194 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
2196 // sanity limit, sort as sentence when more than 6 terms
2197 // (few searches are longer than 6 terms and most titles are not)
2198 if ( $num_terms < 7 ) {
2199 // all words in title
2200 $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
2201 // any word in title, not needed when $num_terms == 1
2202 if ( $num_terms > 1 )
2203 $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
2206 // sentence match in 'post_content'
2207 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
2208 $search_orderby .= 'ELSE 5 END)';
2210 // single word or sentence search
2211 $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
2214 return $search_orderby;
2218 * If the passed orderby value is allowed, convert the alias to a
2219 * properly-prefixed orderby value.
2224 * @global wpdb $wpdb WordPress database abstraction object.
2226 * @param string $orderby Alias for the field to order by.
2227 * @return string|bool Table-prefixed value to used in the ORDER clause. False otherwise.
2229 protected function parse_orderby( $orderby ) {
2232 // Used to filter values.
2233 $allowed_keys = array(
2234 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
2235 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified',
2236 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',
2239 $primary_meta_key = '';
2240 $primary_meta_query = false;
2241 $meta_clauses = $this->meta_query->get_clauses();
2242 if ( ! empty( $meta_clauses ) ) {
2243 $primary_meta_query = reset( $meta_clauses );
2245 if ( ! empty( $primary_meta_query['key'] ) ) {
2246 $primary_meta_key = $primary_meta_query['key'];
2247 $allowed_keys[] = $primary_meta_key;
2250 $allowed_keys[] = 'meta_value';
2251 $allowed_keys[] = 'meta_value_num';
2252 $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
2255 if ( ! in_array( $orderby, $allowed_keys ) ) {
2259 switch ( $orderby ) {
2264 case 'post_modified':
2269 case 'comment_count':
2270 $orderby_clause = "$wpdb->posts.{$orderby}";
2273 $orderby_clause = 'RAND()';
2275 case $primary_meta_key:
2277 if ( ! empty( $primary_meta_query['type'] ) ) {
2278 $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
2280 $orderby_clause = "{$primary_meta_query['alias']}.meta_value";
2283 case 'meta_value_num':
2284 $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
2287 if ( array_key_exists( $orderby, $meta_clauses ) ) {
2288 // $orderby corresponds to a meta_query clause.
2289 $meta_clause = $meta_clauses[ $orderby ];
2290 $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
2292 // Default: order by post field.
2293 $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby );
2299 return $orderby_clause;
2303 * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
2308 * @param string $order The 'order' query variable.
2309 * @return string The sanitized 'order' query variable.
2311 protected function parse_order( $order ) {
2312 if ( ! is_string( $order ) || empty( $order ) ) {
2316 if ( 'ASC' === strtoupper( $order ) ) {
2324 * Sets the 404 property and saves whether query is feed.
2329 public function set_404() {
2330 $is_feed = $this->is_feed;
2332 $this->init_query_flags();
2333 $this->is_404 = true;
2335 $this->is_feed = $is_feed;
2339 * Retrieve query variable.
2344 * @param string $query_var Query variable key.
2345 * @param mixed $default Value to return if the query variable is not set. Default ''.
2348 public function get( $query_var, $default = '' ) {
2349 if ( isset( $this->query_vars[ $query_var ] ) ) {
2350 return $this->query_vars[ $query_var ];
2357 * Set query variable.
2362 * @param string $query_var Query variable key.
2363 * @param mixed $value Query variable value.
2365 public function set($query_var, $value) {
2366 $this->query_vars[$query_var] = $value;
2370 * Retrieve the posts based on query variables.
2372 * There are a few filters and actions that can be used to modify the post
2378 * @return array List of posts.
2380 public function get_posts() {
2383 $this->parse_query();
2386 * Fires after the query variable object is created, but before the actual query is run.
2388 * Note: If using conditional tags, use the method versions within the passed instance
2389 * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
2390 * like is_main_query() test against the global $wp_query instance, not the passed one.
2394 * @param WP_Query &$this The WP_Query instance (passed by reference).
2396 do_action_ref_array( 'pre_get_posts', array( &$this ) );
2399 $q = &$this->query_vars;
2401 // Fill again in case pre_get_posts unset some vars.
2402 $q = $this->fill_query_vars($q);
2405 $this->meta_query = new WP_Meta_Query();
2406 $this->meta_query->parse_query_vars( $q );
2408 // Set a flag if a pre_get_posts hook changed the query vars.
2409 $hash = md5( serialize( $this->query_vars ) );
2410 if ( $hash != $this->query_vars_hash ) {
2411 $this->query_vars_changed = true;
2412 $this->query_vars_hash = $hash;
2416 // First let's clear some variables
2419 $whichmimetype = '';
2425 $post_status_join = false;
2428 if ( isset( $q['caller_get_posts'] ) ) {
2429 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
2430 if ( !isset( $q['ignore_sticky_posts'] ) )
2431 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
2434 if ( !isset( $q['ignore_sticky_posts'] ) )
2435 $q['ignore_sticky_posts'] = false;
2437 if ( !isset($q['suppress_filters']) )
2438 $q['suppress_filters'] = false;
2440 if ( !isset($q['cache_results']) ) {
2441 if ( wp_using_ext_object_cache() )
2442 $q['cache_results'] = false;
2444 $q['cache_results'] = true;
2447 if ( !isset($q['update_post_term_cache']) )
2448 $q['update_post_term_cache'] = true;
2450 if ( !isset($q['update_post_meta_cache']) )
2451 $q['update_post_meta_cache'] = true;
2453 if ( !isset($q['post_type']) ) {
2454 if ( $this->is_search )
2455 $q['post_type'] = 'any';
2457 $q['post_type'] = '';
2459 $post_type = $q['post_type'];
2460 if ( empty( $q['posts_per_page'] ) ) {
2461 $q['posts_per_page'] = get_option( 'posts_per_page' );
2463 if ( isset($q['showposts']) && $q['showposts'] ) {
2464 $q['showposts'] = (int) $q['showposts'];
2465 $q['posts_per_page'] = $q['showposts'];
2467 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
2468 $q['posts_per_page'] = $q['posts_per_archive_page'];
2469 if ( !isset($q['nopaging']) ) {
2470 if ( $q['posts_per_page'] == -1 ) {
2471 $q['nopaging'] = true;
2473 $q['nopaging'] = false;
2477 if ( $this->is_feed ) {
2478 // This overrides posts_per_page.
2479 if ( ! empty( $q['posts_per_rss'] ) ) {
2480 $q['posts_per_page'] = $q['posts_per_rss'];
2482 $q['posts_per_page'] = get_option( 'posts_per_rss' );
2484 $q['nopaging'] = false;
2486 $q['posts_per_page'] = (int) $q['posts_per_page'];
2487 if ( $q['posts_per_page'] < -1 )
2488 $q['posts_per_page'] = abs($q['posts_per_page']);
2489 elseif ( $q['posts_per_page'] == 0 )
2490 $q['posts_per_page'] = 1;
2492 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2493 $q['comments_per_page'] = get_option('comments_per_page');
2495 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2496 $this->is_page = true;
2497 $this->is_home = false;
2498 $q['page_id'] = get_option('page_on_front');
2501 if ( isset($q['page']) ) {
2502 $q['page'] = trim($q['page'], '/');
2503 $q['page'] = absint($q['page']);
2506 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2507 if ( isset($q['no_found_rows']) )
2508 $q['no_found_rows'] = (bool) $q['no_found_rows'];
2510 $q['no_found_rows'] = false;
2512 switch ( $q['fields'] ) {
2514 $fields = "$wpdb->posts.ID";
2517 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2520 $fields = "$wpdb->posts.*";
2523 if ( '' !== $q['menu_order'] )
2524 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
2526 // The "m" parameter is meant for months but accepts datetimes of varying specificity
2528 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2529 if ( strlen($q['m']) > 5 )
2530 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2531 if ( strlen($q['m']) > 7 )
2532 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2533 if ( strlen($q['m']) > 9 )
2534 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2535 if ( strlen($q['m']) > 11 )
2536 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2537 if ( strlen($q['m']) > 13 )
2538 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2541 // Handle the other individual date parameters
2542 $date_parameters = array();
2544 if ( '' !== $q['hour'] )
2545 $date_parameters['hour'] = $q['hour'];
2547 if ( '' !== $q['minute'] )
2548 $date_parameters['minute'] = $q['minute'];
2550 if ( '' !== $q['second'] )
2551 $date_parameters['second'] = $q['second'];
2554 $date_parameters['year'] = $q['year'];
2556 if ( $q['monthnum'] )
2557 $date_parameters['monthnum'] = $q['monthnum'];
2560 $date_parameters['week'] = $q['w'];
2563 $date_parameters['day'] = $q['day'];
2565 if ( $date_parameters ) {
2566 $date_query = new WP_Date_Query( array( $date_parameters ) );
2567 $where .= $date_query->get_sql();
2569 unset( $date_parameters, $date_query );
2571 // Handle complex date queries
2572 if ( ! empty( $q['date_query'] ) ) {
2573 $this->date_query = new WP_Date_Query( $q['date_query'] );
2574 $where .= $this->date_query->get_sql();
2578 // If we've got a post_type AND it's not "any" post_type.
2579 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2580 foreach ( (array)$q['post_type'] as $_post_type ) {
2581 $ptype_obj = get_post_type_object($_post_type);
2582 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2585 if ( ! $ptype_obj->hierarchical ) {
2586 // Non-hierarchical post types can directly use 'name'.
2587 $q['name'] = $q[ $ptype_obj->query_var ];
2589 // Hierarchical post types will operate through 'pagename'.
2590 $q['pagename'] = $q[ $ptype_obj->query_var ];
2594 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2600 if ( '' != $q['name'] ) {
2601 $q['name'] = sanitize_title_for_query( $q['name'] );
2602 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2603 } elseif ( '' != $q['pagename'] ) {
2604 if ( isset($this->queried_object_id) ) {
2605 $reqpage = $this->queried_object_id;
2607 if ( 'page' != $q['post_type'] ) {
2608 foreach ( (array)$q['post_type'] as $_post_type ) {
2609 $ptype_obj = get_post_type_object($_post_type);
2610 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2613 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2619 $reqpage = get_page_by_path($q['pagename']);
2621 if ( !empty($reqpage) )
2622 $reqpage = $reqpage->ID;
2627 $page_for_posts = get_option('page_for_posts');
2628 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2629 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2630 $q['name'] = $q['pagename'];
2631 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2632 $reqpage_obj = get_post( $reqpage );
2633 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2634 $this->is_attachment = true;
2635 $post_type = $q['post_type'] = 'attachment';
2636 $this->is_page = true;
2637 $q['attachment_id'] = $reqpage;
2640 } elseif ( '' != $q['attachment'] ) {
2641 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2642 $q['name'] = $q['attachment'];
2643 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2647 if ( intval($q['comments_popup']) )
2648 $q['p'] = absint($q['comments_popup']);
2650 // If an attachment is requested by number, let it supersede any post number.
2651 if ( $q['attachment_id'] )
2652 $q['p'] = absint($q['attachment_id']);
2654 // If a post number is specified, load that post
2656 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2657 } elseif ( $q['post__in'] ) {
2658 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2659 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2660 } elseif ( $q['post__not_in'] ) {
2661 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2662 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2665 if ( is_numeric( $q['post_parent'] ) ) {
2666 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2667 } elseif ( $q['post_parent__in'] ) {
2668 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
2669 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
2670 } elseif ( $q['post_parent__not_in'] ) {
2671 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
2672 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
2675 if ( $q['page_id'] ) {
2676 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2677 $q['p'] = $q['page_id'];
2678 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2682 // If a search pattern is specified, load the posts that match.
2683 if ( ! empty( $q['s'] ) ) {
2684 $search = $this->parse_search( $q );
2688 * Filter the search SQL that is used in the WHERE clause of WP_Query.
2692 * @param string $search Search SQL for WHERE clause.
2693 * @param WP_Query $this The current WP_Query object.
2695 $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
2698 if ( !$this->is_singular ) {
2699 $this->parse_tax_query( $q );
2701 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2703 $join .= $clauses['join'];
2704 $where .= $clauses['where'];
2707 if ( $this->is_tax ) {
2708 if ( empty($post_type) ) {
2709 // Do a fully inclusive search for currently registered post types of queried taxonomies
2710 $post_type = array();
2711 $taxonomies = array_keys( $this->tax_query->queried_terms );
2712 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
2713 $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
2714 if ( array_intersect( $taxonomies, $object_taxonomies ) )
2719 elseif ( count( $post_type ) == 1 )
2720 $post_type = $post_type[0];
2722 $post_status_join = true;
2723 } elseif ( in_array('attachment', (array) $post_type) ) {
2724 $post_status_join = true;
2729 * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
2730 * 'category_name' vars are set for backward compatibility.
2732 if ( ! empty( $this->tax_query->queried_terms ) ) {
2735 * Set 'taxonomy', 'term', and 'term_id' to the
2736 * first taxonomy other than 'post_tag' or 'category'.
2738 if ( ! isset( $q['taxonomy'] ) ) {
2739 foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
2740 if ( empty( $queried_items['terms'][0] ) ) {
2744 if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
2745 $q['taxonomy'] = $queried_taxonomy;
2747 if ( 'slug' === $queried_items['field'] ) {
2748 $q['term'] = $queried_items['terms'][0];
2750 $q['term_id'] = $queried_items['terms'][0];
2756 // 'cat', 'category_name', 'tag_id'
2757 foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
2758 if ( empty( $queried_items['terms'][0] ) ) {
2762 if ( 'category' === $queried_taxonomy ) {
2763 $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' );
2765 $this->set( 'cat', $the_cat->term_id );
2766 $this->set( 'category_name', $the_cat->slug );
2771 if ( 'post_tag' === $queried_taxonomy ) {
2772 $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' );
2774 $this->set( 'tag_id', $the_tag->term_id );
2781 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2782 $groupby = "{$wpdb->posts}.ID";
2785 // Author/user stuff
2787 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
2788 $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
2789 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
2790 foreach ( $authors as $author ) {
2791 $key = $author > 0 ? 'author__in' : 'author__not_in';
2792 $q[$key][] = abs( $author );
2794 $q['author'] = implode( ',', $authors );
2797 if ( ! empty( $q['author__not_in'] ) ) {
2798 $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
2799 $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
2800 } elseif ( ! empty( $q['author__in'] ) ) {
2801 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
2802 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
2805 // Author stuff for nice URLs
2807 if ( '' != $q['author_name'] ) {
2808 if ( strpos($q['author_name'], '/') !== false ) {
2809 $q['author_name'] = explode('/', $q['author_name']);
2810 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2811 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2813 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2816 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2817 $q['author'] = get_user_by('slug', $q['author_name']);
2819 $q['author'] = $q['author']->ID;
2820 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2823 // MIME-Type stuff for attachment browsing
2825 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2826 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2828 $where .= $search . $whichauthor . $whichmimetype;
2830 if ( ! empty( $this->meta_query->queries ) ) {
2831 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2832 $join .= $clauses['join'];
2833 $where .= $clauses['where'];
2836 $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
2837 if ( ! isset( $q['order'] ) ) {
2838 $q['order'] = $rand ? '' : 'DESC';
2840 $q['order'] = $rand ? '' : $this->parse_order( $q['order'] );
2844 if ( empty( $q['orderby'] ) ) {
2846 * Boolean false or empty array blanks out ORDER BY,
2847 * while leaving the value unset or otherwise empty sets the default.
2849 if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) {
2852 $orderby = "$wpdb->posts.post_date " . $q['order'];
2854 } elseif ( 'none' == $q['orderby'] ) {
2856 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
2857 $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
2858 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
2859 $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
2861 $orderby_array = array();
2862 if ( is_array( $q['orderby'] ) ) {
2863 foreach ( $q['orderby'] as $_orderby => $order ) {
2864 $orderby = addslashes_gpc( urldecode( $_orderby ) );
2865 $parsed = $this->parse_orderby( $orderby );
2871 $orderby_array[] = $parsed . ' ' . $this->parse_order( $order );
2873 $orderby = implode( ', ', $orderby_array );
2876 $q['orderby'] = urldecode( $q['orderby'] );
2877 $q['orderby'] = addslashes_gpc( $q['orderby'] );
2879 foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2880 $parsed = $this->parse_orderby( $orderby );
2881 // Only allow certain values for safety.
2886 $orderby_array[] = $parsed;
2888 $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
2890 if ( empty( $orderby ) ) {
2891 $orderby = "$wpdb->posts.post_date " . $q['order'];
2892 } elseif ( ! empty( $q['order'] ) ) {
2893 $orderby .= " {$q['order']}";
2898 // Order search results by relevance only when another "orderby" is not specified in the query.
2899 if ( ! empty( $q['s'] ) ) {
2900 $search_orderby = '';
2901 if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
2902 $search_orderby = $this->parse_search_order( $q );
2905 * Filter the ORDER BY used when ordering search results.
2909 * @param string $search_orderby The ORDER BY clause.
2910 * @param WP_Query $this The current WP_Query instance.
2912 $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
2913 if ( $search_orderby )
2914 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
2917 if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
2918 $post_type_cap = 'multiple_post_type';
2920 if ( is_array( $post_type ) )
2921 $post_type = reset( $post_type );
2922 $post_type_object = get_post_type_object( $post_type );
2923 if ( empty( $post_type_object ) )
2924 $post_type_cap = $post_type;
2927 if ( isset( $q['post_password'] ) ) {
2928 $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
2929 if ( empty( $q['perm'] ) ) {
2930 $q['perm'] = 'readable';
2932 } elseif ( isset( $q['has_password'] ) ) {
2933 $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
2936 if ( 'any' == $post_type ) {
2937 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2938 if ( empty( $in_search_post_types ) )
2939 $where .= ' AND 1=0 ';
2941 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
2942 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2943 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2944 } elseif ( ! empty( $post_type ) ) {
2945 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2946 $post_type_object = get_post_type_object ( $post_type );
2947 } elseif ( $this->is_attachment ) {
2948 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2949 $post_type_object = get_post_type_object ( 'attachment' );
2950 } elseif ( $this->is_page ) {
2951 $where .= " AND $wpdb->posts.post_type = 'page'";
2952 $post_type_object = get_post_type_object ( 'page' );
2954 $where .= " AND $wpdb->posts.post_type = 'post'";
2955 $post_type_object = get_post_type_object ( 'post' );
2958 $edit_cap = 'edit_post';
2959 $read_cap = 'read_post';
2961 if ( ! empty( $post_type_object ) ) {
2962 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2963 $read_private_cap = $post_type_object->cap->read_private_posts;
2965 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2966 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2969 $user_id = get_current_user_id();
2971 $q_status = array();
2972 if ( ! empty( $q['post_status'] ) ) {
2973 $statuswheres = array();
2974 $q_status = $q['post_status'];
2975 if ( ! is_array( $q_status ) )
2976 $q_status = explode(',', $q_status);
2977 $r_status = array();
2978 $p_status = array();
2979 $e_status = array();
2980 if ( in_array( 'any', $q_status ) ) {
2981 foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) {
2982 if ( ! in_array( $status, $q_status ) ) {
2983 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2987 foreach ( get_post_stati() as $status ) {
2988 if ( in_array( $status, $q_status ) ) {
2989 if ( 'private' == $status )
2990 $p_status[] = "$wpdb->posts.post_status = '$status'";
2992 $r_status[] = "$wpdb->posts.post_status = '$status'";
2997 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2998 $r_status = array_merge($r_status, $p_status);
3002 if ( !empty($e_status) ) {
3003 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
3005 if ( !empty($r_status) ) {
3006 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
3007 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
3009 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
3011 if ( !empty($p_status) ) {
3012 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
3013 $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
3015 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
3017 if ( $post_status_join ) {
3018 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
3019 foreach ( $statuswheres as $index => $statuswhere )
3020 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
3022 $where_status = implode( ' OR ', $statuswheres );
3023 if ( ! empty( $where_status ) ) {
3024 $where .= " AND ($where_status)";
3026 } elseif ( !$this->is_singular ) {
3027 $where .= " AND ($wpdb->posts.post_status = 'publish'";
3029 // Add public states.
3030 $public_states = get_post_stati( array('public' => true) );
3031 foreach ( (array) $public_states as $state ) {
3032 if ( 'publish' == $state ) // Publish is hard-coded above.
3034 $where .= " OR $wpdb->posts.post_status = '$state'";
3037 if ( $this->is_admin ) {
3038 // Add protected states that should show in the admin all list.
3039 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
3040 foreach ( (array) $admin_all_states as $state )
3041 $where .= " OR $wpdb->posts.post_status = '$state'";
3044 if ( is_user_logged_in() ) {
3045 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
3046 $private_states = get_post_stati( array('private' => true) );
3047 foreach ( (array) $private_states as $state )
3048 $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_id AND $wpdb->posts.post_status = '$state'";
3055 * Apply filters on where and join prior to paging so that any
3056 * manipulations to them are reflected in the paging by day queries.
3058 if ( !$q['suppress_filters'] ) {
3060 * Filter the WHERE clause of the query.
3064 * @param string $where The WHERE clause of the query.
3065 * @param WP_Query &$this The WP_Query instance (passed by reference).
3067 $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) );
3070 * Filter the JOIN clause of the query.
3074 * @param string $where The JOIN clause of the query.
3075 * @param WP_Query &$this The WP_Query instance (passed by reference).
3077 $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) );
3081 if ( empty($q['nopaging']) && !$this->is_singular ) {
3082 $page = absint($q['paged']);
3086 if ( empty($q['offset']) ) {
3087 $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', ';
3088 } else { // we're ignoring $page and using 'offset'
3089 $q['offset'] = absint($q['offset']);
3090 $pgstrt = $q['offset'] . ', ';
3092 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
3096 if ( $this->is_comment_feed && ! $this->is_singular ) {
3097 if ( $this->is_archive || $this->is_search ) {
3098 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
3099 $cwhere = "WHERE comment_approved = '1' $where";
3100 $cgroupby = "$wpdb->comments.comment_id";
3101 } else { // Other non singular e.g. front
3102 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
3103 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
3107 if ( !$q['suppress_filters'] ) {
3109 * Filter the JOIN clause of the comments feed query before sending.
3113 * @param string $cjoin The JOIN clause of the query.
3114 * @param WP_Query &$this The WP_Query instance (passed by reference).
3116 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) );
3119 * Filter the WHERE clause of the comments feed query before sending.
3123 * @param string $cwhere The WHERE clause of the query.
3124 * @param WP_Query &$this The WP_Query instance (passed by reference).
3126 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) );
3129 * Filter the GROUP BY clause of the comments feed query before sending.
3133 * @param string $cgroupby The GROUP BY clause of the query.
3134 * @param WP_Query &$this The WP_Query instance (passed by reference).
3136 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) );
3139 * Filter the ORDER BY clause of the comments feed query before sending.
3143 * @param string $corderby The ORDER BY clause of the query.
3144 * @param WP_Query &$this The WP_Query instance (passed by reference).
3146 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
3149 * Filter the LIMIT clause of the comments feed query before sending.
3153 * @param string $climits The JOIN clause of the query.
3154 * @param WP_Query &$this The WP_Query instance (passed by reference).
3156 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
3158 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
3159 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
3161 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
3162 $this->comment_count = count($this->comments);
3164 $post_ids = array();
3166 foreach ( $this->comments as $comment )
3167 $post_ids[] = (int) $comment->comment_post_ID;
3169 $post_ids = join(',', $post_ids);
3172 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
3177 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
3180 * Apply post-paging filters on where and join. Only plugins that
3181 * manipulate paging queries should use these hooks.
3183 if ( !$q['suppress_filters'] ) {
3185 * Filter the WHERE clause of the query.
3187 * Specifically for manipulating paging queries.
3191 * @param string $where The WHERE clause of the query.
3192 * @param WP_Query &$this The WP_Query instance (passed by reference).
3194 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
3197 * Filter the GROUP BY clause of the query.
3201 * @param string $groupby The GROUP BY clause of the query.
3202 * @param WP_Query &$this The WP_Query instance (passed by reference).
3204 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
3207 * Filter the JOIN clause of the query.
3209 * Specifically for manipulating paging queries.
3213 * @param string $join The JOIN clause of the query.
3214 * @param WP_Query &$this The WP_Query instance (passed by reference).
3216 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
3219 * Filter the ORDER BY clause of the query.
3223 * @param string $orderby The ORDER BY clause of the query.
3224 * @param WP_Query &$this The WP_Query instance (passed by reference).
3226 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
3229 * Filter the DISTINCT clause of the query.
3233 * @param string $distinct The DISTINCT clause of the query.
3234 * @param WP_Query &$this The WP_Query instance (passed by reference).
3236 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
3239 * Filter the LIMIT clause of the query.
3243 * @param string $limits The LIMIT clause of the query.
3244 * @param WP_Query &$this The WP_Query instance (passed by reference).
3246 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
3249 * Filter the SELECT clause of the query.
3253 * @param string $fields The SELECT clause of the query.
3254 * @param WP_Query &$this The WP_Query instance (passed by reference).
3256 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
3259 * Filter all query clauses at once, for convenience.
3261 * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
3262 * fields (SELECT), and LIMITS clauses.
3266 * @param array $clauses The list of clauses for the query.
3267 * @param WP_Query &$this The WP_Query instance (passed by reference).
3269 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
3271 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
3272 $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
3273 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
3274 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
3275 $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
3276 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
3277 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
3281 * Fires to announce the query's current selection parameters.
3283 * For use by caching plugins.
3287 * @param string $selection The assembled selection query.
3289 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
3292 * Filter again for the benefit of caching plugins.
3293 * Regular plugins should use the hooks above.
3295 if ( !$q['suppress_filters'] ) {
3297 * Filter the WHERE clause of the query.
3299 * For use by caching plugins.
3303 * @param string $where The WHERE clause of the query.
3304 * @param WP_Query &$this The WP_Query instance (passed by reference).
3306 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
3309 * Filter the GROUP BY clause of the query.
3311 * For use by caching plugins.
3315 * @param string $groupby The GROUP BY clause of the query.
3316 * @param WP_Query &$this The WP_Query instance (passed by reference).
3318 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
3321 * Filter the JOIN clause of the query.
3323 * For use by caching plugins.
3327 * @param string $join The JOIN clause of the query.
3328 * @param WP_Query &$this The WP_Query instance (passed by reference).
3330 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
3333 * Filter the ORDER BY clause of the query.
3335 * For use by caching plugins.
3339 * @param string $orderby The ORDER BY clause of the query.
3340 * @param WP_Query &$this The WP_Query instance (passed by reference).
3342 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
3345 * Filter the DISTINCT clause of the query.
3347 * For use by caching plugins.
3351 * @param string $distinct The DISTINCT clause of the query.
3352 * @param WP_Query &$this The WP_Query instance (passed by reference).
3354 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
3357 * Filter the SELECT clause of the query.
3359 * For use by caching plugins.
3363 * @param string $fields The SELECT clause of the query.
3364 * @param WP_Query &$this The WP_Query instance (passed by reference).
3366 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
3369 * Filter the LIMIT clause of the query.
3371 * For use by caching plugins.
3375 * @param string $limits The LIMIT clause of the query.
3376 * @param WP_Query &$this The WP_Query instance (passed by reference).
3378 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
3381 * Filter all query clauses at once, for convenience.
3383 * For use by caching plugins.
3385 * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
3386 * fields (SELECT), and LIMITS clauses.
3390 * @param array $pieces The pieces of the query.
3391 * @param WP_Query &$this The WP_Query instance (passed by reference).
3393 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
3395 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
3396 $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
3397 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
3398 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
3399 $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
3400 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
3401 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
3404 if ( ! empty($groupby) )
3405 $groupby = 'GROUP BY ' . $groupby;
3406 if ( !empty( $orderby ) )
3407 $orderby = 'ORDER BY ' . $orderby;
3410 if ( !$q['no_found_rows'] && !empty($limits) )
3411 $found_rows = 'SQL_CALC_FOUND_ROWS';
3413 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
3415 if ( !$q['suppress_filters'] ) {
3417 * Filter the completed SQL query before sending.
3421 * @param array $request The complete SQL query.
3422 * @param WP_Query &$this The WP_Query instance (passed by reference).
3424 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
3427 if ( 'ids' == $q['fields'] ) {
3428 $this->posts = $wpdb->get_col( $this->request );
3429 $this->posts = array_map( 'intval', $this->posts );
3430 $this->post_count = count( $this->posts );
3431 $this->set_found_posts( $q, $limits );
3433 return $this->posts;
3436 if ( 'id=>parent' == $q['fields'] ) {
3437 $this->posts = $wpdb->get_results( $this->request );
3438 $this->post_count = count( $this->posts );
3439 $this->set_found_posts( $q, $limits );
3442 foreach ( $this->posts as $key => $post ) {
3443 $this->posts[ $key ]->ID = (int) $post->ID;
3444 $this->posts[ $key ]->post_parent = (int) $post->post_parent;
3446 $r[ (int) $post->ID ] = (int) $post->post_parent;
3452 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
3455 * Filter whether to split the query.
3457 * Splitting the query will cause it to fetch just the IDs of the found posts
3458 * (and then individually fetch each post by ID), rather than fetching every
3459 * complete row at once. One massive result vs. many small results.
3463 * @param bool $split_the_query Whether or not to split the query.
3464 * @param WP_Query $this The WP_Query instance.
3466 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
3468 if ( $split_the_query ) {
3469 // First get the IDs and then fill in the objects
3471 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
3474 * Filter the Post IDs SQL request before sending.
3478 * @param string $request The post ID request.
3479 * @param WP_Query $this The WP_Query instance.
3481 $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
3483 $ids = $wpdb->get_col( $this->request );
3486 $this->posts = $ids;
3487 $this->set_found_posts( $q, $limits );
3488 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
3490 $this->posts = array();
3493 $this->posts = $wpdb->get_results( $this->request );
3494 $this->set_found_posts( $q, $limits );
3497 // Convert to WP_Post objects
3499 $this->posts = array_map( 'get_post', $this->posts );
3501 if ( ! $q['suppress_filters'] ) {
3503 * Filter the raw post results array, prior to status checks.
3507 * @param array $posts The post results array.
3508 * @param WP_Query &$this The WP_Query instance (passed by reference).
3510 $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) );
3513 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
3514 /** This filter is documented in wp-includes/query.php */
3515 $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) );
3517 /** This filter is documented in wp-includes/query.php */
3518 $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
3520 /** This filter is documented in wp-includes/query.php */
3521 $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) );
3522 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
3524 /** This filter is documented in wp-includes/query.php */
3525 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
3526 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
3528 /** This filter is documented in wp-includes/query.php */
3529 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
3531 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
3532 $this->comments = $wpdb->get_results($comments_request);
3533 $this->comment_count = count($this->comments);
3536 // Check post status to determine if post should be displayed.
3537 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
3538 $status = get_post_status($this->posts[0]);
3539 $post_status_obj = get_post_status_object($status);
3540 //$type = get_post_type($this->posts[0]);
3542 // If the post_status was specifically requested, let it pass through.
3543 if ( !$post_status_obj->public && ! in_array( $status, $q_status ) ) {
3545 if ( ! is_user_logged_in() ) {
3546 // User must be logged in to view unpublished posts.
3547 $this->posts = array();
3549 if ( $post_status_obj->protected ) {
3550 // User must have edit permissions on the draft to preview.
3551 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
3552 $this->posts = array();
3554 $this->is_preview = true;
3555 if ( 'future' != $status )
3556 $this->posts[0]->post_date = current_time('mysql');
3558 } elseif ( $post_status_obj->private ) {
3559 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
3560 $this->posts = array();
3562 $this->posts = array();
3567 if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) {
3569 * Filter the single post for preview mode.
3573 * @param WP_Post $post_preview The Post object.
3574 * @param WP_Query &$this The WP_Query instance (passed by reference).
3576 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
3580 // Put sticky posts at the top of the posts array
3581 $sticky_posts = get_option('sticky_posts');
3582 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
3583 $num_posts = count($this->posts);
3585 // Loop over posts and relocate stickies to the front.
3586 for ( $i = 0; $i < $num_posts; $i++ ) {
3587 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
3588 $sticky_post = $this->posts[$i];
3589 // Remove sticky from current position
3590 array_splice($this->posts, $i, 1);
3591 // Move to front, after other stickies
3592 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
3593 // Increment the sticky offset. The next sticky will be placed at this offset.
3595 // Remove post from sticky posts array
3596 $offset = array_search($sticky_post->ID, $sticky_posts);
3597 unset( $sticky_posts[$offset] );
3601 // If any posts have been excluded specifically, Ignore those that are sticky.
3602 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
3603 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
3605 // Fetch sticky posts that weren't in the query results
3606 if ( !empty($sticky_posts) ) {
3607 $stickies = get_posts( array(
3608 'post__in' => $sticky_posts,
3609 'post_type' => $post_type,
3610 'post_status' => 'publish',
3614 foreach ( $stickies as $sticky_post ) {
3615 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
3621 if ( ! $q['suppress_filters'] ) {
3623 * Filter the array of retrieved posts after they've been fetched and
3624 * internally processed.
3628 * @param array $posts The array of retrieved posts.
3629 * @param WP_Query &$this The WP_Query instance (passed by reference).
3631 $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) );
3634 // Ensure that any posts added/modified via one of the filters above are
3635 // of the type WP_Post and are filtered.
3636 if ( $this->posts ) {
3637 $this->post_count = count( $this->posts );
3639 $this->posts = array_map( 'get_post', $this->posts );
3641 if ( $q['cache_results'] )
3642 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
3644 $this->post = reset( $this->posts );
3646 $this->post_count = 0;
3647 $this->posts = array();
3650 return $this->posts;
3654 * Set up the amount of found posts and the number of pages (if limit clause was used)
3655 * for the current query.
3660 private function set_found_posts( $q, $limits ) {
3663 // Bail if posts is an empty array. Continue if posts is an empty string,
3664 // null, or false to accommodate caching plugins that fill posts later.
3665 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
3668 if ( ! empty( $limits ) ) {
3670 * Filter the query to run for retrieving the found posts.
3674 * @param string $found_posts The query to run to find the found posts.
3675 * @param WP_Query &$this The WP_Query instance (passed by reference).
3677 $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
3679 $this->found_posts = count( $this->posts );
3683 * Filter the number of found posts for the query.
3687 * @param int $found_posts The number of posts found.
3688 * @param WP_Query &$this The WP_Query instance (passed by reference).
3690 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
3692 if ( ! empty( $limits ) )
3693 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
3697 * Set up the next post and iterate current post index.
3702 * @return WP_Post Next post.
3704 public function next_post() {
3706 $this->current_post++;
3708 $this->post = $this->posts[$this->current_post];
3713 * Sets up the current post.
3715 * Retrieves the next post, sets up the post, sets the 'in the loop'
3721 public function the_post() {
3723 $this->in_the_loop = true;
3725 if ( $this->current_post == -1 ) // loop has just started
3727 * Fires once the loop is started.
3731 * @param WP_Query &$this The WP_Query instance (passed by reference).
3733 do_action_ref_array( 'loop_start', array( &$this ) );
3735 $post = $this->next_post();
3736 $this->setup_postdata( $post );
3740 * Whether there are more posts available in the loop.
3742 * Calls action 'loop_end', when the loop is complete.
3747 * @return bool True if posts are available, false if end of loop.
3749 public function have_posts() {
3750 if ( $this->current_post + 1 < $this->post_count ) {
3752 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
3754 * Fires once the loop has ended.
3758 * @param WP_Query &$this The WP_Query instance (passed by reference).
3760 do_action_ref_array( 'loop_end', array( &$this ) );
3761 // Do some cleaning up after the loop
3762 $this->rewind_posts();
3765 $this->in_the_loop = false;
3770 * Rewind the posts and reset post index.
3775 public function rewind_posts() {
3776 $this->current_post = -1;
3777 if ( $this->post_count > 0 ) {
3778 $this->post = $this->posts[0];
3783 * Iterate current comment index and return comment object.
3788 * @return object Comment object.
3790 public function next_comment() {
3791 $this->current_comment++;
3793 $this->comment = $this->comments[$this->current_comment];
3794 return $this->comment;
3798 * Sets up the current comment.
3802 * @global object $comment Current comment.
3804 public function the_comment() {
3807 $comment = $this->next_comment();
3809 if ( $this->current_comment == 0 ) {
3811 * Fires once the comment loop is started.
3815 do_action( 'comment_loop_start' );
3820 * Whether there are more comments available.
3822 * Automatically rewinds comments when finished.
3827 * @return bool True, if more comments. False, if no more posts.
3829 public function have_comments() {
3830 if ( $this->current_comment + 1 < $this->comment_count ) {
3832 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
3833 $this->rewind_comments();
3840 * Rewind the comments, resets the comment index and comment to first.
3845 public function rewind_comments() {
3846 $this->current_comment = -1;
3847 if ( $this->comment_count > 0 ) {
3848 $this->comment = $this->comments[0];
3853 * Sets up the WordPress query by parsing query string.
3858 * @param string $query URL query string.
3859 * @return array List of posts.
3861 public function query( $query ) {
3863 $this->query = $this->query_vars = wp_parse_args( $query );
3864 return $this->get_posts();
3868 * Retrieve queried object.
3870 * If queried object is not set, then the queried object will be set from
3871 * the category, tag, taxonomy, posts page, single post, page, or author
3872 * query variable. After it is set up, it will be returned.
3879 public function get_queried_object() {
3880 if ( isset($this->queried_object) )
3881 return $this->queried_object;
3883 $this->queried_object = null;
3884 $this->queried_object_id = 0;
3886 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
3887 if ( $this->is_category ) {
3888 if ( $this->get( 'cat' ) ) {
3889 $term = get_term( $this->get( 'cat' ), 'category' );
3890 } elseif ( $this->get( 'category_name' ) ) {
3891 $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
3893 } elseif ( $this->is_tag ) {
3894 if ( $this->get( 'tag_id' ) ) {
3895 $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
3896 } elseif ( $this->get( 'tag' ) ) {
3897 $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
3900 // For other tax queries, grab the first term from the first clause.
3901 $tax_query_in_and = wp_list_filter( $this->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' );
3903 if ( ! empty( $tax_query_in_and ) ) {
3904 $queried_taxonomies = array_keys( $tax_query_in_and );
3905 $matched_taxonomy = reset( $queried_taxonomies );
3906 $query = $tax_query_in_and[ $matched_taxonomy ];
3908 if ( $query['terms'] ) {
3909 if ( 'term_id' == $query['field'] ) {
3910 $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
3912 $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
3918 if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
3919 $this->queried_object = $term;
3920 $this->queried_object_id = (int) $term->term_id;
3922 if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
3923 _make_cat_compat( $this->queried_object );
3925 } elseif ( $this->is_post_type_archive ) {
3926 $post_type = $this->get( 'post_type' );
3927 if ( is_array( $post_type ) )
3928 $post_type = reset( $post_type );
3929 $this->queried_object = get_post_type_object( $post_type );
3930 } elseif ( $this->is_posts_page ) {
3931 $page_for_posts = get_option('page_for_posts');
3932 $this->queried_object = get_post( $page_for_posts );
3933 $this->queried_object_id = (int) $this->queried_object->ID;
3934 } elseif ( $this->is_singular && ! empty( $this->post ) ) {
3935 $this->queried_object = $this->post;
3936 $this->queried_object_id = (int) $this->post->ID;
3937 } elseif ( $this->is_author ) {
3938 $this->queried_object_id = (int) $this->get('author');
3939 $this->queried_object = get_userdata( $this->queried_object_id );
3942 return $this->queried_object;
3946 * Retrieve ID of the current queried object.
3953 public function get_queried_object_id() {
3954 $this->get_queried_object();
3956 if ( isset($this->queried_object_id) ) {
3957 return $this->queried_object_id;
3966 * Sets up the WordPress query, if parameter is not empty.
3971 * @param string|array $query URL query string or array of vars.
3973 public function __construct($query = '') {
3974 if ( ! empty($query) ) {
3975 $this->query($query);
3980 * Make private properties readable for backwards compatibility.
3985 * @param string $name Property to get.
3986 * @return mixed Property.
3988 public function __get( $name ) {
3989 if ( in_array( $name, $this->compat_fields ) ) {
3990 return $this->$name;
3995 * Make private properties checkable for backwards compatibility.
4000 * @param string $name Property to check if set.
4001 * @return bool Whether the property is set.
4003 public function __isset( $name ) {
4004 if ( in_array( $name, $this->compat_fields ) ) {
4005 return isset( $this->$name );
4010 * Make private/protected methods readable for backwards compatibility.
4015 * @param callable $name Method to call.
4016 * @param array $arguments Arguments to pass when calling.
4017 * @return mixed|bool Return value of the callback, false otherwise.
4019 public function __call( $name, $arguments ) {
4020 if ( in_array( $name, $this->compat_methods ) ) {
4021 return call_user_func_array( array( $this, $name ), $arguments );
4027 * Is the query for an existing archive page?
4029 * Month, Year, Category, Author, Post Type archive...
4035 public function is_archive() {
4036 return (bool) $this->is_archive;
4040 * Is the query for an existing post type archive page?
4044 * @param mixed $post_types Optional. Post type or array of posts types to check against.
4047 public function is_post_type_archive( $post_types = '' ) {
4048 if ( empty( $post_types ) || ! $this->is_post_type_archive )
4049 return (bool) $this->is_post_type_archive;
4051 $post_type = $this->get( 'post_type' );
4052 if ( is_array( $post_type ) )
4053 $post_type = reset( $post_type );
4054 $post_type_object = get_post_type_object( $post_type );
4056 return in_array( $post_type_object->name, (array) $post_types );
4060 * Is the query for an existing attachment page?
4064 * @param mixed $attachment Attachment ID, title, slug, or array of such.
4067 public function is_attachment( $attachment = '' ) {
4068 if ( ! $this->is_attachment ) {
4072 if ( empty( $attachment ) ) {
4076 $attachment = (array) $attachment;
4078 $post_obj = $this->get_queried_object();
4080 if ( in_array( (string) $post_obj->ID, $attachment ) ) {
4082 } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
4084 } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
4091 * Is the query for an existing author archive page?
4093 * If the $author parameter is specified, this function will additionally
4094 * check if the query is for one of the authors specified.
4098 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
4101 public function is_author( $author = '' ) {
4102 if ( !$this->is_author )
4105 if ( empty($author) )
4108 $author_obj = $this->get_queried_object();
4110 $author = (array) $author;
4112 if ( in_array( (string) $author_obj->ID, $author ) )
4114 elseif ( in_array( $author_obj->nickname, $author ) )
4116 elseif ( in_array( $author_obj->user_nicename, $author ) )
4123 * Is the query for an existing category archive page?
4125 * If the $category parameter is specified, this function will additionally
4126 * check if the query is for one of the categories specified.
4130 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
4133 public function is_category( $category = '' ) {
4134 if ( !$this->is_category )
4137 if ( empty($category) )
4140 $cat_obj = $this->get_queried_object();
4142 $category = (array) $category;
4144 if ( in_array( (string) $cat_obj->term_id, $category ) )
4146 elseif ( in_array( $cat_obj->name, $category ) )
4148 elseif ( in_array( $cat_obj->slug, $category ) )
4155 * Is the query for an existing tag archive page?
4157 * If the $tag parameter is specified, this function will additionally
4158 * check if the query is for one of the tags specified.
4162 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
4165 public function is_tag( $tag = '' ) {
4166 if ( ! $this->is_tag )
4169 if ( empty( $tag ) )
4172 $tag_obj = $this->get_queried_object();
4174 $tag = (array) $tag;
4176 if ( in_array( (string) $tag_obj->term_id, $tag ) )
4178 elseif ( in_array( $tag_obj->name, $tag ) )
4180 elseif ( in_array( $tag_obj->slug, $tag ) )
4187 * Is the query for an existing taxonomy archive page?
4189 * If the $taxonomy parameter is specified, this function will additionally
4190 * check if the query is for that specific $taxonomy.
4192 * If the $term parameter is specified in addition to the $taxonomy parameter,
4193 * this function will additionally check if the query is for one of the terms
4198 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
4199 * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
4202 public function is_tax( $taxonomy = '', $term = '' ) {
4203 global $wp_taxonomies;
4205 if ( !$this->is_tax )
4208 if ( empty( $taxonomy ) )
4211 $queried_object = $this->get_queried_object();
4212 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
4213 $term_array = (array) $term;
4215 // Check that the taxonomy matches.
4216 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
4219 // Only a Taxonomy provided.
4220 if ( empty( $term ) )
4223 return isset( $queried_object->term_id ) &&
4224 count( array_intersect(
4225 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
4231 * Whether the current URL is within the comments popup window.
4237 public function is_comments_popup() {
4238 return (bool) $this->is_comments_popup;
4242 * Is the query for an existing date archive?
4248 public function is_date() {
4249 return (bool) $this->is_date;
4253 * Is the query for an existing day archive?
4259 public function is_day() {
4260 return (bool) $this->is_day;
4264 * Is the query for a feed?
4268 * @param string|array $feeds Optional feed types to check.
4271 public function is_feed( $feeds = '' ) {
4272 if ( empty( $feeds ) || ! $this->is_feed )
4273 return (bool) $this->is_feed;
4274 $qv = $this->get( 'feed' );
4275 if ( 'feed' == $qv )
4276 $qv = get_default_feed();
4277 return in_array( $qv, (array) $feeds );
4281 * Is the query for a comments feed?
4287 public function is_comment_feed() {
4288 return (bool) $this->is_comment_feed;
4292 * Is the query for the front page of the site?
4294 * This is for what is displayed at your site's main URL.
4296 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
4298 * If you set a static page for the front page of your site, this function will return
4299 * true when viewing that page.
4301 * Otherwise the same as @see WP_Query::is_home()
4305 * @return bool True, if front of site.
4307 public function is_front_page() {
4309 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
4311 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
4318 * Is the query for the blog homepage?
4320 * This is the page which shows the time based blog content of your site.
4322 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
4324 * If you set a static page for the front page of your site, this function will return
4325 * true only on the page you set as the "Posts page".
4327 * @see WP_Query::is_front_page()
4331 * @return bool True if blog view homepage.
4333 public function is_home() {
4334 return (bool) $this->is_home;
4338 * Is the query for an existing month archive?
4344 public function is_month() {
4345 return (bool) $this->is_month;
4349 * Is the query for an existing single page?
4351 * If the $page parameter is specified, this function will additionally
4352 * check if the query is for one of the pages specified.
4354 * @see WP_Query::is_single()
4355 * @see WP_Query::is_singular()
4359 * @param mixed $page Page ID, title, slug, path, or array of such.
4362 public function is_page( $page = '' ) {
4363 if ( !$this->is_page )
4366 if ( empty( $page ) )
4369 $page_obj = $this->get_queried_object();
4371 $page = (array) $page;
4373 if ( in_array( (string) $page_obj->ID, $page ) ) {
4375 } elseif ( in_array( $page_obj->post_title, $page ) ) {
4377 } elseif ( in_array( $page_obj->post_name, $page ) ) {
4380 foreach ( $page as $pagepath ) {
4381 if ( ! strpos( $pagepath, '/' ) ) {
4384 $pagepath_obj = get_page_by_path( $pagepath );
4386 if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
4396 * Is the query for paged result and not for the first page?
4402 public function is_paged() {
4403 return (bool) $this->is_paged;
4407 * Is the query for a post or page preview?
4413 public function is_preview() {
4414 return (bool) $this->is_preview;
4418 * Is the query for the robots file?
4424 public function is_robots() {
4425 return (bool) $this->is_robots;
4429 * Is the query for a search?
4435 public function is_search() {
4436 return (bool) $this->is_search;
4440 * Is the query for an existing single post?
4442 * Works for any post type, except attachments and pages
4444 * If the $post parameter is specified, this function will additionally
4445 * check if the query is for one of the Posts specified.
4447 * @see WP_Query::is_page()
4448 * @see WP_Query::is_singular()
4452 * @param mixed $post Post ID, title, slug, path, or array of such.
4455 public function is_single( $post = '' ) {
4456 if ( !$this->is_single )
4462 $post_obj = $this->get_queried_object();
4464 $post = (array) $post;
4466 if ( in_array( (string) $post_obj->ID, $post ) ) {
4468 } elseif ( in_array( $post_obj->post_title, $post ) ) {
4470 } elseif ( in_array( $post_obj->post_name, $post ) ) {
4473 foreach ( $post as $postpath ) {
4474 if ( ! strpos( $postpath, '/' ) ) {
4477 $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );
4479 if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
4488 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
4490 * If the $post_types parameter is specified, this function will additionally
4491 * check if the query is for one of the Posts Types specified.
4493 * @see WP_Query::is_page()
4494 * @see WP_Query::is_single()
4498 * @param mixed $post_types Optional. Post Type or array of Post Types
4501 public function is_singular( $post_types = '' ) {
4502 if ( empty( $post_types ) || !$this->is_singular )
4503 return (bool) $this->is_singular;
4505 $post_obj = $this->get_queried_object();
4507 return in_array( $post_obj->post_type, (array) $post_types );
4511 * Is the query for a specific time?
4517 public function is_time() {
4518 return (bool) $this->is_time;
4522 * Is the query for a trackback endpoint call?
4528 public function is_trackback() {
4529 return (bool) $this->is_trackback;
4533 * Is the query for an existing year archive?
4539 public function is_year() {
4540 return (bool) $this->is_year;
4544 * Is the query a 404 (returns no results)?
4550 public function is_404() {
4551 return (bool) $this->is_404;
4555 * Is the query the main query?
4561 public function is_main_query() {
4562 global $wp_the_query;
4563 return $wp_the_query === $this;
4567 * Set up global post data.
4571 * @param WP_Post $post Post data.
4572 * @return bool True when finished.
4574 public function setup_postdata( $post ) {
4575 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
4577 $id = (int) $post->ID;
4579 $authordata = get_userdata($post->post_author);
4581 $currentday = mysql2date('d.m.y', $post->post_date, false);
4582 $currentmonth = mysql2date('m', $post->post_date, false);
4585 $page = $this->get( 'page' );
4590 * Force full post content when viewing the permalink for the $post,
4591 * or when on an RSS feed. Otherwise respect the 'more' tag.
4593 if ( $post->ID === get_queried_object_id() && ( $this->is_page() || $this->is_single() ) ) {
4595 } elseif ( $this->is_feed() ) {
4601 $content = $post->post_content;
4602 if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
4605 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
4606 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
4607 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
4609 // Ignore nextpage at the beginning of the content.
4610 if ( 0 === strpos( $content, '<!--nextpage-->' ) )
4611 $content = substr( $content, 15 );
4613 $pages = explode('<!--nextpage-->', $content);
4614 $numpages = count($pages);
4615 if ( $numpages > 1 )
4618 $pages = array( $post->post_content );
4622 * Fires once the post data has been setup.
4625 * @since 4.1.0 Introduced `$this` parameter.
4627 * @param WP_Post &$post The Post object (passed by reference).
4628 * @param WP_Query &$this The current Query object (passed by reference).
4630 do_action_ref_array( 'the_post', array( &$post, &$this ) );
4635 * After looping through a nested query, this function
4636 * restores the $post global to the current post in this query.
4640 public function reset_postdata() {
4641 if ( ! empty( $this->post ) ) {
4642 $GLOBALS['post'] = $this->post;
4643 setup_postdata( $this->post );
4649 * Redirect old slugs to the correct permalink.
4651 * Attempts to find the current slug from the past slugs.
4656 * @global wpdb $wpdb WordPress database abstraction object.
4658 * @return null If no link is found, null is returned.
4660 function wp_old_slug_redirect() {
4662 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
4665 // Guess the current post_type based on the query vars.
4666 if ( get_query_var('post_type') )
4667 $post_type = get_query_var('post_type');
4668 elseif ( !empty($wp_query->query_vars['pagename']) )
4669 $post_type = 'page';
4671 $post_type = 'post';
4673 if ( is_array( $post_type ) ) {
4674 if ( count( $post_type ) > 1 )
4676 $post_type = reset( $post_type );
4679 // Do not attempt redirect for hierarchical post types
4680 if ( is_post_type_hierarchical( $post_type ) )
4683 $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']);
4685 // if year, monthnum, or day have been specified, make our query more precise
4686 // just in case there are multiple identical _wp_old_slug values
4687 if ( '' != $wp_query->query_vars['year'] )
4688 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
4689 if ( '' != $wp_query->query_vars['monthnum'] )
4690 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
4691 if ( '' != $wp_query->query_vars['day'] )
4692 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
4694 $id = (int) $wpdb->get_var($query);
4699 $link = get_permalink($id);
4704 wp_redirect( $link, 301 ); // Permanent redirect
4710 * Set up global post data.
4714 * @param object $post Post data.
4715 * @return bool True when finished.
4717 function setup_postdata( $post ) {
4720 if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
4721 return $wp_query->setup_postdata( $post );