7 function get_query_var($var) {
10 return $wp_query->get($var);
13 function set_query_var($var, $value) {
16 return $wp_query->set($var, $value);
19 function &query_posts($query) {
20 unset($GLOBALS['wp_query']);
21 $GLOBALS['wp_query'] =& new WP_Query();
22 return $GLOBALS['wp_query']->query($query);
25 function wp_reset_query() {
26 unset($GLOBALS['wp_query']);
27 $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
29 if ( !empty($wp_query->post) ) {
30 $GLOBALS['post'] = $wp_query->post;
31 setup_postdata($wp_query->post);
39 function is_admin () {
40 if ( defined('WP_ADMIN') )
45 function is_archive () {
48 return $wp_query->is_archive;
51 function is_attachment () {
54 return $wp_query->is_attachment;
57 function is_author ($author = '') {
60 if ( !$wp_query->is_author )
66 $author_obj = $wp_query->get_queried_object();
68 $author = (array) $author;
70 if ( in_array( $author_obj->ID, $author ) )
72 elseif ( in_array( $author_obj->nickname, $author ) )
74 elseif ( in_array( $author_obj->user_nicename, $author ) )
80 function is_category ($category = '') {
83 if ( !$wp_query->is_category )
86 if ( empty($category) )
89 $cat_obj = $wp_query->get_queried_object();
91 $category = (array) $category;
93 if ( in_array( $cat_obj->term_id, $category ) )
95 elseif ( in_array( $cat_obj->name, $category ) )
97 elseif ( in_array( $cat_obj->slug, $category ) )
103 function is_tag( $slug = '' ) {
106 if ( !$wp_query->is_tag )
109 if ( empty( $slug ) )
112 $tag_obj = $wp_query->get_queried_object();
114 $slug = (array) $slug;
116 if ( in_array( $tag_obj->slug, $slug ) )
122 function is_tax( $slug = '' ) {
125 if ( !$wp_query->is_tax )
131 $term = $wp_query->get_queried_object();
133 $slug = (array) $slug;
135 if ( in_array( $term->slug, $slug ) )
141 function is_comments_popup () {
144 return $wp_query->is_comments_popup;
147 function is_date () {
150 return $wp_query->is_date;
156 return $wp_query->is_day;
159 function is_feed () {
162 return $wp_query->is_feed;
166 * is_front_page() - Is it the front of the site, whether blog view or a WP Page?
172 * @return bool True if front of site
174 function is_front_page () {
176 if ( 'posts' == get_option('show_on_front') && is_home() )
178 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
185 * is_home() - Is it the blog view homepage?
188 * @global object $wp_query
190 * @return bool True if blog view homepage
192 function is_home () {
195 return $wp_query->is_home;
198 function is_month () {
201 return $wp_query->is_month;
204 function is_page ($page = '') {
207 if ( !$wp_query->is_page )
213 $page_obj = $wp_query->get_queried_object();
215 $page = (array) $page;
217 if ( in_array( $page_obj->ID, $page ) )
219 elseif ( in_array( $page_obj->post_title, $page ) )
221 else if ( in_array( $page_obj->post_name, $page ) )
227 function is_paged () {
230 return $wp_query->is_paged;
233 function is_plugin_page() {
236 if ( isset($plugin_page) )
242 function is_preview() {
245 return $wp_query->is_preview;
248 function is_robots() {
251 return $wp_query->is_robots;
254 function is_search () {
257 return $wp_query->is_search;
260 function is_single ($post = '') {
263 if ( !$wp_query->is_single )
269 $post_obj = $wp_query->get_queried_object();
271 $post = (array) $post;
273 if ( in_array( $post_obj->ID, $post ) )
275 elseif ( in_array( $post_obj->post_title, $post ) )
277 elseif ( in_array( $post_obj->post_name, $post ) )
283 function is_singular() {
286 return $wp_query->is_singular;
289 function is_time () {
292 return $wp_query->is_time;
295 function is_trackback () {
298 return $wp_query->is_trackback;
301 function is_year () {
304 return $wp_query->is_year;
310 return $wp_query->is_404;
314 * The Loop. Post loop control.
317 function have_posts() {
320 return $wp_query->have_posts();
323 function in_the_loop() {
326 return $wp_query->in_the_loop;
329 function rewind_posts() {
332 return $wp_query->rewind_posts();
335 function the_post() {
338 $wp_query->the_post();
345 function have_comments() {
347 return $wp_query->have_comments();
350 function the_comment() {
352 return $wp_query->the_comment();
361 var $query_vars = array();
363 var $queried_object_id;
368 var $current_post = -1;
369 var $in_the_loop = false;
373 var $comment_count = 0;
374 var $current_comment = -1;
377 var $found_posts = 0;
378 var $max_num_pages = 0;
380 var $is_single = false;
381 var $is_preview = false;
382 var $is_page = false;
383 var $is_archive = false;
384 var $is_date = false;
385 var $is_year = false;
386 var $is_month = false;
388 var $is_time = false;
389 var $is_author = false;
390 var $is_category = false;
393 var $is_search = false;
394 var $is_feed = false;
395 var $is_comment_feed = false;
396 var $is_trackback = false;
397 var $is_home = false;
399 var $is_comments_popup = false;
400 var $is_admin = false;
401 var $is_attachment = false;
402 var $is_singular = false;
403 var $is_robots = false;
404 var $is_posts_page = false;
406 function init_query_flags() {
407 $this->is_single = false;
408 $this->is_page = false;
409 $this->is_archive = false;
410 $this->is_date = false;
411 $this->is_year = false;
412 $this->is_month = false;
413 $this->is_day = false;
414 $this->is_time = false;
415 $this->is_author = false;
416 $this->is_category = false;
417 $this->is_tag = false;
418 $this->is_tax = false;
419 $this->is_search = false;
420 $this->is_feed = false;
421 $this->is_comment_feed = false;
422 $this->is_trackback = false;
423 $this->is_home = false;
424 $this->is_404 = false;
425 $this->is_paged = false;
426 $this->is_admin = false;
427 $this->is_attachment = false;
428 $this->is_singular = false;
429 $this->is_robots = false;
430 $this->is_posts_page = false;
436 $this->query_vars = array();
437 unset($this->queried_object);
438 unset($this->queried_object_id);
439 $this->post_count = 0;
440 $this->current_post = -1;
441 $this->in_the_loop = false;
443 $this->init_query_flags();
446 // Reparse the query vars.
447 function parse_query_vars() {
448 $this->parse_query('');
451 function fill_query_vars($array) {
486 foreach ($keys as $key) {
487 if ( !isset($array[$key]))
491 $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
492 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
494 foreach ( $array_keys as $key ) {
495 if ( !isset($array[$key]))
496 $array[$key] = array();
501 // Parse a query string and set query type booleans.
502 function parse_query ($query) {
503 if ( !empty($query) || !isset($this->query) ) {
505 if ( is_array($query) )
506 $this->query_vars = $query;
508 parse_str($query, $this->query_vars);
509 $this->query = $query;
512 $this->query_vars = $this->fill_query_vars($this->query_vars);
513 $qv = &$this->query_vars;
515 if ( ! empty($qv['robots']) )
516 $this->is_robots = true;
518 $qv['p'] = absint($qv['p']);
519 $qv['page_id'] = absint($qv['page_id']);
520 $qv['year'] = absint($qv['year']);
521 $qv['monthnum'] = absint($qv['monthnum']);
522 $qv['day'] = absint($qv['day']);
523 $qv['w'] = absint($qv['w']);
524 $qv['m'] = absint($qv['m']);
525 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
526 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
527 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
528 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
530 // Compat. Map subpost to attachment.
531 if ( '' != $qv['subpost'] )
532 $qv['attachment'] = $qv['subpost'];
533 if ( '' != $qv['subpost_id'] )
534 $qv['attachment_id'] = $qv['subpost_id'];
536 $qv['attachment_id'] = absint($qv['attachment_id']);
538 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
539 $this->is_single = true;
540 $this->is_attachment = true;
541 } elseif ( '' != $qv['name'] ) {
542 $this->is_single = true;
543 } elseif ( $qv['p'] ) {
544 $this->is_single = true;
545 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
546 // If year, month, day, hour, minute, and second are set, a single
547 // post is being queried.
548 $this->is_single = true;
549 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
550 $this->is_page = true;
551 $this->is_single = false;
552 } elseif ( !empty($qv['s']) ) {
553 $this->is_search = true;
555 // Look for archive queries. Dates, categories, authors.
557 if ( '' !== $qv['second'] ) {
558 $this->is_time = true;
559 $this->is_date = true;
562 if ( '' !== $qv['minute'] ) {
563 $this->is_time = true;
564 $this->is_date = true;
567 if ( '' !== $qv['hour'] ) {
568 $this->is_time = true;
569 $this->is_date = true;
573 if (! $this->is_date) {
574 $this->is_day = true;
575 $this->is_date = true;
579 if ( $qv['monthnum'] ) {
580 if (! $this->is_date) {
581 $this->is_month = true;
582 $this->is_date = true;
587 if (! $this->is_date) {
588 $this->is_year = true;
589 $this->is_date = true;
594 $this->is_date = true;
595 if (strlen($qv['m']) > 9) {
596 $this->is_time = true;
597 } else if (strlen($qv['m']) > 7) {
598 $this->is_day = true;
599 } else if (strlen($qv['m']) > 5) {
600 $this->is_month = true;
602 $this->is_year = true;
606 if ('' != $qv['w']) {
607 $this->is_date = true;
610 if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
611 $this->is_category = false;
613 if (strpos($qv['cat'], '-') !== false) {
614 $this->is_category = false;
616 $this->is_category = true;
620 if ( '' != $qv['category_name'] ) {
621 $this->is_category = true;
624 if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
625 $qv['category__in'] = array();
627 $qv['category__in'] = array_map('absint', $qv['category__in']);
628 $this->is_category = true;
631 if ( !is_array($qv['category__not_in']) || empty($qv['category__not_in']) ) {
632 $qv['category__not_in'] = array();
634 $qv['category__not_in'] = array_map('absint', $qv['category__not_in']);
637 if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
638 $qv['category__and'] = array();
640 $qv['category__and'] = array_map('absint', $qv['category__and']);
641 $this->is_category = true;
644 if ( '' != $qv['tag'] )
645 $this->is_tag = true;
647 $qv['tag_id'] = absint($qv['tag_id']);
648 if ( !empty($qv['tag_id']) )
649 $this->is_tag = true;
651 if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
652 $qv['tag__in'] = array();
654 $qv['tag__in'] = array_map('absint', $qv['tag__in']);
655 $this->is_tag = true;
658 if ( !is_array($qv['tag__not_in']) || empty($qv['tag__not_in']) ) {
659 $qv['tag__not_in'] = array();
661 $qv['tag__not_in'] = array_map('absint', $qv['tag__not_in']);
664 if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
665 $qv['tag__and'] = array();
667 $qv['tag__and'] = array_map('absint', $qv['tag__and']);
668 $this->is_category = true;
671 if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
672 $qv['tag_slug__in'] = array();
674 $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
675 $this->is_tag = true;
678 if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
679 $qv['tag_slug__and'] = array();
681 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
682 $this->is_tag = true;
685 if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
686 $this->is_tax = false;
687 foreach ( $GLOBALS['wp_taxonomies'] as $t ) {
688 if ( isset($t->query_var) && '' != $qv[$t->query_var] ) {
689 $this->is_tax = true;
694 $this->is_tax = true;
697 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
698 $this->is_author = false;
700 $this->is_author = true;
703 if ( '' != $qv['author_name'] ) {
704 $this->is_author = true;
707 if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag ) )
708 $this->is_archive = true;
711 if ( '' != $qv['feed'] )
712 $this->is_feed = true;
714 if ( '' != $qv['tb'] )
715 $this->is_trackback = true;
717 if ( '' != $qv['paged'] )
718 $this->is_paged = true;
720 if ( '' != $qv['comments_popup'] )
721 $this->is_comments_popup = true;
723 // if we're previewing inside the write screen
724 if ('' != $qv['preview'])
725 $this->is_preview = true;
728 $this->is_admin = true;
730 if ( false !== strpos($qv['feed'], 'comments-') ) {
731 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
732 $qv['withcomments'] = 1;
735 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
737 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
738 $this->is_comment_feed = true;
740 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 ) )
741 $this->is_home = true;
743 // Correct is_* for page_on_front and page_for_posts
744 if ( $this->is_home && ( empty($this->query) || $qv['preview'] == 'true' ) && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
745 $this->is_page = true;
746 $this->is_home = false;
747 $qv['page_id'] = get_option('page_on_front');
750 if ( '' != $qv['pagename'] ) {
751 $this->queried_object =& get_page_by_path($qv['pagename']);
752 if ( !empty($this->queried_object) )
753 $this->queried_object_id = (int) $this->queried_object->ID;
755 unset($this->queried_object);
757 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
758 $this->is_page = false;
759 $this->is_home = true;
760 $this->is_posts_page = true;
764 if ( $qv['page_id'] ) {
765 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
766 $this->is_page = false;
767 $this->is_home = true;
768 $this->is_posts_page = true;
772 if ( !empty($qv['post_type']) )
773 $qv['post_type'] = sanitize_user($qv['post_type'], true);
775 if ( !empty($qv['post_status']) )
776 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
778 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
779 $this->is_comment_feed = false;
781 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
782 // Done correcting is_* for page_on_front and page_for_posts
784 if ('404' == $qv['error'])
787 if ( !empty($query) )
788 do_action_ref_array('parse_query', array(&$this));
792 $is_feed = $this->is_feed;
794 $this->init_query_flags();
795 $this->is_404 = true;
797 $this->is_feed = $is_feed;
800 function get($query_var) {
801 if (isset($this->query_vars[$query_var])) {
802 return $this->query_vars[$query_var];
808 function set($query_var, $value) {
809 $this->query_vars[$query_var] = $value;
812 function &get_posts() {
813 global $wpdb, $user_ID;
815 do_action_ref_array('pre_get_posts', array(&$this));
818 $q = &$this->query_vars;
820 $q = $this->fill_query_vars($q);
822 // First let's clear some variables
832 $fields = "$wpdb->posts.*";
833 $post_status_join = false;
836 if ( !isset($q['suppress_filters']) )
837 $q['suppress_filters'] = false;
839 if ( !isset($q['post_type']) ) {
840 if ( $this->is_search )
841 $q['post_type'] = 'any';
843 $q['post_type'] = 'post';
845 $post_type = $q['post_type'];
846 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
847 $q['posts_per_page'] = get_option('posts_per_page');
848 if ( isset($q['showposts']) && $q['showposts'] ) {
849 $q['showposts'] = (int) $q['showposts'];
850 $q['posts_per_page'] = $q['showposts'];
852 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
853 $q['posts_per_page'] = $q['posts_per_archive_page'];
854 if ( !isset($q['nopaging']) ) {
855 if ($q['posts_per_page'] == -1) {
856 $q['nopaging'] = true;
858 $q['nopaging'] = false;
861 if ( $this->is_feed ) {
862 $q['posts_per_page'] = get_option('posts_per_rss');
863 $q['nopaging'] = false;
865 $q['posts_per_page'] = (int) $q['posts_per_page'];
866 if ( $q['posts_per_page'] < -1 )
867 $q['posts_per_page'] = abs($q['posts_per_page']);
868 else if ( $q['posts_per_page'] == 0 )
869 $q['posts_per_page'] = 1;
871 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
872 $this->is_page = true;
873 $this->is_home = false;
874 $q['page_id'] = get_option('page_on_front');
877 if (isset($q['page'])) {
878 $q['page'] = trim($q['page'], '/');
879 $q['page'] = absint($q['page']);
882 // If a month is specified in the querystring, load that month
884 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
885 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
886 if (strlen($q['m'])>5)
887 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
888 if (strlen($q['m'])>7)
889 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
890 if (strlen($q['m'])>9)
891 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
892 if (strlen($q['m'])>11)
893 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
894 if (strlen($q['m'])>13)
895 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
898 if ( '' !== $q['hour'] )
899 $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
901 if ( '' !== $q['minute'] )
902 $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
904 if ( '' !== $q['second'] )
905 $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
908 $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
910 if ( $q['monthnum'] )
911 $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
914 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
916 if ('' != $q['name']) {
917 $q['name'] = sanitize_title($q['name']);
918 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
919 } else if ('' != $q['pagename']) {
920 if ( isset($this->queried_object_id) )
921 $reqpage = $this->queried_object_id;
923 $reqpage = get_page_by_path($q['pagename']);
924 if ( !empty($reqpage) )
925 $reqpage = $reqpage->ID;
930 $page_for_posts = get_option('page_for_posts');
931 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
932 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
933 $page_paths = '/' . trim($q['pagename'], '/');
934 $q['pagename'] = sanitize_title(basename($page_paths));
935 $q['name'] = $q['pagename'];
936 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
937 $reqpage_obj = get_page($reqpage);
938 if ( 'attachment' == $reqpage_obj->post_type ) {
939 $this->is_attachment = true;
940 $this->is_page = true;
941 $q['attachment_id'] = $reqpage;
944 } elseif ('' != $q['attachment']) {
945 $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
946 $attach_paths = '/' . trim($q['attachment'], '/');
947 $q['attachment'] = sanitize_title(basename($attach_paths));
948 $q['name'] = $q['attachment'];
949 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
953 $where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'";
955 if ( intval($q['comments_popup']) )
956 $q['p'] = absint($q['comments_popup']);
958 // If an attachment is requested by number, let it supercede any post number.
959 if ( $q['attachment_id'] )
960 $q['p'] = absint($q['attachment_id']);
962 // If a post number is specified, load that post
964 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
965 } elseif ( $q['post__in'] ) {
966 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
967 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
968 } elseif ( $q['post__not_in'] ) {
969 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
970 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
973 if ( $q['post_parent'] )
974 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
976 if ( $q['page_id'] ) {
977 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
978 $q['p'] = $q['page_id'];
979 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
983 // If a search pattern is specified, load the posts that match
984 if ( !empty($q['s']) ) {
985 // added slashes screw with quote grouping when done early, so done later
986 $q['s'] = stripslashes($q['s']);
987 if ($q['sentence']) {
988 $q['search_terms'] = array($q['s']);
990 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches);
991 $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
993 $n = ($q['exact']) ? '' : '%';
995 foreach((array)$q['search_terms'] as $term) {
996 $term = addslashes_gpc($term);
997 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
998 $searchand = ' AND ';
1000 $term = $wpdb->escape($q['s']);
1001 if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
1002 $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
1004 if ( !empty($search) )
1005 $search = " AND ({$search}) ";
1010 if ( empty($q['cat']) || ($q['cat'] == '0') ||
1011 // Bypass cat checks if fetching specific posts
1012 $this->is_singular ) {
1015 $q['cat'] = ''.urldecode($q['cat']).'';
1016 $q['cat'] = addslashes_gpc($q['cat']);
1017 $cat_array = preg_split('/[,\s]+/', $q['cat']);
1019 $req_cats = array();
1020 foreach ( $cat_array as $cat ) {
1021 $cat = intval($cat);
1026 $q['category__in'][] = $cat;
1027 $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
1029 $q['category__not_in'][] = $cat;
1030 $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
1033 $q['cat'] = implode(',', $req_cats);
1036 if ( !empty($q['category__in']) || !empty($q['category__not_in']) || !empty($q['category__and']) ) {
1037 $groupby = "{$wpdb->posts}.ID";
1040 if ( !empty($q['category__in']) ) {
1041 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
1042 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
1043 $include_cats = "'" . implode("', '", $q['category__in']) . "'";
1044 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
1047 if ( !empty($q['category__not_in']) ) {
1048 $ids = get_objects_in_term($q['category__not_in'], 'category');
1049 if ( is_wp_error( $ids ) )
1051 if ( is_array($ids) && count($ids > 0) ) {
1052 $out_posts = "'" . implode("', '", $ids) . "'";
1053 $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
1057 // Category stuff for nice URLs
1058 if ( '' != $q['category_name'] && !$this->is_singular ) {
1059 $reqcat = get_category_by_path($q['category_name']);
1060 $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
1061 $cat_paths = '/' . trim($q['category_name'], '/');
1062 $q['category_name'] = sanitize_title(basename($cat_paths));
1064 $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
1065 $q['category_name'] = sanitize_title(basename($cat_paths));
1066 $cat_paths = explode('/', $cat_paths);
1068 foreach ( (array) $cat_paths as $pathdir )
1069 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
1071 //if we don't match the entire hierarchy fallback on just matching the nicename
1072 if ( empty($reqcat) )
1073 $reqcat = get_category_by_path($q['category_name'], false);
1075 if ( !empty($reqcat) )
1076 $reqcat = $reqcat->term_id;
1080 $q['cat'] = $reqcat;
1082 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
1083 $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
1084 $in_cats = array($q['cat']);
1085 $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
1086 $in_cats = "'" . implode("', '", $in_cats) . "'";
1087 $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
1088 $groupby = "{$wpdb->posts}.ID";
1092 if ( '' != $q['tag'] ) {
1093 if ( strpos($q['tag'], ',') !== false ) {
1094 $tags = preg_split('/[,\s]+/', $q['tag']);
1095 foreach ( (array) $tags as $tag ) {
1096 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1097 $q['tag_slug__in'][] = $tag;
1099 } else if ( preg_match('/[+\s]+/', $q['tag']) ) {
1100 $tags = preg_split('/[+\s]+/', $q['tag']);
1101 foreach ( (array) $tags as $tag ) {
1102 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
1103 $q['tag_slug__and'][] = $tag;
1106 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1107 $q['tag_slug__in'][] = $q['tag'];
1111 if ( !empty($q['tag__in']) || !empty($q['tag__not_in']) || !empty($q['tag__and']) ||
1112 !empty($q['tag_slug__in']) || !empty($q['tag_slug__and']) ) {
1113 $groupby = "{$wpdb->posts}.ID";
1116 if ( !empty($q['tag__in']) ) {
1117 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
1118 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1119 $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
1120 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
1121 $reqtag = is_term( $q['tag__in'][0], 'post_tag' );
1122 if ( !empty($reqtag) )
1123 $q['tag_id'] = $reqtag['term_id'];
1126 if ( !empty($q['tag_slug__in']) ) {
1127 $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) ";
1128 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1129 $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
1130 $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
1131 $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
1132 if ( !empty($reqtag) )
1133 $q['tag_id'] = $reqtag->term_id;
1136 if ( !empty($q['tag__not_in']) ) {
1137 $ids = get_objects_in_term($q['tag__not_in'], 'post_tag');
1138 if ( is_array($ids) && count($ids > 0) ) {
1139 $out_posts = "'" . implode("', '", $ids) . "'";
1140 $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
1144 // Tag and slug intersections.
1145 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag');
1146 foreach ($intersections as $item => $taxonomy) {
1147 if ( empty($q[$item]) ) continue;
1149 if ( $item != 'category__and' ) {
1150 $reqtag = is_term( $q[$item][0], 'post_tag' );
1151 if ( !empty($reqtag) )
1152 $q['tag_id'] = $reqtag['term_id'];
1155 $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id';
1157 $q[$item] = array_unique($q[$item]);
1158 $tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
1159 $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
1160 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
1162 $post_ids = $wpdb->get_col($tsql);
1164 if ( count($post_ids) )
1165 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
1167 $whichcat = " AND 0 = 1";
1173 if ( $this->is_tax ) {
1174 if ( '' != $q['taxonomy'] ) {
1175 $taxonomy = $q['taxonomy'];
1176 $tt[$taxonomy] = $q['term'];
1177 $terms = get_terms($q['taxonomy'], array('slug'=>$q['term']));
1179 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
1180 if ( isset($t->query_var) && '' != $q[$t->query_var] ) {
1181 $terms = get_terms($taxonomy, array('slug'=>$q[$t->query_var]));
1182 if ( !is_wp_error($terms) )
1187 if ( is_wp_error($terms) || empty($terms) ) {
1188 $whichcat = " AND 0 ";
1190 foreach ( $terms as $term )
1191 $term_ids[] = $term->term_id;
1192 $post_ids = get_objects_in_term($term_ids, $taxonomy);
1193 if ( !is_wp_error($post_ids) && count($post_ids) ) {
1194 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
1196 $q['post_status'] = 'publish';
1197 $post_status_join = true;
1199 $whichcat = " AND 0 ";
1204 // Author/user stuff
1206 if ( empty($q['author']) || ($q['author'] == '0') ) {
1209 $q['author'] = ''.urldecode($q['author']).'';
1210 $q['author'] = addslashes_gpc($q['author']);
1211 if (strpos($q['author'], '-') !== false) {
1214 $q['author'] = explode('-', $q['author']);
1215 $q['author'] = '' . absint($q['author'][1]);
1220 $author_array = preg_split('/[,\s]+/', $q['author']);
1221 $whichauthor .= " AND ($wpdb->posts.post_author ".$eq.' '.absint($author_array[0]);
1222 for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
1223 $whichauthor .= ' '.$andor." $wpdb->posts.post_author ".$eq.' '.absint($author_array[$i]);
1225 $whichauthor .= ')';
1228 // Author stuff for nice URLs
1230 if ('' != $q['author_name']) {
1231 if (strpos($q['author_name'], '/') !== false) {
1232 $q['author_name'] = explode('/',$q['author_name']);
1233 if ($q['author_name'][count($q['author_name'])-1]) {
1234 $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
1236 $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
1239 $q['author_name'] = sanitize_title($q['author_name']);
1240 $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
1241 $whichauthor .= " AND ($wpdb->posts.post_author = ".absint($q['author']).')';
1244 // MIME-Type stuff for attachment browsing
1246 if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] )
1247 $whichmimetype = wp_post_mime_type_where($q['post_mime_type']);
1249 $where .= $search.$whichcat.$whichauthor.$whichmimetype;
1251 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
1252 $q['order'] = 'DESC';
1255 if ( empty($q['orderby']) ) {
1256 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
1258 // Used to filter values
1259 $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand');
1260 $q['orderby'] = urldecode($q['orderby']);
1261 $q['orderby'] = addslashes_gpc($q['orderby']);
1262 $orderby_array = explode(' ',$q['orderby']);
1263 if ( empty($orderby_array) )
1264 $orderby_array[] = $q['orderby'];
1266 for ($i = 0; $i < count($orderby_array); $i++) {
1267 // Only allow certain values for safety
1268 $orderby = $orderby_array[$i];
1273 $orderby = "$wpdb->posts.ID";
1276 $orderby = 'RAND()';
1279 $orderby = "$wpdb->posts.post_" . $orderby;
1281 if ( in_array($orderby_array[$i], $allowed_keys) )
1282 $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
1284 // append ASC or DESC at the end
1285 if ( !empty($q['orderby']))
1286 $q['orderby'] .= " {$q['order']}";
1288 if ( empty($q['orderby']) )
1289 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
1292 if ( $this->is_attachment ) {
1293 $where .= " AND $wpdb->posts.post_type = 'attachment'";
1294 } elseif ($this->is_page) {
1295 $where .= " AND $wpdb->posts.post_type = 'page'";
1296 } elseif ($this->is_single) {
1297 $where .= " AND $wpdb->posts.post_type = 'post'";
1298 } elseif ( 'any' == $post_type ) {
1301 $where .= " AND $wpdb->posts.post_type = '$post_type'";
1304 if ( isset($q['post_status']) && '' != $q['post_status'] ) {
1305 $statuswheres = array();
1306 $q_status = explode(',', $q['post_status']);
1307 $r_status = array();
1308 $p_status = array();
1309 if ( in_array( 'draft' , $q_status ) )
1310 $r_status[] = "$wpdb->posts.post_status = 'draft'";
1311 if ( in_array( 'pending', $q_status ) )
1312 $r_status[] = "$wpdb->posts.post_status = 'pending'";
1313 if ( in_array( 'future' , $q_status ) )
1314 $r_status[] = "$wpdb->posts.post_status = 'future'";
1315 if ( in_array( 'inherit' , $q_status ) )
1316 $r_status[] = "$wpdb->posts.post_status = 'inherit'";
1317 if ( in_array( 'private', $q_status ) )
1318 $p_status[] = "$wpdb->posts.post_status = 'private'";
1319 if ( in_array( 'publish', $q_status ) )
1320 $r_status[] = "$wpdb->posts.post_status = 'publish'";
1322 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
1323 $r_status = array_merge($r_status, $p_status);
1327 if ( !empty($r_status) ) {
1328 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type}s") )
1329 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
1331 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
1333 if ( !empty($p_status) ) {
1334 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type}s") )
1335 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
1337 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
1339 if ( $post_status_join ) {
1340 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
1341 foreach ( $statuswheres as $index => $statuswhere )
1342 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
1344 foreach ( $statuswheres as $statuswhere )
1345 $where .= " AND $statuswhere";
1346 } elseif ( !$this->is_singular ) {
1347 $where .= " AND ($wpdb->posts.post_status = 'publish'";
1350 $where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'";
1352 if ( is_user_logged_in() ) {
1353 $where .= current_user_can( "read_private_{$post_type}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'";
1360 if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
1361 $join .= " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
1362 if ( ! empty($q['meta_key']) )
1363 $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
1364 if ( ! empty($q['meta_value']) )
1365 $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value = %s ", $q['meta_value']);
1367 // Apply filters on where and join prior to paging so that any
1368 // manipulations to them are reflected in the paging by day queries.
1369 if ( !$q['suppress_filters'] ) {
1370 $where = apply_filters('posts_where', $where);
1371 $join = apply_filters('posts_join', $join);
1375 if ( empty($q['nopaging']) && !$this->is_singular ) {
1376 $page = absint($q['paged']);
1381 if ( empty($q['offset']) ) {
1383 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
1384 $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
1385 } else { // we're ignoring $page and using 'offset'
1386 $q['offset'] = absint($q['offset']);
1387 $pgstrt = $q['offset'] . ', ';
1388 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
1393 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
1394 if ( $this->is_archive || $this->is_search ) {
1395 $cjoin = "LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
1396 $cwhere = "WHERE comment_approved = '1' $where";
1397 $cgroupby = "GROUP BY $wpdb->comments.comment_id";
1398 } else { // Other non singular e.g. front
1399 $cjoin = "LEFT JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
1400 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
1404 if ( !$q['suppress_filters'] ) {
1405 $cjoin = apply_filters('comment_feed_join', $cjoin);
1406 $cwhere = apply_filters('comment_feed_where', $cwhere);
1407 $cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
1410 $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss'));
1411 $this->comment_count = count($this->comments);
1413 $post_ids = array();
1415 foreach ($this->comments as $comment)
1416 $post_ids[] = (int) $comment->comment_post_ID;
1418 $post_ids = join(',', $post_ids);
1421 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
1426 $orderby = $q['orderby'];
1428 // Apply post-paging filters on where and join. Only plugins that
1429 // manipulate paging queries should use these hooks.
1430 if ( !$q['suppress_filters'] ) {
1431 $where = apply_filters('posts_where_paged', $where);
1432 $groupby = apply_filters('posts_groupby', $groupby);
1433 $join = apply_filters('posts_join_paged', $join);
1434 $orderby = apply_filters('posts_orderby', $orderby);
1435 $distinct = apply_filters('posts_distinct', $distinct);
1436 $fields = apply_filters('posts_fields', $fields);
1437 $limits = apply_filters( 'post_limits', $limits );
1440 // Announce current selection parameters. For use by caching plugins.
1441 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
1443 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
1444 if ( !$q['suppress_filters'] ) {
1445 $where = apply_filters('posts_where_request', $where);
1446 $groupby = apply_filters('posts_groupby_request', $groupby);
1447 $join = apply_filters('posts_join_request', $join);
1448 $orderby = apply_filters('posts_orderby_request', $orderby);
1449 $distinct = apply_filters('posts_distinct_request', $distinct);
1450 $fields = apply_filters('posts_fields_request', $fields);
1451 $limits = apply_filters( 'post_limits_request', $limits );
1454 if ( ! empty($groupby) )
1455 $groupby = 'GROUP BY ' . $groupby;
1456 if ( !empty( $orderby ) )
1457 $orderby = 'ORDER BY ' . $orderby;
1459 if ( !empty($limits) )
1460 $found_rows = 'SQL_CALC_FOUND_ROWS';
1462 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
1463 if ( !$q['suppress_filters'] )
1464 $this->request = apply_filters('posts_request', $this->request);
1466 $this->posts = $wpdb->get_results($this->request);
1467 // Raw results filter. Prior to status checks.
1468 if ( !$q['suppress_filters'] )
1469 $this->posts = apply_filters('posts_results', $this->posts);
1471 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
1472 $cjoin = apply_filters('comment_feed_join', '');
1473 $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'");
1474 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss');
1475 $this->comments = $wpdb->get_results($comments_request);
1476 $this->comment_count = count($this->comments);
1479 if ( !empty($limits) ) {
1480 $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
1481 $this->found_posts = $wpdb->get_var( $found_posts_query );
1482 $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
1483 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
1486 // Check post status to determine if post should be displayed.
1487 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
1488 $status = get_post_status($this->posts[0]);
1489 //$type = get_post_type($this->posts[0]);
1490 if ( ('publish' != $status) ) {
1491 if ( ! is_user_logged_in() ) {
1492 // User must be logged in to view unpublished posts.
1493 $this->posts = array();
1495 if (in_array($status, array('draft', 'pending')) ) {
1496 // User must have edit permissions on the draft to preview.
1497 if (! current_user_can('edit_post', $this->posts[0]->ID)) {
1498 $this->posts = array();
1500 $this->is_preview = true;
1501 $this->posts[0]->post_date = current_time('mysql');
1503 } else if ('future' == $status) {
1504 $this->is_preview = true;
1505 if (!current_user_can('edit_post', $this->posts[0]->ID)) {
1506 $this->posts = array ( );
1509 if (! current_user_can('read_post', $this->posts[0]->ID))
1510 $this->posts = array();
1516 if ( !$q['suppress_filters'] )
1517 $this->posts = apply_filters('the_posts', $this->posts);
1519 update_post_caches($this->posts);
1521 $this->post_count = count($this->posts);
1522 if ($this->post_count > 0) {
1523 $this->post = $this->posts[0];
1526 return $this->posts;
1529 function next_post() {
1531 $this->current_post++;
1533 $this->post = $this->posts[$this->current_post];
1537 function the_post() {
1539 $this->in_the_loop = true;
1540 $post = $this->next_post();
1541 setup_postdata($post);
1543 if ( $this->current_post == 0 ) // loop has just started
1544 do_action('loop_start');
1547 function have_posts() {
1548 if ($this->current_post + 1 < $this->post_count) {
1550 } elseif ($this->current_post + 1 == $this->post_count && $this->post_count > 0) {
1551 do_action('loop_end');
1552 // Do some cleaning up after the loop
1553 $this->rewind_posts();
1556 $this->in_the_loop = false;
1560 function rewind_posts() {
1561 $this->current_post = -1;
1562 if ($this->post_count > 0) {
1563 $this->post = $this->posts[0];
1567 function next_comment() {
1568 $this->current_comment++;
1570 $this->comment = $this->comments[$this->current_comment];
1571 return $this->comment;
1574 function the_comment() {
1577 $comment = $this->next_comment();
1579 if ($this->current_comment == 0) {
1580 do_action('comment_loop_start');
1584 function have_comments() {
1585 if ($this->current_comment + 1 < $this->comment_count) {
1587 } elseif ($this->current_comment + 1 == $this->comment_count) {
1588 $this->rewind_comments();
1594 function rewind_comments() {
1595 $this->current_comment = -1;
1596 if ($this->comment_count > 0) {
1597 $this->comment = $this->comments[0];
1601 function &query($query) {
1602 $this->parse_query($query);
1603 return $this->get_posts();
1606 function get_queried_object() {
1607 if (isset($this->queried_object)) {
1608 return $this->queried_object;
1611 $this->queried_object = NULL;
1612 $this->queried_object_id = 0;
1614 if ($this->is_category) {
1615 $cat = $this->get('cat');
1616 $category = &get_category($cat);
1617 $this->queried_object = &$category;
1618 $this->queried_object_id = (int) $cat;
1619 } else if ($this->is_tag) {
1620 $tag_id = $this->get('tag_id');
1621 $tag = &get_term($tag_id, 'post_tag');
1622 if ( is_wp_error( $tag ) )
1624 $this->queried_object = &$tag;
1625 $this->queried_object_id = (int) $tag_id;
1626 } else if ($this->is_tax) {
1627 $tax = $this->get('taxonomy');
1628 $slug = $this->get('term');
1629 $term = &get_terms($tax, array('slug'=>$slug));
1630 if ( is_wp_error($term) || empty($term) )
1633 $this->queried_object = $term;
1634 $this->queried_object_id = $term->term_id;
1635 } else if ($this->is_posts_page) {
1636 $this->queried_object = & get_page(get_option('page_for_posts'));
1637 $this->queried_object_id = (int) $this->queried_object->ID;
1638 } else if ($this->is_single) {
1639 $this->queried_object = $this->post;
1640 $this->queried_object_id = (int) $this->post->ID;
1641 } else if ($this->is_page) {
1642 $this->queried_object = $this->post;
1643 $this->queried_object_id = (int) $this->post->ID;
1644 } else if ($this->is_author) {
1645 $author_id = (int) $this->get('author');
1646 $author = get_userdata($author_id);
1647 $this->queried_object = $author;
1648 $this->queried_object_id = $author_id;
1651 return $this->queried_object;
1654 function get_queried_object_id() {
1655 $this->get_queried_object();
1657 if (isset($this->queried_object_id)) {
1658 return $this->queried_object_id;
1664 function WP_Query ($query = '') {
1665 if (! empty($query)) {
1666 $this->query($query);
1672 // Redirect old slugs
1673 function wp_old_slug_redirect () {
1675 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
1678 $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND meta_key = '_wp_old_slug' AND meta_value='" . $wp_query->query_vars['name'] . "'";
1680 // if year, monthnum, or day have been specified, make our query more precise
1681 // just in case there are multiple identical _wp_old_slug values
1682 if ( '' != $wp_query->query_vars['year'] )
1683 $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
1684 if ( '' != $wp_query->query_vars['monthnum'] )
1685 $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
1686 if ( '' != $wp_query->query_vars['day'] )
1687 $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
1689 $id = (int) $wpdb->get_var($query);
1694 $link = get_permalink($id);
1699 wp_redirect($link, '301'); // Permanent redirect
1706 // Private helper functions
1709 // Setup global post data.
1710 function setup_postdata($post) {
1711 global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
1713 $id = (int) $post->ID;
1715 $authordata = get_userdata($post->post_author);
1717 $day = mysql2date('d.m.y', $post->post_date);
1718 $currentmonth = mysql2date('m', $post->post_date);
1720 $page = get_query_var('page');
1723 if ( is_single() || is_page() || is_feed() )
1725 $content = $post->post_content;
1726 if ( preg_match('/<!--nextpage-->/', $content) ) {
1730 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
1731 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
1732 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
1733 $pages = explode('<!--nextpage-->', $content);
1734 $numpages = count($pages);
1736 $pages[0] = $post->post_content;