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'];
34 function is_admin () {
35 if ( defined('WP_ADMIN') )
40 function is_archive () {
43 return $wp_query->is_archive;
46 function is_attachment () {
49 return $wp_query->is_attachment;
52 function is_author ($author = '') {
55 if ( !$wp_query->is_author )
61 $author_obj = $wp_query->get_queried_object();
63 if ( $author == $author_obj->ID )
65 elseif ( $author == $author_obj->nickname )
67 elseif ( $author == $author_obj->user_nicename )
73 function is_category ($category = '') {
76 if ( !$wp_query->is_category )
79 if ( empty($category) )
82 $cat_obj = $wp_query->get_queried_object();
84 if ( $category == $cat_obj->term_id )
86 else if ( $category == $cat_obj->name )
88 elseif ( $category == $cat_obj->slug )
94 function is_tag( $slug = '' ) {
96 if ( !$wp_query->is_tag )
102 $tag_obj = $wp_query->get_queried_object();
103 if ( $slug == $tag_obj->slug )
108 function is_comments_popup () {
111 return $wp_query->is_comments_popup;
114 function is_date () {
117 return $wp_query->is_date;
123 return $wp_query->is_day;
126 function is_feed () {
129 return $wp_query->is_feed;
132 function is_home () {
135 return $wp_query->is_home;
138 function is_month () {
141 return $wp_query->is_month;
144 function is_page ($page = '') {
147 if ( !$wp_query->is_page )
153 $page_obj = $wp_query->get_queried_object();
155 if ( $page == $page_obj->ID )
157 elseif ( $page == $page_obj->post_title )
159 else if ( $page == $page_obj->post_name )
165 function is_paged () {
168 return $wp_query->is_paged;
171 function is_plugin_page() {
174 if ( isset($plugin_page) )
180 function is_preview() {
183 return $wp_query->is_preview;
186 function is_robots() {
189 return $wp_query->is_robots;
192 function is_search () {
195 return $wp_query->is_search;
198 function is_single ($post = '') {
201 if ( !$wp_query->is_single )
207 $post_obj = $wp_query->get_queried_object();
209 if ( $post == $post_obj->ID )
211 elseif ( $post == $post_obj->post_title )
213 elseif ( $post == $post_obj->post_name )
219 function is_singular() {
222 return $wp_query->is_singular;
225 function is_time () {
228 return $wp_query->is_time;
231 function is_trackback () {
234 return $wp_query->is_trackback;
237 function is_year () {
240 return $wp_query->is_year;
246 return $wp_query->is_404;
250 * The Loop. Post loop control.
253 function have_posts() {
256 return $wp_query->have_posts();
259 function in_the_loop() {
262 return $wp_query->in_the_loop;
265 function rewind_posts() {
268 return $wp_query->rewind_posts();
271 function the_post() {
274 $wp_query->the_post();
281 function have_comments() {
283 return $wp_query->have_comments();
286 function the_comment() {
288 return $wp_query->the_comment();
297 var $query_vars = array();
299 var $queried_object_id;
304 var $current_post = -1;
305 var $in_the_loop = false;
309 var $comment_count = 0;
310 var $current_comment = -1;
313 var $found_posts = 0;
314 var $max_num_pages = 0;
316 var $is_single = false;
317 var $is_preview = false;
318 var $is_page = false;
319 var $is_archive = false;
320 var $is_date = false;
321 var $is_year = false;
322 var $is_month = false;
324 var $is_time = false;
325 var $is_author = false;
326 var $is_category = false;
328 var $is_search = false;
329 var $is_feed = false;
330 var $is_comment_feed = false;
331 var $is_trackback = false;
332 var $is_home = false;
334 var $is_comments_popup = false;
335 var $is_admin = false;
336 var $is_attachment = false;
337 var $is_singular = false;
338 var $is_robots = false;
339 var $is_posts_page = false;
341 function init_query_flags() {
342 $this->is_single = false;
343 $this->is_page = false;
344 $this->is_archive = false;
345 $this->is_date = false;
346 $this->is_year = false;
347 $this->is_month = false;
348 $this->is_day = false;
349 $this->is_time = false;
350 $this->is_author = false;
351 $this->is_category = false;
352 $this->is_tag = false;
353 $this->is_search = false;
354 $this->is_feed = false;
355 $this->is_comment_feed = false;
356 $this->is_trackback = false;
357 $this->is_home = false;
358 $this->is_404 = false;
359 $this->is_paged = false;
360 $this->is_admin = false;
361 $this->is_attachment = false;
362 $this->is_singular = false;
363 $this->is_robots = false;
364 $this->is_posts_page = false;
370 $this->query_vars = array();
371 unset($this->queried_object);
372 unset($this->queried_object_id);
373 $this->post_count = 0;
374 $this->current_post = -1;
375 $this->in_the_loop = false;
377 $this->init_query_flags();
380 // Reparse the query vars.
381 function parse_query_vars() {
382 $this->parse_query('');
385 function fill_query_vars($array) {
417 foreach ($keys as $key) {
418 if ( !isset($array[$key]))
422 $array_keys = array('category__in', 'category__not_in', 'category__and',
423 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
425 foreach ( $array_keys as $key ) {
426 if ( !isset($array[$key]))
427 $array[$key] = array();
432 // Parse a query string and set query type booleans.
433 function parse_query ($query) {
434 if ( !empty($query) || !isset($this->query) ) {
436 if ( is_array($query) )
437 $this->query_vars = $query;
439 parse_str($query, $this->query_vars);
440 $this->query = $query;
443 $this->query_vars = $this->fill_query_vars($this->query_vars);
444 $qv = &$this->query_vars;
446 if ( ! empty($qv['robots']) )
447 $this->is_robots = true;
449 $qv['p'] = (int) $qv['p'];
450 $qv['page_id'] = (int) $qv['page_id'];
451 $qv['year'] = (int) $qv['year'];
452 $qv['monthnum'] = (int) $qv['monthnum'];
453 $qv['day'] = (int) $qv['day'];
454 $qv['w'] = (int) $qv['w'];
455 $qv['m'] = (int) $qv['m'];
456 if ( '' !== $qv['hour'] ) $qv['hour'] = (int) $qv['hour'];
457 if ( '' !== $qv['minute'] ) $qv['minute'] = (int) $qv['minute'];
458 if ( '' !== $qv['second'] ) $qv['second'] = (int) $qv['second'];
460 // Compat. Map subpost to attachment.
461 if ( '' != $qv['subpost'] )
462 $qv['attachment'] = $qv['subpost'];
463 if ( '' != $qv['subpost_id'] )
464 $qv['attachment_id'] = $qv['subpost_id'];
466 $qv['attachment_id'] = (int) $qv['attachment_id'];
468 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
469 $this->is_single = true;
470 $this->is_attachment = true;
471 } elseif ( '' != $qv['name'] ) {
472 $this->is_single = true;
473 } elseif ( $qv['p'] ) {
474 $this->is_single = true;
475 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
476 // If year, month, day, hour, minute, and second are set, a single
477 // post is being queried.
478 $this->is_single = true;
479 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
480 $this->is_page = true;
481 $this->is_single = false;
482 } elseif ( !empty($qv['s']) ) {
483 $this->is_search = true;
485 // Look for archive queries. Dates, categories, authors.
487 if ( '' !== $qv['second'] ) {
488 $this->is_time = true;
489 $this->is_date = true;
492 if ( '' !== $qv['minute'] ) {
493 $this->is_time = true;
494 $this->is_date = true;
497 if ( '' !== $qv['hour'] ) {
498 $this->is_time = true;
499 $this->is_date = true;
503 if (! $this->is_date) {
504 $this->is_day = true;
505 $this->is_date = true;
509 if ( $qv['monthnum'] ) {
510 if (! $this->is_date) {
511 $this->is_month = true;
512 $this->is_date = true;
517 if (! $this->is_date) {
518 $this->is_year = true;
519 $this->is_date = true;
524 $this->is_date = true;
525 if (strlen($qv['m']) > 9) {
526 $this->is_time = true;
527 } else if (strlen($qv['m']) > 7) {
528 $this->is_day = true;
529 } else if (strlen($qv['m']) > 5) {
530 $this->is_month = true;
532 $this->is_year = true;
536 if ('' != $qv['w']) {
537 $this->is_date = true;
540 if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
541 $this->is_category = false;
543 if (strpos($qv['cat'], '-') !== false) {
544 $this->is_category = false;
546 $this->is_category = true;
550 if ( '' != $qv['category_name'] ) {
551 $this->is_category = true;
554 if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
555 $qv['category__in'] = array();
557 $qv['category__in'] = array_map('intval', $qv['category__in']);
558 $this->is_category = true;
561 if ( !is_array($qv['category___not_in']) || empty($qv['category__not_in']) ) {
562 $qv['category__not_in'] = array();
564 $qv['category__not_in'] = array_map('intval', $qv['category__not_in']);
567 if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
568 $qv['category__and'] = array();
570 $qv['category__and'] = array_map('intval', $qv['category__and']);
571 $this->is_category = true;
574 if ( '' != $qv['tag'] )
575 $this->is_tag = true;
577 $qv['tag_id'] = (int) $qv['tag_id'];
578 if ( !empty($qv['tag_id']) )
579 $this->is_tag = true;
581 if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
582 $qv['tag__in'] = array();
584 $qv['tag__in'] = array_map('intval', $qv['tag__in']);
585 $this->is_tag = true;
588 if ( !is_array($qv['tag___not_in']) || empty($qv['tag__not_in']) ) {
589 $qv['tag__not_in'] = array();
591 $qv['tag__not_in'] = array_map('intval', $qv['tag__not_in']);
594 if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
595 $qv['tag__and'] = array();
597 $qv['tag__and'] = array_map('intval', $qv['tag__and']);
598 $this->is_category = true;
601 if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
602 $qv['tag_slug__in'] = array();
604 $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
605 $this->is_tag = true;
608 if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
609 $qv['tag_slug__and'] = array();
611 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
612 $this->is_tag = true;
615 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
616 $this->is_author = false;
618 $this->is_author = true;
621 if ( '' != $qv['author_name'] ) {
622 $this->is_author = true;
625 if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag ) )
626 $this->is_archive = true;
629 if ( '' != $qv['feed'] )
630 $this->is_feed = true;
632 if ( '' != $qv['tb'] )
633 $this->is_trackback = true;
635 if ( '' != $qv['paged'] )
636 $this->is_paged = true;
638 if ( '' != $qv['comments_popup'] )
639 $this->is_comments_popup = true;
641 // if we're previewing inside the write screen
642 if ('' != $qv['preview'])
643 $this->is_preview = true;
646 $this->is_admin = true;
648 if ( false !== strpos($qv['feed'], 'comments-') ) {
649 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
650 $qv['withcomments'] = 1;
653 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
655 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
656 $this->is_comment_feed = true;
658 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 ) )
659 $this->is_home = true;
661 // Correct is_* for page_on_front and page_for_posts
662 if ( $this->is_home && ( empty($this->query) || $qv['preview'] == 'true' ) && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
663 $this->is_page = true;
664 $this->is_home = false;
665 $qv['page_id'] = get_option('page_on_front');
668 if ( '' != $qv['pagename'] ) {
669 $this->queried_object =& get_page_by_path($qv['pagename']);
670 if ( !empty($this->queried_object) )
671 $this->queried_object_id = (int) $this->queried_object->ID;
673 unset($this->queried_object);
675 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
676 $this->is_page = false;
677 $this->is_home = true;
678 $this->is_posts_page = true;
682 if ( $qv['page_id'] ) {
683 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
684 $this->is_page = false;
685 $this->is_home = true;
686 $this->is_posts_page = true;
690 if ( !empty($qv['post_type']) )
691 $qv['post_type'] = sanitize_user($qv['post_type'], true);
693 if ( !empty($qv['post_status']) )
694 $qv['post_status'] = sanitize_user($qv['post_status'], true);
696 if ( $this->is_posts_page && !$qv['withcomments'] )
697 $this->is_comment_feed = false;
699 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
700 // Done correcting is_* for page_on_front and page_for_posts
702 if ('404' == $qv['error'])
705 if ( !empty($query) )
706 do_action_ref_array('parse_query', array(&$this));
710 $is_feed = $this->is_feed;
712 $this->init_query_flags();
713 $this->is_404 = true;
715 $this->is_feed = $is_feed;
718 function get($query_var) {
719 if (isset($this->query_vars[$query_var])) {
720 return $this->query_vars[$query_var];
726 function set($query_var, $value) {
727 $this->query_vars[$query_var] = $value;
730 function &get_posts() {
731 global $wpdb, $pagenow, $user_ID;
733 do_action_ref_array('pre_get_posts', array(&$this));
736 $q = &$this->query_vars;
738 $q = $this->fill_query_vars($q);
740 // First let's clear some variables
752 if ( !isset($q['post_type']) )
753 $q['post_type'] = 'post';
754 $post_type = $q['post_type'];
755 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
756 $q['posts_per_page'] = get_option('posts_per_page');
757 if ( isset($q['showposts']) && $q['showposts'] ) {
758 $q['showposts'] = (int) $q['showposts'];
759 $q['posts_per_page'] = $q['showposts'];
761 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
762 $q['posts_per_page'] = $q['posts_per_archive_page'];
763 if ( !isset($q['nopaging']) ) {
764 if ($q['posts_per_page'] == -1) {
765 $q['nopaging'] = true;
767 $q['nopaging'] = false;
770 if ( $this->is_feed ) {
771 $q['posts_per_page'] = get_option('posts_per_rss');
772 $q['nopaging'] = false;
774 $q['posts_per_page'] = (int) $q['posts_per_page'];
775 if ( $q['posts_per_page'] < -1 )
776 $q['posts_per_page'] = abs($q['posts_per_page']);
777 else if ( $q['posts_per_page'] == 0 )
778 $q['posts_per_page'] = 1;
780 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
781 $this->is_page = true;
782 $this->is_home = false;
783 $q['page_id'] = get_option('page_on_front');
786 if (isset($q['page'])) {
787 $q['page'] = trim($q['page'], '/');
788 $q['page'] = (int) $q['page'];
789 $q['page'] = abs($q['page']);
792 $add_hours = intval(get_option('gmt_offset'));
793 $add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
794 $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
796 // If a month is specified in the querystring, load that month
798 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
799 $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4);
800 if (strlen($q['m'])>5)
801 $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2);
802 if (strlen($q['m'])>7)
803 $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2);
804 if (strlen($q['m'])>9)
805 $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2);
806 if (strlen($q['m'])>11)
807 $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2);
808 if (strlen($q['m'])>13)
809 $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2);
812 if ( '' !== $q['hour'] )
813 $where .= " AND HOUR(post_date)='" . $q['hour'] . "'";
815 if ( '' !== $q['minute'] )
816 $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'";
818 if ( '' !== $q['second'] )
819 $where .= " AND SECOND(post_date)='" . $q['second'] . "'";
822 $where .= " AND YEAR(post_date)='" . $q['year'] . "'";
824 if ( $q['monthnum'] )
825 $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'";
828 $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'";
830 if ('' != $q['name']) {
831 $q['name'] = sanitize_title($q['name']);
832 $where .= " AND post_name = '" . $q['name'] . "'";
833 } else if ('' != $q['pagename']) {
834 if ( isset($this->queried_object_id) )
835 $reqpage = $this->queried_object_id;
837 $reqpage = get_page_by_path($q['pagename']);
838 if ( !empty($reqpage) )
839 $reqpage = $reqpage->ID;
844 if ( ('page' != get_option('show_on_front') ) || ( $reqpage != get_option('page_for_posts') ) ) {
845 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
846 $page_paths = '/' . trim($q['pagename'], '/');
847 $q['pagename'] = sanitize_title(basename($page_paths));
848 $q['name'] = $q['pagename'];
849 $where .= " AND (ID = '$reqpage')";
851 } elseif ('' != $q['attachment']) {
852 $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
853 $attach_paths = '/' . trim($q['attachment'], '/');
854 $q['attachment'] = sanitize_title(basename($attach_paths));
855 $q['name'] = $q['attachment'];
856 $where .= " AND post_name = '" . $q['attachment'] . "'";
860 $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'";
862 if ( intval($q['comments_popup']) )
863 $q['p'] = intval($q['comments_popup']);
865 // If an attachment is requested by number, let it supercede any post number.
866 if ( $q['attachment_id'] )
867 $q['p'] = $q['attachment_id'];
869 // If a post number is specified, load that post
871 $where = ' AND ID = ' . $q['p'];
873 if ( $q['page_id'] ) {
874 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
875 $q['p'] = $q['page_id'];
876 $where = ' AND ID = ' . $q['page_id'];
880 // If a search pattern is specified, load the posts that match
881 if ( !empty($q['s']) ) {
882 // added slashes screw with quote grouping when done early, so done later
883 $q['s'] = stripslashes($q['s']);
884 if ($q['sentence']) {
885 $q['search_terms'] = array($q['s']);
888 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q[s], $matches);
889 $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
891 $n = ($q['exact']) ? '' : '%';
893 foreach((array)$q['search_terms'] as $term) {
894 $term = addslashes_gpc($term);
895 $search .= "{$searchand}((post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}'))";
896 $searchand = ' AND ';
898 $term = addslashes_gpc($q['s']);
899 if (!$q['sentence'] && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
900 $search .= " OR (post_title LIKE '{$n}{$term}{$n}') OR (post_content LIKE '{$n}{$term}{$n}')";
902 if ( !empty($search) )
903 $search = " AND ({$search}) ";
908 if ( empty($q['cat']) || ($q['cat'] == '0') ||
909 // Bypass cat checks if fetching specific posts
910 $this->is_singular ) {
913 $q['cat'] = ''.urldecode($q['cat']).'';
914 $q['cat'] = addslashes_gpc($q['cat']);
915 $cat_array = preg_split('/[,\s]+/', $q['cat']);
916 foreach ( $cat_array as $cat ) {
921 $q['category__in'][] = $cat;
922 $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
924 $q['category__not_in'][] = $cat;
925 $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
930 if ( !empty($q['category__in']) || !empty($q['category__not_in']) || !empty($q['category__and']) ) {
931 $groupby = "{$wpdb->posts}.ID";
934 if ( !empty($q['category__in']) ) {
935 $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) ";
936 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
937 $include_cats = "'" . implode("', '", $q['category__in']) . "'";
938 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
941 if ( !empty($q['category__not_in']) ) {
942 $ids = get_objects_in_term($q['category__not_in'], 'category');
943 if ( is_wp_error( $ids ) )
945 if ( is_array($ids) && count($ids > 0) ) {
946 $out_posts = "'" . implode("', '", $ids) . "'";
947 $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
951 // Category stuff for nice URLs
952 if ( '' != $q['category_name'] ) {
953 $reqcat = get_category_by_path($q['category_name']);
954 $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
955 $cat_paths = '/' . trim($q['category_name'], '/');
956 $q['category_name'] = sanitize_title(basename($cat_paths));
958 $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
959 $q['category_name'] = sanitize_title(basename($cat_paths));
960 $cat_paths = explode('/', $cat_paths);
962 foreach ( (array) $cat_paths as $pathdir )
963 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
965 //if we don't match the entire hierarchy fallback on just matching the nicename
966 if ( empty($reqcat) )
967 $reqcat = get_category_by_path($q['category_name'], false);
969 if ( !empty($reqcat) )
970 $reqcat = $reqcat->term_id;
976 $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) ";
977 $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
978 $in_cats = array($q['cat']);
979 $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
980 $in_cats = "'" . implode("', '", $in_cats) . "'";
981 $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
982 $groupby = "{$wpdb->posts}.ID";
986 if ( '' != $q['tag'] ) {
987 if ( strpos($q['tag'], ',') !== false ) {
988 $tags = preg_split('/[,\s]+/', $q['tag']);
989 foreach ( (array) $tags as $tag ) {
990 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
991 $q['tag_slug__in'][] = $tag;
993 } else if ( preg_match('/[+\s]+/', $q['tag']) ) {
994 $tags = preg_split('/[+\s]+/', $q['tag']);
995 foreach ( (array) $tags as $tag ) {
996 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
997 $q['tag_slug__and'][] = $tag;
1000 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
1001 $reqtag = is_term( $q['tag'], 'post_tag' );
1002 if ( !empty($reqtag) )
1003 $reqtag = $reqtag['term_id'];
1007 $q['tag_id'] = $reqtag;
1008 $q['tag__in'][] = $reqtag;
1012 if ( !empty($q['tag__in']) || !empty($q['tag__not_in']) || !empty($q['tag__and']) ||
1013 !empty($q['tag_slug__in']) || !empty($q['tag_slug__and']) ) {
1014 $groupby = "{$wpdb->posts}.ID";
1017 if ( !empty($q['tag__in']) ) {
1018 $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) ";
1019 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1020 $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
1021 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
1022 $reqtag = is_term( $q['tag__in'][0], 'post_tag' );
1023 if ( !empty($reqtag) )
1024 $q['tag_id'] = $reqtag['term_id'];
1027 if ( !empty($q['tag_slug__in']) ) {
1028 $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) ";
1029 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
1030 $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
1031 $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
1032 $reqtag = is_term( $q['tag_slug__in'][0], 'post_tag' );
1033 if ( !empty($reqtag) )
1034 $q['tag_id'] = $reqtag['term_id'];
1037 if ( !empty($q['tag__not_in']) ) {
1038 $ids = get_objects_in_term($q['tag__not_in'], 'post_tag');
1039 if ( is_array($ids) && count($ids > 0) ) {
1040 $out_posts = "'" . implode("', '", $ids) . "'";
1041 $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
1045 // Tag and slug intersections.
1046 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag');
1047 foreach ($intersections as $item => $taxonomy) {
1048 if ( empty($q[$item]) ) continue;
1050 if ( $item != 'category__and' ) {
1051 $reqtag = is_term( $q[$item][0], 'post_tag' );
1052 if ( !empty($reqtag) )
1053 $q['tag_id'] = $reqtag['term_id'];
1056 $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id';
1058 $q[$item] = array_unique($q[$item]);
1059 $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)";
1060 $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
1061 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
1063 $post_ids = $wpdb->get_col($tsql);
1065 if ( count($post_ids) )
1066 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
1068 $whichcat = " AND 0 = 1";
1073 // Author/user stuff
1075 if ( empty($q['author']) || ($q['author'] == '0') ) {
1078 $q['author'] = ''.urldecode($q['author']).'';
1079 $q['author'] = addslashes_gpc($q['author']);
1080 if (strpos($q['author'], '-') !== false) {
1083 $q['author'] = explode('-', $q['author']);
1084 $q['author'] = ''.intval($q['author'][1]);
1089 $author_array = preg_split('/[,\s]+/', $q['author']);
1090 $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
1091 for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
1092 $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
1094 $whichauthor .= ')';
1097 // Author stuff for nice URLs
1099 if ('' != $q['author_name']) {
1100 if (strpos($q['author_name'], '/') !== false) {
1101 $q['author_name'] = explode('/',$q['author_name']);
1102 if ($q['author_name'][count($q['author_name'])-1]) {
1103 $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
1105 $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
1108 $q['author_name'] = sanitize_title($q['author_name']);
1109 $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
1110 $whichauthor .= ' AND (post_author = '.intval($q['author']).')';
1113 $where .= $search.$whichcat.$whichauthor;
1115 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
1116 $q['order'] = 'DESC';
1119 if ( empty($q['orderby']) ) {
1120 $q['orderby'] = 'post_date '.$q['order'];
1122 // Used to filter values
1123 $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order');
1124 $q['orderby'] = urldecode($q['orderby']);
1125 $q['orderby'] = addslashes_gpc($q['orderby']);
1126 $orderby_array = explode(' ',$q['orderby']);
1127 if ( empty($orderby_array) )
1128 $orderby_array[] = $q['orderby'];
1130 for ($i = 0; $i < count($orderby_array); $i++) {
1131 // Only allow certain values for safety
1132 $orderby = $orderby_array[$i];
1133 if ( 'menu_order' != $orderby )
1134 $orderby = 'post_' . $orderby;
1135 if ( in_array($orderby_array[$i], $allowed_keys) )
1136 $q['orderby'] .= (($i == 0) ? '' : ',') . "$orderby {$q['order']}";
1138 if ( empty($q['orderby']) )
1139 $q['orderby'] = 'post_date '.$q['order'];
1142 if ( $this->is_attachment ) {
1143 $where .= " AND post_type = 'attachment'";
1144 } elseif ($this->is_page) {
1145 $where .= " AND post_type = 'page'";
1146 } elseif ($this->is_single) {
1147 $where .= " AND post_type = 'post'";
1149 $where .= " AND post_type = '$post_type'";
1152 if ( isset($q['post_status']) && '' != $q['post_status'] ) {
1153 $q_status = explode(',', $q['post_status']);
1154 $r_status = array();
1155 if ( in_array( 'draft' , $q_status ) )
1156 $r_status[] = "post_status = 'draft'";
1157 if ( in_array( 'pending', $q_status ) )
1158 $r_status[] = "post_status = 'pending'";
1159 if ( in_array( 'future' , $q_status ) )
1160 $r_status[] = "post_status = 'future'";
1161 if ( in_array( 'inherit' , $q_status ) )
1162 $r_status[] = "post_status = 'inherit'";
1163 if ( in_array( 'private', $q_status ) )
1164 $r_status[] = "post_status = 'private'";
1165 if ( in_array( 'publish', $q_status ) )
1166 $r_status[] = "post_status = 'publish'";
1167 if ( !empty($r_status) )
1168 $where .= " AND (" . join( ' OR ', $r_status ) . ")";
1169 } elseif ( !$this->is_singular ) {
1170 $where .= " AND (post_status = 'publish'";
1173 $where .= " OR post_status = 'future' OR post_status = 'draft' OR post_status = 'pending'";
1175 if ( is_user_logged_in() ) {
1176 $where .= current_user_can( "read_private_{$post_type}s" ) ? " OR post_status = 'private'" : " OR post_author = $user_ID AND post_status = 'private'";
1182 // Apply filters on where and join prior to paging so that any
1183 // manipulations to them are reflected in the paging by day queries.
1184 $where = apply_filters('posts_where', $where);
1185 $join = apply_filters('posts_join', $join);
1188 if ( empty($q['nopaging']) && !$this->is_singular ) {
1189 $page = abs(intval($q['paged']));
1194 if ( empty($q['offset']) ) {
1196 $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', ';
1197 $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
1198 } else { // we're ignoring $page and using 'offset'
1199 $q['offset'] = abs(intval($q['offset']));
1200 $pgstrt = $q['offset'] . ', ';
1201 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
1206 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
1207 if ( $this->is_archive || $this->is_search ) {
1208 $cjoin = "LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
1209 $cwhere = "WHERE comment_approved = '1' $where";
1210 $cgroupby = "GROUP BY $wpdb->comments.comment_id";
1211 } else { // Other non singular e.g. front
1212 $cjoin = "LEFT JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
1213 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
1217 $cjoin = apply_filters('comment_feed_join', $cjoin);
1218 $cwhere = apply_filters('comment_feed_where', $cwhere);
1219 $cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
1221 $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'));
1222 $this->comment_count = count($this->comments);
1224 $post_ids = array();
1226 foreach ($this->comments as $comment)
1227 $post_ids[] = (int) $comment->comment_post_ID;
1229 $post_ids = join(',', $post_ids);
1232 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
1237 // Apply post-paging filters on where and join. Only plugins that
1238 // manipulate paging queries should use these hooks.
1240 // Announce current selection parameters. For use by caching plugins.
1241 do_action( 'posts_selection', $where . $groupby . $q['orderby'] . $limits . $join );
1243 $where = apply_filters('posts_where_paged', $where);
1244 $groupby = apply_filters('posts_groupby', $groupby);
1245 if ( ! empty($groupby) )
1246 $groupby = 'GROUP BY ' . $groupby;
1247 $join = apply_filters('posts_join_paged', $join);
1248 $orderby = apply_filters('posts_orderby', $q['orderby']);
1249 if ( !empty( $orderby ) )
1250 $orderby = 'ORDER BY ' . $orderby;
1251 $distinct = apply_filters('posts_distinct', $distinct);
1252 $fields = apply_filters('posts_fields', "$wpdb->posts.*");
1253 $limits = apply_filters( 'post_limits', $limits );
1255 if ( !empty($limits) )
1256 $found_rows = 'SQL_CALC_FOUND_ROWS';
1258 $request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
1259 $this->request = apply_filters('posts_request', $request);
1261 $this->posts = $wpdb->get_results($this->request);
1262 // Raw results filter. Prior to status checks.
1263 $this->posts = apply_filters('posts_results', $this->posts);
1265 if ( $this->is_comment_feed && $this->is_singular ) {
1266 $cjoin = apply_filters('comment_feed_join', '');
1267 $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = {$this->posts[0]->ID} AND comment_approved = '1'");
1268 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss');
1269 $this->comments = $wpdb->get_results($comments_request);
1270 $this->comment_count = count($this->comments);
1273 if ( !empty($limits) ) {
1274 $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
1275 $this->found_posts = $wpdb->get_var( $found_posts_query );
1276 $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
1277 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
1280 // Check post status to determine if post should be displayed.
1281 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
1282 $status = get_post_status($this->posts[0]);
1283 //$type = get_post_type($this->posts[0]);
1284 if ( ('publish' != $status) ) {
1285 if ( ! is_user_logged_in() ) {
1286 // User must be logged in to view unpublished posts.
1287 $this->posts = array();
1289 if (in_array($status, array('draft', 'pending')) ) {
1290 // User must have edit permissions on the draft to preview.
1291 if (! current_user_can('edit_post', $this->posts[0]->ID)) {
1292 $this->posts = array();
1294 $this->is_preview = true;
1295 $this->posts[0]->post_date = current_time('mysql');
1297 } else if ('future' == $status) {
1298 $this->is_preview = true;
1299 if (!current_user_can('edit_post', $this->posts[0]->ID)) {
1300 $this->posts = array ( );
1303 if (! current_user_can('read_post', $this->posts[0]->ID))
1304 $this->posts = array();
1310 $this->posts = apply_filters('the_posts', $this->posts);
1312 update_post_caches($this->posts);
1314 $this->post_count = count($this->posts);
1315 if ($this->post_count > 0) {
1316 $this->post = $this->posts[0];
1319 return $this->posts;
1322 function next_post() {
1324 $this->current_post++;
1326 $this->post = $this->posts[$this->current_post];
1330 function the_post() {
1332 $this->in_the_loop = true;
1333 $post = $this->next_post();
1334 setup_postdata($post);
1336 if ( $this->current_post == 0 ) // loop has just started
1337 do_action('loop_start');
1340 function have_posts() {
1341 if ($this->current_post + 1 < $this->post_count) {
1343 } elseif ($this->current_post + 1 == $this->post_count) {
1344 do_action('loop_end');
1345 // Do some cleaning up after the loop
1346 $this->rewind_posts();
1349 $this->in_the_loop = false;
1353 function rewind_posts() {
1354 $this->current_post = -1;
1355 if ($this->post_count > 0) {
1356 $this->post = $this->posts[0];
1360 function next_comment() {
1361 $this->current_comment++;
1363 $this->comment = $this->comments[$this->current_comment];
1364 return $this->comment;
1367 function the_comment() {
1370 $comment = $this->next_comment();
1372 if ($this->current_comment == 0) {
1373 do_action('comment_loop_start');
1377 function have_comments() {
1378 if ($this->current_comment + 1 < $this->comment_count) {
1380 } elseif ($this->current_comment + 1 == $this->comment_count) {
1381 $this->rewind_comments();
1387 function rewind_comments() {
1388 $this->current_comment = -1;
1389 if ($this->comment_count > 0) {
1390 $this->comment = $this->comments[0];
1394 function &query($query) {
1395 $this->parse_query($query);
1396 return $this->get_posts();
1399 function get_queried_object() {
1400 if (isset($this->queried_object)) {
1401 return $this->queried_object;
1404 $this->queried_object = NULL;
1405 $this->queried_object_id = 0;
1407 if ($this->is_category) {
1408 $cat = $this->get('cat');
1409 $category = &get_category($cat);
1410 $this->queried_object = &$category;
1411 $this->queried_object_id = (int) $cat;
1412 } else if ($this->is_tag) {
1413 $tag_id = $this->get('tag_id');
1414 $tag = &get_term($tag_id, 'post_tag');
1415 if ( is_wp_error( $tag ) )
1417 $this->queried_object = &$tag;
1418 $this->queried_object_id = (int) $tag_id;
1419 } else if ($this->is_posts_page) {
1420 $this->queried_object = & get_page(get_option('page_for_posts'));
1421 $this->queried_object_id = (int) $this->queried_object->ID;
1422 } else if ($this->is_single) {
1423 $this->queried_object = $this->post;
1424 $this->queried_object_id = (int) $this->post->ID;
1425 } else if ($this->is_page) {
1426 $this->queried_object = $this->post;
1427 $this->queried_object_id = (int) $this->post->ID;
1428 } else if ($this->is_author) {
1429 $author_id = (int) $this->get('author');
1430 $author = get_userdata($author_id);
1431 $this->queried_object = $author;
1432 $this->queried_object_id = $author_id;
1435 return $this->queried_object;
1438 function get_queried_object_id() {
1439 $this->get_queried_object();
1441 if (isset($this->queried_object_id)) {
1442 return $this->queried_object_id;
1448 function WP_Query ($query = '') {
1449 if (! empty($query)) {
1450 $this->query($query);
1456 // Redirect old slugs
1457 function wp_old_slug_redirect () {
1459 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
1462 $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'] . "'";
1464 // if year, monthnum, or day have been specified, make our query more precise
1465 // just in case there are multiple identical _wp_old_slug values
1466 if ( '' != $wp_query->query_vars['year'] )
1467 $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
1468 if ( '' != $wp_query->query_vars['monthnum'] )
1469 $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
1470 if ( '' != $wp_query->query_vars['day'] )
1471 $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
1473 $id = (int) $wpdb->get_var($query);
1478 $link = get_permalink($id);
1483 wp_redirect($link, '301'); // Permanent redirect
1490 // Private helper functions
1493 // Setup global post data.
1494 function setup_postdata($post) {
1495 global $id, $postdata, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages, $wp_query;
1498 $id = (int) $post->ID;
1500 $authordata = get_userdata($post->post_author);
1502 $day = mysql2date('d.m.y', $post->post_date);
1503 $currentmonth = mysql2date('m', $post->post_date);
1505 $page = get_query_var('page');
1508 if ( is_single() || is_page() )
1510 $content = $post->post_content;
1511 if ( preg_match('/<!--nextpage-->/', $content) ) {
1515 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
1516 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
1517 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
1518 $pages = explode('<!--nextpage-->', $content);
1519 $numpages = count($pages);
1521 $pages[0] = $post->post_content;