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 http://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.
24 function get_query_var($var) {
27 return $wp_query->get($var);
31 * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
33 * @uses WP_Query::get_queried_object
40 function get_queried_object() {
42 return $wp_query->get_queried_object();
46 * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
48 * @uses WP_Query::get_queried_object_id()
55 function get_queried_object_id() {
57 return $wp_query->get_queried_object_id();
63 * @see WP_Query::set()
67 * @param string $var Query variable key.
71 function set_query_var($var, $value) {
74 return $wp_query->set($var, $value);
78 * Set up The Loop with query parameters.
80 * This will override the current WordPress Loop and shouldn't be used more than
81 * once. This must not be used within the WordPress Loop.
86 * @param string $query
87 * @return array List of posts
89 function query_posts($query) {
90 $GLOBALS['wp_query'] = new WP_Query();
91 return $GLOBALS['wp_query']->query($query);
95 * Destroy the previous query and set up a new query.
97 * This should be used after {@link query_posts()} and before another {@link
98 * query_posts()}. This will remove obscure bugs that occur when the previous
99 * wp_query object is not destroyed properly before another is set up.
104 function wp_reset_query() {
105 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
110 * After looping through a separate query, this function restores
111 * the $post global to the current post in the main query
116 function wp_reset_postdata() {
118 if ( !empty($wp_query->post) ) {
119 $GLOBALS['post'] = $wp_query->post;
120 setup_postdata($wp_query->post);
129 * Is the query for an existing archive page?
131 * Month, Year, Category, Author, Post Type archive...
133 * @see WP_Query::is_archive()
139 function is_archive() {
142 if ( ! isset( $wp_query ) ) {
143 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
147 return $wp_query->is_archive();
151 * Is the query for an existing post type archive page?
153 * @see WP_Query::is_post_type_archive()
157 * @param mixed $post_types Optional. Post type or array of posts types to check against.
160 function is_post_type_archive( $post_types = '' ) {
163 if ( ! isset( $wp_query ) ) {
164 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
168 return $wp_query->is_post_type_archive( $post_types );
172 * Is the query for an existing attachment page?
174 * @see WP_Query::is_attachment()
180 function is_attachment() {
183 if ( ! isset( $wp_query ) ) {
184 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
188 return $wp_query->is_attachment();
192 * Is the query for an existing author archive page?
194 * If the $author parameter is specified, this function will additionally
195 * check if the query is for one of the authors specified.
197 * @see WP_Query::is_author()
201 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
204 function is_author( $author = '' ) {
207 if ( ! isset( $wp_query ) ) {
208 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
212 return $wp_query->is_author( $author );
216 * Is the query for an existing category archive page?
218 * If the $category parameter is specified, this function will additionally
219 * check if the query is for one of the categories specified.
221 * @see WP_Query::is_category()
225 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
228 function is_category( $category = '' ) {
231 if ( ! isset( $wp_query ) ) {
232 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
236 return $wp_query->is_category( $category );
240 * Is the query for an existing tag archive page?
242 * If the $tag parameter is specified, this function will additionally
243 * check if the query is for one of the tags specified.
245 * @see WP_Query::is_tag()
249 * @param mixed $slug Optional. Tag slug or array of slugs.
252 function is_tag( $slug = '' ) {
255 if ( ! isset( $wp_query ) ) {
256 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
260 return $wp_query->is_tag( $slug );
264 * Is the query for an existing taxonomy archive page?
266 * If the $taxonomy parameter is specified, this function will additionally
267 * check if the query is for that specific $taxonomy.
269 * If the $term parameter is specified in addition to the $taxonomy parameter,
270 * this function will additionally check if the query is for one of the terms
273 * @see WP_Query::is_tax()
277 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
278 * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
281 function is_tax( $taxonomy = '', $term = '' ) {
284 if ( ! isset( $wp_query ) ) {
285 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
289 return $wp_query->is_tax( $taxonomy, $term );
293 * Whether the current URL is within the comments popup window.
295 * @see WP_Query::is_comments_popup()
301 function is_comments_popup() {
304 if ( ! isset( $wp_query ) ) {
305 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
309 return $wp_query->is_comments_popup();
313 * Is the query for an existing date archive?
315 * @see WP_Query::is_date()
324 if ( ! isset( $wp_query ) ) {
325 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
329 return $wp_query->is_date();
333 * Is the query for an existing day archive?
335 * @see WP_Query::is_day()
344 if ( ! isset( $wp_query ) ) {
345 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
349 return $wp_query->is_day();
353 * Is the query for a feed?
355 * @see WP_Query::is_feed()
359 * @param string|array $feeds Optional feed types to check.
362 function is_feed( $feeds = '' ) {
365 if ( ! isset( $wp_query ) ) {
366 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
370 return $wp_query->is_feed( $feeds );
374 * Is the query for a comments feed?
376 * @see WP_Query::is_comments_feed()
382 function is_comment_feed() {
385 if ( ! isset( $wp_query ) ) {
386 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
390 return $wp_query->is_comment_feed();
394 * Is the query for the front page of the site?
396 * This is for what is displayed at your site's main URL.
398 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
400 * If you set a static page for the front page of your site, this function will return
401 * true when viewing that page.
403 * Otherwise the same as @see is_home()
405 * @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() {
724 return $wp_query->is_main_query();
728 * The Loop. Post loop control.
732 * Whether current WordPress query has results to loop over.
734 * @see WP_Query::have_posts()
740 function have_posts() {
743 return $wp_query->have_posts();
747 * Whether the caller is in the Loop.
752 * @return bool True if caller is within loop, false if loop hasn't started or ended.
754 function in_the_loop() {
757 return $wp_query->in_the_loop;
761 * Rewind the loop posts.
763 * @see WP_Query::rewind_posts()
769 function rewind_posts() {
772 return $wp_query->rewind_posts();
776 * Iterate the post index in the loop.
778 * @see WP_Query::the_post()
782 function the_post() {
785 $wp_query->the_post();
793 * Whether there are comments to loop over.
795 * @see WP_Query::have_comments()
801 function have_comments() {
803 return $wp_query->have_comments();
807 * Iterate comment index in the comment loop.
809 * @see WP_Query::the_comment()
815 function the_comment() {
817 return $wp_query->the_comment();
825 * The WordPress Query class.
827 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
834 * Query vars set by the user
843 * Query vars, after parsing
849 var $query_vars = array();
852 * Taxonomy query, as passed to get_tax_sql()
856 * @var object WP_Tax_Query
861 * Metadata query container
865 * @var object WP_Meta_Query
867 var $meta_query = false;
870 * Holds the data for a single object that is queried.
872 * Holds the contents of a post, page, category, attachment.
881 * The ID of the queried object.
887 var $queried_object_id;
890 * Get post database query.
908 * The amount of posts for the current query.
917 * Index of the current item in the loop.
923 var $current_post = -1;
926 * Whether the loop has started and the caller is in the loop.
932 var $in_the_loop = false;
935 * The current post ID.
944 * The list of comments for current post.
953 * The amount of comments for the posts.
959 var $comment_count = 0;
962 * The index of the comment in the comment loop.
968 var $current_comment = -1;
971 * Current comment ID.
980 * The amount of found posts for the current query.
982 * If limit clause was not used, equals $post_count.
988 var $found_posts = 0;
991 * The amount of pages.
997 var $max_num_pages = 0;
1000 * The amount of comment pages.
1006 var $max_num_comment_pages = 0;
1009 * Set if query is single post.
1015 var $is_single = false;
1018 * Set if query is preview of blog.
1024 var $is_preview = false;
1027 * Set if query returns a page.
1033 var $is_page = false;
1036 * Set if query is an archive list.
1042 var $is_archive = false;
1045 * Set if query is part of a date.
1051 var $is_date = false;
1054 * Set if query contains a year.
1060 var $is_year = false;
1063 * Set if query contains a month.
1069 var $is_month = false;
1072 * Set if query contains a day.
1078 var $is_day = false;
1081 * Set if query contains time.
1087 var $is_time = false;
1090 * Set if query contains an author.
1096 var $is_author = false;
1099 * Set if query contains category.
1105 var $is_category = false;
1108 * Set if query contains tag.
1114 var $is_tag = false;
1117 * Set if query contains taxonomy.
1123 var $is_tax = false;
1126 * Set if query was part of a search result.
1132 var $is_search = false;
1135 * Set if query is feed display.
1141 var $is_feed = false;
1144 * Set if query is comment feed display.
1150 var $is_comment_feed = false;
1153 * Set if query is trackback.
1159 var $is_trackback = false;
1162 * Set if query is blog homepage.
1168 var $is_home = false;
1171 * Set if query couldn't found anything.
1177 var $is_404 = false;
1180 * Set if query is within comments popup window.
1186 var $is_comments_popup = false;
1189 * Set if query is paged
1195 var $is_paged = false;
1198 * Set if query is part of administration page.
1204 var $is_admin = false;
1207 * Set if query is an attachment.
1213 var $is_attachment = false;
1216 * Set if is single, is a page, or is an attachment.
1222 var $is_singular = false;
1225 * Set if query is for robots.
1231 var $is_robots = false;
1234 * Set if query contains posts.
1236 * Basically, the homepage if the option isn't set for the static homepage.
1242 var $is_posts_page = false;
1245 * Set if query is for a post type archive.
1251 var $is_post_type_archive = false;
1254 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
1255 * whether we have to re-parse because something has changed
1260 var $query_vars_hash = false;
1263 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
1264 * via pre_get_posts hooks.
1269 var $query_vars_changed = true;
1272 * Set if post thumbnails are cached
1278 var $thumbnails_cached = false;
1281 * Resets query flags to false.
1283 * The query flags are what page info WordPress was able to figure out.
1288 function init_query_flags() {
1289 $this->is_single = false;
1290 $this->is_preview = false;
1291 $this->is_page = false;
1292 $this->is_archive = false;
1293 $this->is_date = false;
1294 $this->is_year = false;
1295 $this->is_month = false;
1296 $this->is_day = false;
1297 $this->is_time = false;
1298 $this->is_author = false;
1299 $this->is_category = false;
1300 $this->is_tag = false;
1301 $this->is_tax = false;
1302 $this->is_search = false;
1303 $this->is_feed = false;
1304 $this->is_comment_feed = false;
1305 $this->is_trackback = false;
1306 $this->is_home = false;
1307 $this->is_404 = false;
1308 $this->is_comments_popup = false;
1309 $this->is_paged = false;
1310 $this->is_admin = false;
1311 $this->is_attachment = false;
1312 $this->is_singular = false;
1313 $this->is_robots = false;
1314 $this->is_posts_page = false;
1315 $this->is_post_type_archive = false;
1319 * Initiates object properties and sets default values.
1325 unset($this->posts);
1326 unset($this->query);
1327 $this->query_vars = array();
1328 unset($this->queried_object);
1329 unset($this->queried_object_id);
1330 $this->post_count = 0;
1331 $this->current_post = -1;
1332 $this->in_the_loop = false;
1333 unset( $this->request );
1334 unset( $this->post );
1335 unset( $this->comments );
1336 unset( $this->comment );
1337 $this->comment_count = 0;
1338 $this->current_comment = -1;
1339 $this->found_posts = 0;
1340 $this->max_num_pages = 0;
1341 $this->max_num_comment_pages = 0;
1343 $this->init_query_flags();
1347 * Reparse the query vars.
1352 function parse_query_vars() {
1353 $this->parse_query();
1357 * Fills in the query variables, which do not exist within the parameter.
1362 * @param array $array Defined query variables.
1363 * @return array Complete query variables with undefined ones filled in empty.
1365 function fill_query_vars($array) {
1404 foreach ( $keys as $key ) {
1405 if ( !isset($array[$key]) )
1409 $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
1410 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in' );
1412 foreach ( $array_keys as $key ) {
1413 if ( !isset($array[$key]) )
1414 $array[$key] = array();
1420 * Parse a query string and set query type booleans.
1425 * @param string|array $query Optional query.
1427 function parse_query( $query = '' ) {
1428 if ( ! empty( $query ) ) {
1430 $this->query = $this->query_vars = wp_parse_args( $query );
1431 } elseif ( ! isset( $this->query ) ) {
1432 $this->query = $this->query_vars;
1435 $this->query_vars = $this->fill_query_vars($this->query_vars);
1436 $qv = &$this->query_vars;
1437 $this->query_vars_changed = true;
1439 if ( ! empty($qv['robots']) )
1440 $this->is_robots = true;
1442 $qv['p'] = absint($qv['p']);
1443 $qv['page_id'] = absint($qv['page_id']);
1444 $qv['year'] = absint($qv['year']);
1445 $qv['monthnum'] = absint($qv['monthnum']);
1446 $qv['day'] = absint($qv['day']);
1447 $qv['w'] = absint($qv['w']);
1448 $qv['m'] = absint($qv['m']);
1449 $qv['paged'] = absint($qv['paged']);
1450 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
1451 $qv['pagename'] = trim( $qv['pagename'] );
1452 $qv['name'] = trim( $qv['name'] );
1453 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
1454 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
1455 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
1456 if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
1458 // Compat. Map subpost to attachment.
1459 if ( '' != $qv['subpost'] )
1460 $qv['attachment'] = $qv['subpost'];
1461 if ( '' != $qv['subpost_id'] )
1462 $qv['attachment_id'] = $qv['subpost_id'];
1464 $qv['attachment_id'] = absint($qv['attachment_id']);
1466 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
1467 $this->is_single = true;
1468 $this->is_attachment = true;
1469 } elseif ( '' != $qv['name'] ) {
1470 $this->is_single = true;
1471 } elseif ( $qv['p'] ) {
1472 $this->is_single = true;
1473 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
1474 // If year, month, day, hour, minute, and second are set, a single
1475 // post is being queried.
1476 $this->is_single = true;
1477 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
1478 $this->is_page = true;
1479 $this->is_single = false;
1481 // Look for archive queries. Dates, categories, authors, search, post type archives.
1483 if ( !empty($qv['s']) ) {
1484 $this->is_search = true;
1487 if ( '' !== $qv['second'] ) {
1488 $this->is_time = true;
1489 $this->is_date = true;
1492 if ( '' !== $qv['minute'] ) {
1493 $this->is_time = true;
1494 $this->is_date = true;
1497 if ( '' !== $qv['hour'] ) {
1498 $this->is_time = true;
1499 $this->is_date = true;
1503 if ( ! $this->is_date ) {
1504 $this->is_day = true;
1505 $this->is_date = true;
1509 if ( $qv['monthnum'] ) {
1510 if ( ! $this->is_date ) {
1511 $this->is_month = true;
1512 $this->is_date = true;
1516 if ( $qv['year'] ) {
1517 if ( ! $this->is_date ) {
1518 $this->is_year = true;
1519 $this->is_date = true;
1524 $this->is_date = true;
1525 if ( strlen($qv['m']) > 9 ) {
1526 $this->is_time = true;
1527 } else if ( strlen($qv['m']) > 7 ) {
1528 $this->is_day = true;
1529 } else if ( strlen($qv['m']) > 5 ) {
1530 $this->is_month = true;
1532 $this->is_year = true;
1536 if ( '' != $qv['w'] ) {
1537 $this->is_date = true;
1540 $this->query_vars_hash = false;
1541 $this->parse_tax_query( $qv );
1543 foreach ( $this->tax_query->queries as $tax_query ) {
1544 if ( 'NOT IN' != $tax_query['operator'] ) {
1545 switch ( $tax_query['taxonomy'] ) {
1547 $this->is_category = true;
1550 $this->is_tag = true;
1553 $this->is_tax = true;
1557 unset( $tax_query );
1559 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
1560 $this->is_author = false;
1562 $this->is_author = true;
1565 if ( '' != $qv['author_name'] )
1566 $this->is_author = true;
1568 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
1569 $post_type_obj = get_post_type_object( $qv['post_type'] );
1570 if ( ! empty( $post_type_obj->has_archive ) )
1571 $this->is_post_type_archive = true;
1574 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
1575 $this->is_archive = true;
1578 if ( '' != $qv['feed'] )
1579 $this->is_feed = true;
1581 if ( '' != $qv['tb'] )
1582 $this->is_trackback = true;
1584 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
1585 $this->is_paged = true;
1587 if ( '' != $qv['comments_popup'] )
1588 $this->is_comments_popup = true;
1590 // if we're previewing inside the write screen
1591 if ( '' != $qv['preview'] )
1592 $this->is_preview = true;
1595 $this->is_admin = true;
1597 if ( false !== strpos($qv['feed'], 'comments-') ) {
1598 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
1599 $qv['withcomments'] = 1;
1602 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1604 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
1605 $this->is_comment_feed = true;
1607 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 ) )
1608 $this->is_home = true;
1610 // Correct is_* for page_on_front and page_for_posts
1611 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
1612 $_query = wp_parse_args($this->query);
1613 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
1614 if ( isset($_query['pagename']) && '' == $_query['pagename'] )
1615 unset($_query['pagename']);
1616 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
1617 $this->is_page = true;
1618 $this->is_home = false;
1619 $qv['page_id'] = get_option('page_on_front');
1620 // Correct <!--nextpage--> for page_on_front
1621 if ( !empty($qv['paged']) ) {
1622 $qv['page'] = $qv['paged'];
1623 unset($qv['paged']);
1628 if ( '' != $qv['pagename'] ) {
1629 $this->queried_object = get_page_by_path($qv['pagename']);
1630 if ( !empty($this->queried_object) )
1631 $this->queried_object_id = (int) $this->queried_object->ID;
1633 unset($this->queried_object);
1635 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
1636 $this->is_page = false;
1637 $this->is_home = true;
1638 $this->is_posts_page = true;
1642 if ( $qv['page_id'] ) {
1643 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
1644 $this->is_page = false;
1645 $this->is_home = true;
1646 $this->is_posts_page = true;
1650 if ( !empty($qv['post_type']) ) {
1651 if ( is_array($qv['post_type']) )
1652 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
1654 $qv['post_type'] = sanitize_key($qv['post_type']);
1657 if ( ! empty( $qv['post_status'] ) ) {
1658 if ( is_array( $qv['post_status'] ) )
1659 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
1661 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
1664 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
1665 $this->is_comment_feed = false;
1667 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
1668 // Done correcting is_* for page_on_front and page_for_posts
1670 if ( '404' == $qv['error'] )
1673 $this->query_vars_hash = md5( serialize( $this->query_vars ) );
1674 $this->query_vars_changed = false;
1676 do_action_ref_array('parse_query', array(&$this));
1680 * Parses various taxonomy related query vars.
1685 * @param array &$q The query variables
1687 function parse_tax_query( &$q ) {
1688 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
1689 $tax_query = $q['tax_query'];
1691 $tax_query = array();
1694 if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
1695 $tax_query[] = array(
1696 'taxonomy' => $q['taxonomy'],
1697 'terms' => array( $q['term'] ),
1702 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1703 if ( 'post_tag' == $taxonomy )
1704 continue; // Handled further down in the $q['tag'] block
1706 if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
1707 $tax_query_defaults = array(
1708 'taxonomy' => $taxonomy,
1712 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
1713 $q[$t->query_var] = wp_basename( $q[$t->query_var] );
1716 $term = $q[$t->query_var];
1718 if ( strpos($term, '+') !== false ) {
1719 $terms = preg_split( '/[+]+/', $term );
1720 foreach ( $terms as $term ) {
1721 $tax_query[] = array_merge( $tax_query_defaults, array(
1722 'terms' => array( $term )
1726 $tax_query[] = array_merge( $tax_query_defaults, array(
1727 'terms' => preg_split( '/[,]+/', $term )
1734 if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
1735 $q['cat'] = ''.urldecode($q['cat']).'';
1736 $q['cat'] = addslashes_gpc($q['cat']);
1737 $cat_array = preg_split('/[,\s]+/', $q['cat']);
1739 $req_cats = array();
1740 foreach ( (array) $cat_array as $cat ) {
1741 $cat = intval($cat);
1746 $q['category__in'][] = $cat;
1747 $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
1749 $q['category__not_in'][] = $cat;
1750 $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
1753 $q['cat'] = implode(',', $req_cats);
1756 if ( !empty($q['category__in']) ) {
1757 $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
1758 $tax_query[] = array(
1759 'taxonomy' => 'category',
1760 'terms' => $q['category__in'],
1761 'field' => 'term_id',
1762 'include_children' => false
1766 if ( !empty($q['category__not_in']) ) {
1767 $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
1768 $tax_query[] = array(
1769 'taxonomy' => 'category',
1770 'terms' => $q['category__not_in'],
1771 'operator' => 'NOT IN',
1772 'include_children' => false
1776 if ( !empty($q['category__and']) ) {
1777 $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
1778 $tax_query[] = array(
1779 'taxonomy' => 'category',
1780 'terms' => $q['category__and'],
1781 'field' => 'term_id',
1782 'operator' => 'AND',
1783 'include_children' => false
1788 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
1789 if ( strpos($q['tag'], ',') !== false ) {
1790 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
1791 foreach ( (array) $tags as $tag ) {
1792 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1793 $q['tag_slug__in'][] = $tag;
1795 } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
1796 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
1797 foreach ( (array) $tags as $tag ) {
1798 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1799 $q['tag_slug__and'][] = $tag;
1802 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1803 $q['tag_slug__in'][] = $q['tag'];
1807 if ( !empty($q['tag_id']) ) {
1808 $q['tag_id'] = absint( $q['tag_id'] );
1809 $tax_query[] = array(
1810 'taxonomy' => 'post_tag',
1811 'terms' => $q['tag_id']
1815 if ( !empty($q['tag__in']) ) {
1816 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
1817 $tax_query[] = array(
1818 'taxonomy' => 'post_tag',
1819 'terms' => $q['tag__in']
1823 if ( !empty($q['tag__not_in']) ) {
1824 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
1825 $tax_query[] = array(
1826 'taxonomy' => 'post_tag',
1827 'terms' => $q['tag__not_in'],
1828 'operator' => 'NOT IN'
1832 if ( !empty($q['tag__and']) ) {
1833 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
1834 $tax_query[] = array(
1835 'taxonomy' => 'post_tag',
1836 'terms' => $q['tag__and'],
1841 if ( !empty($q['tag_slug__in']) ) {
1842 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
1843 $tax_query[] = array(
1844 'taxonomy' => 'post_tag',
1845 'terms' => $q['tag_slug__in'],
1850 if ( !empty($q['tag_slug__and']) ) {
1851 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
1852 $tax_query[] = array(
1853 'taxonomy' => 'post_tag',
1854 'terms' => $q['tag_slug__and'],
1860 $this->tax_query = new WP_Tax_Query( $tax_query );
1864 * Sets the 404 property and saves whether query is feed.
1869 function set_404() {
1870 $is_feed = $this->is_feed;
1872 $this->init_query_flags();
1873 $this->is_404 = true;
1875 $this->is_feed = $is_feed;
1879 * Retrieve query variable.
1884 * @param string $query_var Query variable key.
1887 function get($query_var) {
1888 if ( isset($this->query_vars[$query_var]) )
1889 return $this->query_vars[$query_var];
1895 * Set query variable.
1900 * @param string $query_var Query variable key.
1901 * @param mixed $value Query variable value.
1903 function set($query_var, $value) {
1904 $this->query_vars[$query_var] = $value;
1908 * Retrieve the posts based on query variables.
1910 * There are a few filters and actions that can be used to modify the post
1915 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
1917 * @return array List of posts.
1919 function get_posts() {
1920 global $wpdb, $user_ID, $_wp_using_ext_object_cache;
1922 $this->parse_query();
1924 do_action_ref_array('pre_get_posts', array(&$this));
1927 $q = &$this->query_vars;
1929 // Fill again in case pre_get_posts unset some vars.
1930 $q = $this->fill_query_vars($q);
1933 $this->meta_query = new WP_Meta_Query();
1934 $this->meta_query->parse_query_vars( $q );
1936 // Set a flag if a pre_get_posts hook changed the query vars.
1937 $hash = md5( serialize( $this->query_vars ) );
1938 if ( $hash != $this->query_vars_hash ) {
1939 $this->query_vars_changed = true;
1940 $this->query_vars_hash = $hash;
1944 // First let's clear some variables
1947 $whichmimetype = '';
1954 $post_status_join = false;
1957 if ( isset( $q['caller_get_posts'] ) ) {
1958 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
1959 if ( !isset( $q['ignore_sticky_posts'] ) )
1960 $q['ignore_sticky_posts'] = $q['caller_get_posts'];
1963 if ( !isset( $q['ignore_sticky_posts'] ) )
1964 $q['ignore_sticky_posts'] = false;
1966 if ( !isset($q['suppress_filters']) )
1967 $q['suppress_filters'] = false;
1969 if ( !isset($q['cache_results']) ) {
1970 if ( $_wp_using_ext_object_cache )
1971 $q['cache_results'] = false;
1973 $q['cache_results'] = true;
1976 if ( !isset($q['update_post_term_cache']) )
1977 $q['update_post_term_cache'] = true;
1979 if ( !isset($q['update_post_meta_cache']) )
1980 $q['update_post_meta_cache'] = true;
1982 if ( !isset($q['post_type']) ) {
1983 if ( $this->is_search )
1984 $q['post_type'] = 'any';
1986 $q['post_type'] = '';
1988 $post_type = $q['post_type'];
1989 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
1990 $q['posts_per_page'] = get_option('posts_per_page');
1991 if ( isset($q['showposts']) && $q['showposts'] ) {
1992 $q['showposts'] = (int) $q['showposts'];
1993 $q['posts_per_page'] = $q['showposts'];
1995 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
1996 $q['posts_per_page'] = $q['posts_per_archive_page'];
1997 if ( !isset($q['nopaging']) ) {
1998 if ( $q['posts_per_page'] == -1 ) {
1999 $q['nopaging'] = true;
2001 $q['nopaging'] = false;
2004 if ( $this->is_feed ) {
2005 $q['posts_per_page'] = get_option('posts_per_rss');
2006 $q['nopaging'] = false;
2008 $q['posts_per_page'] = (int) $q['posts_per_page'];
2009 if ( $q['posts_per_page'] < -1 )
2010 $q['posts_per_page'] = abs($q['posts_per_page']);
2011 else if ( $q['posts_per_page'] == 0 )
2012 $q['posts_per_page'] = 1;
2014 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
2015 $q['comments_per_page'] = get_option('comments_per_page');
2017 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
2018 $this->is_page = true;
2019 $this->is_home = false;
2020 $q['page_id'] = get_option('page_on_front');
2023 if ( isset($q['page']) ) {
2024 $q['page'] = trim($q['page'], '/');
2025 $q['page'] = absint($q['page']);
2028 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
2029 if ( isset($q['no_found_rows']) )
2030 $q['no_found_rows'] = (bool) $q['no_found_rows'];
2032 $q['no_found_rows'] = false;
2034 switch ( $q['fields'] ) {
2036 $fields = "$wpdb->posts.ID";
2039 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
2042 $fields = "$wpdb->posts.*";
2045 if ( '' !== $q['menu_order'] )
2046 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
2048 // If a month is specified in the querystring, load that month
2050 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
2051 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
2052 if ( strlen($q['m']) > 5 )
2053 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
2054 if ( strlen($q['m']) > 7 )
2055 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
2056 if ( strlen($q['m']) > 9 )
2057 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
2058 if ( strlen($q['m']) > 11 )
2059 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
2060 if ( strlen($q['m']) > 13 )
2061 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
2064 if ( '' !== $q['hour'] )
2065 $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
2067 if ( '' !== $q['minute'] )
2068 $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
2070 if ( '' !== $q['second'] )
2071 $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
2074 $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
2076 if ( $q['monthnum'] )
2077 $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
2080 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
2082 // If we've got a post_type AND it's not "any" post_type.
2083 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
2084 foreach ( (array)$q['post_type'] as $_post_type ) {
2085 $ptype_obj = get_post_type_object($_post_type);
2086 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
2089 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
2090 // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
2091 $q['name'] = $q[ $ptype_obj->query_var ];
2093 // Hierarchical post_types will operate through the
2094 $q['pagename'] = $q[ $ptype_obj->query_var ];
2098 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
2104 if ( '' != $q['name'] ) {
2105 $q['name'] = sanitize_title_for_query( $q['name'] );
2106 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
2107 } elseif ( '' != $q['pagename'] ) {
2108 if ( isset($this->queried_object_id) ) {
2109 $reqpage = $this->queried_object_id;
2111 if ( 'page' != $q['post_type'] ) {
2112 foreach ( (array)$q['post_type'] as $_post_type ) {
2113 $ptype_obj = get_post_type_object($_post_type);
2114 if ( !$ptype_obj || !$ptype_obj->hierarchical )
2117 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
2123 $reqpage = get_page_by_path($q['pagename']);
2125 if ( !empty($reqpage) )
2126 $reqpage = $reqpage->ID;
2131 $page_for_posts = get_option('page_for_posts');
2132 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
2133 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
2134 $q['name'] = $q['pagename'];
2135 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
2136 $reqpage_obj = get_post( $reqpage );
2137 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
2138 $this->is_attachment = true;
2139 $post_type = $q['post_type'] = 'attachment';
2140 $this->is_page = true;
2141 $q['attachment_id'] = $reqpage;
2144 } elseif ( '' != $q['attachment'] ) {
2145 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
2146 $q['name'] = $q['attachment'];
2147 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
2151 $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
2153 if ( intval($q['comments_popup']) )
2154 $q['p'] = absint($q['comments_popup']);
2156 // If an attachment is requested by number, let it supersede any post number.
2157 if ( $q['attachment_id'] )
2158 $q['p'] = absint($q['attachment_id']);
2160 // If a post number is specified, load that post
2162 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
2163 } elseif ( $q['post__in'] ) {
2164 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
2165 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
2166 } elseif ( $q['post__not_in'] ) {
2167 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
2168 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
2171 if ( is_numeric( $q['post_parent'] ) ) {
2172 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
2173 } elseif ( $q['post_parent__in'] ) {
2174 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
2175 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
2176 } elseif ( $q['post_parent__not_in'] ) {
2177 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
2178 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
2181 if ( $q['page_id'] ) {
2182 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
2183 $q['p'] = $q['page_id'];
2184 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
2188 // If a search pattern is specified, load the posts that match
2189 if ( !empty($q['s']) ) {
2190 // added slashes screw with quote grouping when done early, so done later
2191 $q['s'] = stripslashes($q['s']);
2192 if ( empty( $_GET['s'] ) && $this->is_main_query() )
2193 $q['s'] = urldecode($q['s']);
2194 if ( !empty($q['sentence']) ) {
2195 $q['search_terms'] = array($q['s']);
2197 preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
2198 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
2200 $n = !empty($q['exact']) ? '' : '%';
2202 foreach( (array) $q['search_terms'] as $term ) {
2203 $term = esc_sql( like_escape( $term ) );
2204 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
2205 $searchand = ' AND ';
2208 if ( !empty($search) ) {
2209 $search = " AND ({$search}) ";
2210 if ( !is_user_logged_in() )
2211 $search .= " AND ($wpdb->posts.post_password = '') ";
2215 // Allow plugins to contextually add/remove/modify the search section of the database query
2216 $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
2219 if ( !$this->is_singular ) {
2220 $this->parse_tax_query( $q );
2222 $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
2224 $join .= $clauses['join'];
2225 $where .= $clauses['where'];
2228 if ( $this->is_tax ) {
2229 if ( empty($post_type) ) {
2230 // Do a fully inclusive search for currently registered post types of queried taxonomies
2231 $post_type = array();
2232 $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
2233 foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
2234 $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
2235 if ( array_intersect( $taxonomies, $object_taxonomies ) )
2240 elseif ( count( $post_type ) == 1 )
2241 $post_type = $post_type[0];
2243 $post_status_join = true;
2244 } elseif ( in_array('attachment', (array) $post_type) ) {
2245 $post_status_join = true;
2250 if ( !empty($this->tax_query->queries) ) {
2251 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
2252 if ( !empty( $tax_query_in_and ) ) {
2253 if ( !isset( $q['taxonomy'] ) ) {
2254 foreach ( $tax_query_in_and as $a_tax_query ) {
2255 if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
2256 $q['taxonomy'] = $a_tax_query['taxonomy'];
2257 if ( 'slug' == $a_tax_query['field'] )
2258 $q['term'] = $a_tax_query['terms'][0];
2260 $q['term_id'] = $a_tax_query['terms'][0];
2267 $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
2268 if ( ! empty( $cat_query ) ) {
2269 $cat_query = reset( $cat_query );
2271 if ( ! empty( $cat_query['terms'][0] ) ) {
2272 $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
2274 $this->set( 'cat', $the_cat->term_id );
2275 $this->set( 'category_name', $the_cat->slug );
2280 unset( $cat_query );
2282 $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
2283 if ( ! empty( $tag_query ) ) {
2284 $tag_query = reset( $tag_query );
2286 if ( ! empty( $tag_query['terms'][0] ) ) {
2287 $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
2289 $this->set( 'tag_id', $the_tag->term_id );
2293 unset( $tag_query );
2297 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
2298 $groupby = "{$wpdb->posts}.ID";
2301 // Author/user stuff
2303 if ( empty($q['author']) || ($q['author'] == '0') ) {
2306 $q['author'] = (string)urldecode($q['author']);
2307 $q['author'] = addslashes_gpc($q['author']);
2308 if ( strpos($q['author'], '-') !== false ) {
2311 $q['author'] = explode('-', $q['author']);
2312 $q['author'] = (string)absint($q['author'][1]);
2317 $author_array = preg_split('/[,\s]+/', $q['author']);
2318 $_author_array = array();
2319 foreach ( $author_array as $key => $_author )
2320 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
2321 $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
2322 unset($author_array, $_author_array);
2325 // Author stuff for nice URLs
2327 if ( '' != $q['author_name'] ) {
2328 if ( strpos($q['author_name'], '/') !== false ) {
2329 $q['author_name'] = explode('/', $q['author_name']);
2330 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
2331 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
2333 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
2336 $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
2337 $q['author'] = get_user_by('slug', $q['author_name']);
2339 $q['author'] = $q['author']->ID;
2340 $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
2343 // MIME-Type stuff for attachment browsing
2345 if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
2346 $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
2348 $where .= $search . $whichauthor . $whichmimetype;
2350 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
2351 $q['order'] = 'DESC';
2354 if ( empty($q['orderby']) ) {
2355 $orderby = "$wpdb->posts.post_date " . $q['order'];
2356 } elseif ( 'none' == $q['orderby'] ) {
2358 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
2359 $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
2360 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
2361 $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
2363 // Used to filter values
2364 $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
2365 if ( !empty($q['meta_key']) ) {
2366 $allowed_keys[] = $q['meta_key'];
2367 $allowed_keys[] = 'meta_value';
2368 $allowed_keys[] = 'meta_value_num';
2370 $q['orderby'] = urldecode($q['orderby']);
2371 $q['orderby'] = addslashes_gpc($q['orderby']);
2373 $orderby_array = array();
2374 foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
2375 // Only allow certain values for safety
2376 if ( ! in_array($orderby, $allowed_keys) )
2379 switch ( $orderby ) {
2381 $orderby = "$wpdb->posts.menu_order";
2384 $orderby = "$wpdb->posts.ID";
2387 $orderby = 'RAND()';
2389 case $q['meta_key']:
2391 $orderby = "$wpdb->postmeta.meta_value";
2393 case 'meta_value_num':
2394 $orderby = "$wpdb->postmeta.meta_value+0";
2396 case 'comment_count':
2397 $orderby = "$wpdb->posts.comment_count";
2400 $orderby = "$wpdb->posts.post_" . $orderby;
2403 $orderby_array[] = $orderby;
2405 $orderby = implode( ',', $orderby_array );
2407 if ( empty( $orderby ) )
2408 $orderby = "$wpdb->posts.post_date ".$q['order'];
2410 $orderby .= " {$q['order']}";
2413 if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
2414 $post_type_cap = 'multiple_post_type';
2416 if ( is_array( $post_type ) )
2417 $post_type = reset( $post_type );
2418 $post_type_object = get_post_type_object( $post_type );
2419 if ( empty( $post_type_object ) )
2420 $post_type_cap = $post_type;
2423 if ( 'any' == $post_type ) {
2424 $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
2425 if ( ! empty( $in_search_post_types ) )
2426 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
2427 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
2428 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
2429 } elseif ( ! empty( $post_type ) ) {
2430 $where .= " AND $wpdb->posts.post_type = '$post_type'";
2431 $post_type_object = get_post_type_object ( $post_type );
2432 } elseif ( $this->is_attachment ) {
2433 $where .= " AND $wpdb->posts.post_type = 'attachment'";
2434 $post_type_object = get_post_type_object ( 'attachment' );
2435 } elseif ( $this->is_page ) {
2436 $where .= " AND $wpdb->posts.post_type = 'page'";
2437 $post_type_object = get_post_type_object ( 'page' );
2439 $where .= " AND $wpdb->posts.post_type = 'post'";
2440 $post_type_object = get_post_type_object ( 'post' );
2443 $edit_cap = 'edit_post';
2444 $read_cap = 'read_post';
2446 if ( ! empty( $post_type_object ) ) {
2447 $edit_others_cap = $post_type_object->cap->edit_others_posts;
2448 $read_private_cap = $post_type_object->cap->read_private_posts;
2450 $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
2451 $read_private_cap = 'read_private_' . $post_type_cap . 's';
2454 if ( ! empty( $q['post_status'] ) ) {
2455 $statuswheres = array();
2456 $q_status = $q['post_status'];
2457 if ( ! is_array( $q_status ) )
2458 $q_status = explode(',', $q_status);
2459 $r_status = array();
2460 $p_status = array();
2461 $e_status = array();
2462 if ( in_array('any', $q_status) ) {
2463 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
2464 $e_status[] = "$wpdb->posts.post_status <> '$status'";
2466 foreach ( get_post_stati() as $status ) {
2467 if ( in_array( $status, $q_status ) ) {
2468 if ( 'private' == $status )
2469 $p_status[] = "$wpdb->posts.post_status = '$status'";
2471 $r_status[] = "$wpdb->posts.post_status = '$status'";
2476 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
2477 $r_status = array_merge($r_status, $p_status);
2481 if ( !empty($e_status) ) {
2482 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
2484 if ( !empty($r_status) ) {
2485 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
2486 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
2488 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
2490 if ( !empty($p_status) ) {
2491 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
2492 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
2494 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
2496 if ( $post_status_join ) {
2497 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
2498 foreach ( $statuswheres as $index => $statuswhere )
2499 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
2501 foreach ( $statuswheres as $statuswhere )
2502 $where .= " AND $statuswhere";
2503 } elseif ( !$this->is_singular ) {
2504 $where .= " AND ($wpdb->posts.post_status = 'publish'";
2506 // Add public states.
2507 $public_states = get_post_stati( array('public' => true) );
2508 foreach ( (array) $public_states as $state ) {
2509 if ( 'publish' == $state ) // Publish is hard-coded above.
2511 $where .= " OR $wpdb->posts.post_status = '$state'";
2514 if ( $this->is_admin ) {
2515 // Add protected states that should show in the admin all list.
2516 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
2517 foreach ( (array) $admin_all_states as $state )
2518 $where .= " OR $wpdb->posts.post_status = '$state'";
2521 if ( is_user_logged_in() ) {
2522 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
2523 $private_states = get_post_stati( array('private' => true) );
2524 foreach ( (array) $private_states as $state )
2525 $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'";
2531 if ( !empty( $this->meta_query->queries ) ) {
2532 $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
2533 $join .= $clauses['join'];
2534 $where .= $clauses['where'];
2537 // Apply filters on where and join prior to paging so that any
2538 // manipulations to them are reflected in the paging by day queries.
2539 if ( !$q['suppress_filters'] ) {
2540 $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
2541 $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
2545 if ( empty($q['nopaging']) && !$this->is_singular ) {
2546 $page = absint($q['paged']);
2550 if ( empty($q['offset']) ) {
2551 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
2552 } else { // we're ignoring $page and using 'offset'
2553 $q['offset'] = absint($q['offset']);
2554 $pgstrt = $q['offset'] . ', ';
2556 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
2560 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
2561 if ( $this->is_archive || $this->is_search ) {
2562 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
2563 $cwhere = "WHERE comment_approved = '1' $where";
2564 $cgroupby = "$wpdb->comments.comment_id";
2565 } else { // Other non singular e.g. front
2566 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
2567 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
2571 if ( !$q['suppress_filters'] ) {
2572 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
2573 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
2574 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
2575 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2576 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2578 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2579 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2581 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
2582 $this->comment_count = count($this->comments);
2584 $post_ids = array();
2586 foreach ( $this->comments as $comment )
2587 $post_ids[] = (int) $comment->comment_post_ID;
2589 $post_ids = join(',', $post_ids);
2592 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
2597 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
2599 // Apply post-paging filters on where and join. Only plugins that
2600 // manipulate paging queries should use these hooks.
2601 if ( !$q['suppress_filters'] ) {
2602 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
2603 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
2604 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
2605 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
2606 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
2607 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
2608 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
2610 // Filter all clauses at once, for convenience
2611 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
2612 foreach ( $pieces as $piece )
2613 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2616 // Announce current selection parameters. For use by caching plugins.
2617 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
2619 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
2620 if ( !$q['suppress_filters'] ) {
2621 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
2622 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
2623 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
2624 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
2625 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
2626 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
2627 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
2629 // Filter all clauses at once, for convenience
2630 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
2631 foreach ( $pieces as $piece )
2632 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
2635 if ( ! empty($groupby) )
2636 $groupby = 'GROUP BY ' . $groupby;
2637 if ( !empty( $orderby ) )
2638 $orderby = 'ORDER BY ' . $orderby;
2641 if ( !$q['no_found_rows'] && !empty($limits) )
2642 $found_rows = 'SQL_CALC_FOUND_ROWS';
2644 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2646 if ( !$q['suppress_filters'] ) {
2647 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
2650 if ( 'ids' == $q['fields'] ) {
2651 $this->posts = $wpdb->get_col( $this->request );
2652 $this->post_count = count( $this->posts );
2653 $this->set_found_posts( $q, $limits );
2655 return $this->posts;
2658 if ( 'id=>parent' == $q['fields'] ) {
2659 $this->posts = $wpdb->get_results( $this->request );
2660 $this->post_count = count( $this->posts );
2661 $this->set_found_posts( $q, $limits );
2664 foreach ( $this->posts as $post )
2665 $r[ $post->ID ] = $post->post_parent;
2670 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
2671 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
2673 if ( $split_the_query ) {
2674 // First get the IDs and then fill in the objects
2676 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
2678 $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
2680 $ids = $wpdb->get_col( $this->request );
2683 $this->posts = $ids;
2684 $this->set_found_posts( $q, $limits );
2685 _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
2687 $this->posts = array();
2690 $this->posts = $wpdb->get_results( $this->request );
2691 $this->set_found_posts( $q, $limits );
2694 // Convert to WP_Post objects
2696 $this->posts = array_map( 'get_post', $this->posts );
2698 // Raw results filter. Prior to status checks.
2699 if ( !$q['suppress_filters'] )
2700 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
2702 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
2703 $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
2704 $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
2705 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
2706 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
2707 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
2708 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
2709 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
2710 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
2711 $this->comments = $wpdb->get_results($comments_request);
2712 $this->comment_count = count($this->comments);
2715 // Check post status to determine if post should be displayed.
2716 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
2717 $status = get_post_status($this->posts[0]);
2718 $post_status_obj = get_post_status_object($status);
2719 //$type = get_post_type($this->posts[0]);
2720 if ( !$post_status_obj->public ) {
2721 if ( ! is_user_logged_in() ) {
2722 // User must be logged in to view unpublished posts.
2723 $this->posts = array();
2725 if ( $post_status_obj->protected ) {
2726 // User must have edit permissions on the draft to preview.
2727 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
2728 $this->posts = array();
2730 $this->is_preview = true;
2731 if ( 'future' != $status )
2732 $this->posts[0]->post_date = current_time('mysql');
2734 } elseif ( $post_status_obj->private ) {
2735 if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
2736 $this->posts = array();
2738 $this->posts = array();
2743 if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
2744 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
2747 // Put sticky posts at the top of the posts array
2748 $sticky_posts = get_option('sticky_posts');
2749 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
2750 $num_posts = count($this->posts);
2752 // Loop over posts and relocate stickies to the front.
2753 for ( $i = 0; $i < $num_posts; $i++ ) {
2754 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
2755 $sticky_post = $this->posts[$i];
2756 // Remove sticky from current position
2757 array_splice($this->posts, $i, 1);
2758 // Move to front, after other stickies
2759 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
2760 // Increment the sticky offset. The next sticky will be placed at this offset.
2762 // Remove post from sticky posts array
2763 $offset = array_search($sticky_post->ID, $sticky_posts);
2764 unset( $sticky_posts[$offset] );
2768 // If any posts have been excluded specifically, Ignore those that are sticky.
2769 if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
2770 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
2772 // Fetch sticky posts that weren't in the query results
2773 if ( !empty($sticky_posts) ) {
2774 $stickies = get_posts( array(
2775 'post__in' => $sticky_posts,
2776 'post_type' => $post_type,
2777 'post_status' => 'publish',
2781 foreach ( $stickies as $sticky_post ) {
2782 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
2788 if ( !$q['suppress_filters'] )
2789 $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
2791 // Ensure that any posts added/modified via one of the filters above are
2792 // of the type WP_Post and are filtered.
2793 if ( $this->posts ) {
2794 $this->post_count = count( $this->posts );
2796 $this->posts = array_map( 'get_post', $this->posts );
2798 if ( $q['cache_results'] )
2799 update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
2801 $this->post = reset( $this->posts );
2803 $this->post_count = 0;
2804 $this->posts = array();
2807 return $this->posts;
2811 * Set up the amount of found posts and the number of pages (if limit clause was used)
2812 * for the current query.
2817 function set_found_posts( $q, $limits ) {
2820 // Bail if posts is an empty array. Continue if posts is an empty string,
2821 // null, or false to accommodate caching plugins that fill posts later.
2822 if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
2825 if ( ! empty( $limits ) )
2826 $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
2828 $this->found_posts = count( $this->posts );
2830 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
2832 if ( ! empty( $limits ) )
2833 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
2837 * Set up the next post and iterate current post index.
2842 * @return WP_Post Next post.
2844 function next_post() {
2846 $this->current_post++;
2848 $this->post = $this->posts[$this->current_post];
2853 * Sets up the current post.
2855 * Retrieves the next post, sets up the post, sets the 'in the loop'
2861 * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
2863 function the_post() {
2865 $this->in_the_loop = true;
2867 if ( $this->current_post == -1 ) // loop has just started
2868 do_action_ref_array('loop_start', array(&$this));
2870 $post = $this->next_post();
2871 setup_postdata($post);
2875 * Whether there are more posts available in the loop.
2877 * Calls action 'loop_end', when the loop is complete.
2881 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
2883 * @return bool True if posts are available, false if end of loop.
2885 function have_posts() {
2886 if ( $this->current_post + 1 < $this->post_count ) {
2888 } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
2889 do_action_ref_array('loop_end', array(&$this));
2890 // Do some cleaning up after the loop
2891 $this->rewind_posts();
2894 $this->in_the_loop = false;
2899 * Rewind the posts and reset post index.
2904 function rewind_posts() {
2905 $this->current_post = -1;
2906 if ( $this->post_count > 0 ) {
2907 $this->post = $this->posts[0];
2912 * Iterate current comment index and return comment object.
2917 * @return object Comment object.
2919 function next_comment() {
2920 $this->current_comment++;
2922 $this->comment = $this->comments[$this->current_comment];
2923 return $this->comment;
2927 * Sets up the current comment.
2931 * @global object $comment Current comment.
2932 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
2934 function the_comment() {
2937 $comment = $this->next_comment();
2939 if ( $this->current_comment == 0 ) {
2940 do_action('comment_loop_start');
2945 * Whether there are more comments available.
2947 * Automatically rewinds comments when finished.
2952 * @return bool True, if more comments. False, if no more posts.
2954 function have_comments() {
2955 if ( $this->current_comment + 1 < $this->comment_count ) {
2957 } elseif ( $this->current_comment + 1 == $this->comment_count ) {
2958 $this->rewind_comments();
2965 * Rewind the comments, resets the comment index and comment to first.
2970 function rewind_comments() {
2971 $this->current_comment = -1;
2972 if ( $this->comment_count > 0 ) {
2973 $this->comment = $this->comments[0];
2978 * Sets up the WordPress query by parsing query string.
2983 * @param string $query URL query string.
2984 * @return array List of posts.
2986 function query( $query ) {
2988 $this->query = $this->query_vars = wp_parse_args( $query );
2989 return $this->get_posts();
2993 * Retrieve queried object.
2995 * If queried object is not set, then the queried object will be set from
2996 * the category, tag, taxonomy, posts page, single post, page, or author
2997 * query variable. After it is set up, it will be returned.
3004 function get_queried_object() {
3005 if ( isset($this->queried_object) )
3006 return $this->queried_object;
3008 $this->queried_object = null;
3009 $this->queried_object_id = 0;
3011 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
3012 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
3014 $query = reset( $tax_query_in_and );
3016 if ( 'term_id' == $query['field'] )
3017 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
3018 elseif ( $query['terms'] )
3019 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
3021 if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
3022 $this->queried_object = $term;
3023 $this->queried_object_id = (int) $term->term_id;
3025 if ( $this->is_category )
3026 _make_cat_compat( $this->queried_object );
3028 } elseif ( $this->is_post_type_archive ) {
3029 $this->queried_object = get_post_type_object( $this->get('post_type') );
3030 } elseif ( $this->is_posts_page ) {
3031 $page_for_posts = get_option('page_for_posts');
3032 $this->queried_object = get_post( $page_for_posts );
3033 $this->queried_object_id = (int) $this->queried_object->ID;
3034 } elseif ( $this->is_singular && !is_null($this->post) ) {
3035 $this->queried_object = $this->post;
3036 $this->queried_object_id = (int) $this->post->ID;
3037 } elseif ( $this->is_author ) {
3038 $this->queried_object_id = (int) $this->get('author');
3039 $this->queried_object = get_userdata( $this->queried_object_id );
3042 return $this->queried_object;
3046 * Retrieve ID of the current queried object.
3053 function get_queried_object_id() {
3054 $this->get_queried_object();
3056 if ( isset($this->queried_object_id) ) {
3057 return $this->queried_object_id;
3066 * Sets up the WordPress query, if parameter is not empty.
3071 * @param string $query URL query string.
3074 function __construct($query = '') {
3075 if ( ! empty($query) ) {
3076 $this->query($query);
3081 * Is the query for an existing archive page?
3083 * Month, Year, Category, Author, Post Type archive...
3089 function is_archive() {
3090 return (bool) $this->is_archive;
3094 * Is the query for an existing post type archive page?
3098 * @param mixed $post_types Optional. Post type or array of posts types to check against.
3101 function is_post_type_archive( $post_types = '' ) {
3102 if ( empty( $post_types ) || !$this->is_post_type_archive )
3103 return (bool) $this->is_post_type_archive;
3105 $post_type_object = $this->get_queried_object();
3107 return in_array( $post_type_object->name, (array) $post_types );
3111 * Is the query for an existing attachment page?
3117 function is_attachment() {
3118 return (bool) $this->is_attachment;
3122 * Is the query for an existing author archive page?
3124 * If the $author parameter is specified, this function will additionally
3125 * check if the query is for one of the authors specified.
3129 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
3132 function is_author( $author = '' ) {
3133 if ( !$this->is_author )
3136 if ( empty($author) )
3139 $author_obj = $this->get_queried_object();
3141 $author = (array) $author;
3143 if ( in_array( $author_obj->ID, $author ) )
3145 elseif ( in_array( $author_obj->nickname, $author ) )
3147 elseif ( in_array( $author_obj->user_nicename, $author ) )
3154 * Is the query for an existing category archive page?
3156 * If the $category parameter is specified, this function will additionally
3157 * check if the query is for one of the categories specified.
3161 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
3164 function is_category( $category = '' ) {
3165 if ( !$this->is_category )
3168 if ( empty($category) )
3171 $cat_obj = $this->get_queried_object();
3173 $category = (array) $category;
3175 if ( in_array( $cat_obj->term_id, $category ) )
3177 elseif ( in_array( $cat_obj->name, $category ) )
3179 elseif ( in_array( $cat_obj->slug, $category ) )
3186 * Is the query for an existing tag archive page?
3188 * If the $tag parameter is specified, this function will additionally
3189 * check if the query is for one of the tags specified.
3193 * @param mixed $slug Optional. Tag slug or array of slugs.
3196 function is_tag( $slug = '' ) {
3197 if ( !$this->is_tag )
3200 if ( empty( $slug ) )
3203 $tag_obj = $this->get_queried_object();
3205 $slug = (array) $slug;
3207 if ( in_array( $tag_obj->slug, $slug ) )
3214 * Is the query for an existing taxonomy archive page?
3216 * If the $taxonomy parameter is specified, this function will additionally
3217 * check if the query is for that specific $taxonomy.
3219 * If the $term parameter is specified in addition to the $taxonomy parameter,
3220 * this function will additionally check if the query is for one of the terms
3225 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
3226 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
3229 function is_tax( $taxonomy = '', $term = '' ) {
3230 global $wp_taxonomies;
3232 if ( !$this->is_tax )
3235 if ( empty( $taxonomy ) )
3238 $queried_object = $this->get_queried_object();
3239 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
3240 $term_array = (array) $term;
3242 // Check that the taxonomy matches.
3243 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
3246 // Only a Taxonomy provided.
3247 if ( empty( $term ) )
3250 return isset( $queried_object->term_id ) &&
3251 count( array_intersect(
3252 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
3258 * Whether the current URL is within the comments popup window.
3264 function is_comments_popup() {
3265 return (bool) $this->is_comments_popup;
3269 * Is the query for an existing date archive?
3275 function is_date() {
3276 return (bool) $this->is_date;
3280 * Is the query for an existing day archive?
3287 return (bool) $this->is_day;
3291 * Is the query for a feed?
3295 * @param string|array $feeds Optional feed types to check.
3298 function is_feed( $feeds = '' ) {
3299 if ( empty( $feeds ) || ! $this->is_feed )
3300 return (bool) $this->is_feed;
3301 $qv = $this->get( 'feed' );
3302 if ( 'feed' == $qv )
3303 $qv = get_default_feed();
3304 return in_array( $qv, (array) $feeds );
3308 * Is the query for a comments feed?
3314 function is_comment_feed() {
3315 return (bool) $this->is_comment_feed;
3319 * Is the query for the front page of the site?
3321 * This is for what is displayed at your site's main URL.
3323 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
3325 * If you set a static page for the front page of your site, this function will return
3326 * true when viewing that page.
3328 * Otherwise the same as @see WP_Query::is_home()
3332 * @uses get_option()
3334 * @return bool True, if front of site.
3336 function is_front_page() {
3338 if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
3340 elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
3347 * Is the query for the blog homepage?
3349 * This is the page which shows the time based blog content of your site.
3351 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
3353 * If you set a static page for the front page of your site, this function will return
3354 * true only on the page you set as the "Posts page".
3356 * @see WP_Query::is_front_page()
3360 * @return bool True if blog view homepage.
3362 function is_home() {
3363 return (bool) $this->is_home;
3367 * Is the query for an existing month archive?
3373 function is_month() {
3374 return (bool) $this->is_month;
3378 * Is the query for an existing single page?
3380 * If the $page parameter is specified, this function will additionally
3381 * check if the query is for one of the pages specified.
3383 * @see WP_Query::is_single()
3384 * @see WP_Query::is_singular()
3388 * @param mixed $page Page ID, title, slug, or array of such.
3391 function is_page( $page = '' ) {
3392 if ( !$this->is_page )
3395 if ( empty( $page ) )
3398 $page_obj = $this->get_queried_object();
3400 $page = (array) $page;
3402 if ( in_array( $page_obj->ID, $page ) )
3404 elseif ( in_array( $page_obj->post_title, $page ) )
3406 else if ( in_array( $page_obj->post_name, $page ) )
3413 * Is the query for paged result and not for the first page?
3419 function is_paged() {
3420 return (bool) $this->is_paged;
3424 * Is the query for a post or page preview?
3430 function is_preview() {
3431 return (bool) $this->is_preview;
3435 * Is the query for the robots file?
3441 function is_robots() {
3442 return (bool) $this->is_robots;
3446 * Is the query for a search?
3452 function is_search() {
3453 return (bool) $this->is_search;
3457 * Is the query for an existing single post?
3459 * Works for any post type, except attachments and pages
3461 * If the $post parameter is specified, this function will additionally
3462 * check if the query is for one of the Posts specified.
3464 * @see WP_Query::is_page()
3465 * @see WP_Query::is_singular()
3469 * @param mixed $post Post ID, title, slug, or array of such.
3472 function is_single( $post = '' ) {
3473 if ( !$this->is_single )
3479 $post_obj = $this->get_queried_object();
3481 $post = (array) $post;
3483 if ( in_array( $post_obj->ID, $post ) )
3485 elseif ( in_array( $post_obj->post_title, $post ) )
3487 elseif ( in_array( $post_obj->post_name, $post ) )
3494 * Is the query for an existing single post of any post type (post, attachment, page, ... )?
3496 * If the $post_types parameter is specified, this function will additionally
3497 * check if the query is for one of the Posts Types specified.
3499 * @see WP_Query::is_page()
3500 * @see WP_Query::is_single()
3504 * @param mixed $post_types Optional. Post Type or array of Post Types
3507 function is_singular( $post_types = '' ) {
3508 if ( empty( $post_types ) || !$this->is_singular )
3509 return (bool) $this->is_singular;
3511 $post_obj = $this->get_queried_object();
3513 return in_array( $post_obj->post_type, (array) $post_types );
3517 * Is the query for a specific time?
3523 function is_time() {
3524 return (bool) $this->is_time;
3528 * Is the query for a trackback endpoint call?
3534 function is_trackback() {
3535 return (bool) $this->is_trackback;
3539 * Is the query for an existing year archive?
3545 function is_year() {
3546 return (bool) $this->is_year;
3550 * Is the query a 404 (returns no results)?
3557 return (bool) $this->is_404;
3561 * Is the query the main query?
3567 function is_main_query() {
3568 global $wp_the_query;
3569 return $wp_the_query === $this;
3574 * Redirect old slugs to the correct permalink.
3576 * Attempts to find the current slug from the past slugs.
3582 * @return null If no link is found, null is returned.
3584 function wp_old_slug_redirect() {
3586 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
3589 // Guess the current post_type based on the query vars.
3590 if ( get_query_var('post_type') )
3591 $post_type = get_query_var('post_type');
3592 elseif ( !empty($wp_query->query_vars['pagename']) )
3593 $post_type = 'page';
3595 $post_type = 'post';
3597 if ( is_array( $post_type ) ) {
3598 if ( count( $post_type ) > 1 )
3600 $post_type = array_shift( $post_type );
3603 // Do not attempt redirect for hierarchical post types
3604 if ( is_post_type_hierarchical( $post_type ) )
3607 $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']);
3609 // if year, monthnum, or day have been specified, make our query more precise
3610 // just in case there are multiple identical _wp_old_slug values
3611 if ( '' != $wp_query->query_vars['year'] )
3612 $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
3613 if ( '' != $wp_query->query_vars['monthnum'] )
3614 $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
3615 if ( '' != $wp_query->query_vars['day'] )
3616 $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
3618 $id = (int) $wpdb->get_var($query);
3623 $link = get_permalink($id);
3628 wp_redirect( $link, 301 ); // Permanent redirect
3634 * Set up global post data.
3638 * @param object $post Post data.
3639 * @uses do_action_ref_array() Calls 'the_post'
3640 * @return bool True when finished.
3642 function setup_postdata( $post ) {
3643 global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
3645 $id = (int) $post->ID;
3647 $authordata = get_userdata($post->post_author);
3649 $currentday = mysql2date('d.m.y', $post->post_date, false);
3650 $currentmonth = mysql2date('m', $post->post_date, false);
3653 $page = get_query_var('page');
3656 if ( is_single() || is_page() || is_feed() )
3658 $content = $post->post_content;
3659 if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
3662 $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
3663 $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
3664 $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
3665 // Ignore nextpage at the beginning of the content.
3666 if ( 0 === strpos( $content, '<!--nextpage-->' ) )
3667 $content = substr( $content, 15 );
3668 $pages = explode('<!--nextpage-->', $content);
3669 $numpages = count($pages);
3670 if ( $numpages > 1 )
3673 $pages = array( $post->post_content );
3676 do_action_ref_array('the_post', array(&$post));