-
+ 5FFFD9A4EDB5C0FF99F2202E253CDADE9E59486DD577BF2D951485D53C7D91246E38E3003F586AB6946D970F90616DAEA8196E77CA6CB9AFF7538D6665786B0F
mp-wp/wp-includes/query.php
(0 . 0)(1 . 2684)
144764 <?php
144765 /**
144766 * WordPress Query API
144767 *
144768 * The query API attempts to get which part of WordPress to the user is on. It
144769 * also provides functionality to getting URL query information.
144770 *
144771 * @link http://codex.wordpress.org/The_Loop More information on The Loop.
144772 *
144773 * @package WordPress
144774 * @subpackage Query
144775 */
144776
144777 /**
144778 * Retrieve variable in the WP_Query class.
144779 *
144780 * @see WP_Query::get()
144781 * @since 1.5.0
144782 * @uses $wp_query
144783 *
144784 * @param string $var The variable key to retrieve.
144785 * @return mixed
144786 */
144787 function get_query_var($var) {
144788 global $wp_query;
144789
144790 return $wp_query->get($var);
144791 }
144792
144793 /**
144794 * Set query variable.
144795 *
144796 * @see WP_Query::set()
144797 * @since 2.2.0
144798 * @uses $wp_query
144799 *
144800 * @param string $var Query variable key.
144801 * @param mixed $value
144802 * @return null
144803 */
144804 function set_query_var($var, $value) {
144805 global $wp_query;
144806
144807 return $wp_query->set($var, $value);
144808 }
144809
144810 /**
144811 * Setup The Loop with query parameters.
144812 *
144813 * This will override the current WordPress Loop and shouldn't be used more than
144814 * once. This must not be used within the WordPress Loop.
144815 *
144816 * @since 1.5.0
144817 * @uses $wp_query
144818 *
144819 * @param string $query
144820 * @return array List of posts
144821 */
144822 function &query_posts($query) {
144823 unset($GLOBALS['wp_query']);
144824 $GLOBALS['wp_query'] = new WP_Query();
144825 return $GLOBALS['wp_query']->query($query);
144826 }
144827
144828 /**
144829 * Destroy the previous query and setup a new query.
144830 *
144831 * This should be used after {@link query_posts()} and before another {@link
144832 * query_posts()}. This will remove obscure bugs that occur when the previous
144833 * wp_query object is not destroyed properly before another is setup.
144834 *
144835 * @since 2.3.0
144836 * @uses $wp_query
144837 */
144838 function wp_reset_query() {
144839 unset($GLOBALS['wp_query']);
144840 $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
144841 global $wp_query;
144842 if ( !empty($wp_query->post) ) {
144843 $GLOBALS['post'] = $wp_query->post;
144844 setup_postdata($wp_query->post);
144845 }
144846 }
144847
144848 /*
144849 * Query type checks.
144850 */
144851
144852 /**
144853 * Whether the current request is in WordPress admin Panel
144854 *
144855 * Does not inform on whether the user is an admin! Use capability checks to
144856 * tell if the user should be accessing a section or not.
144857 *
144858 * @since 1.5.1
144859 *
144860 * @return bool True if inside WordPress administration pages.
144861 */
144862 function is_admin () {
144863 if ( defined('WP_ADMIN') )
144864 return WP_ADMIN;
144865 return false;
144866 }
144867
144868 /**
144869 * Is query requesting an archive page.
144870 *
144871 * @since 1.5.0
144872 * @uses $wp_query
144873 *
144874 * @return bool True if page is archive.
144875 */
144876 function is_archive () {
144877 global $wp_query;
144878
144879 return $wp_query->is_archive;
144880 }
144881
144882 /**
144883 * Is query requesting an attachment page.
144884 *
144885 * @since 2.0.0
144886 * @uses $wp_query
144887 *
144888 * @return bool True if page is attachment.
144889 */
144890 function is_attachment () {
144891 global $wp_query;
144892
144893 return $wp_query->is_attachment;
144894 }
144895
144896 /**
144897 * Is query requesting an author page.
144898 *
144899 * If the $author parameter is specified then the check will be expanded to
144900 * include whether the queried author matches the one given in the parameter.
144901 * You can match against integers and against strings.
144902 *
144903 * If matching against an integer, the ID should be used of the author for the
144904 * test. If the $author is an ID and matches the author page user ID, then
144905 * 'true' will be returned.
144906 *
144907 * If matching against strings, then the test will be matched against both the
144908 * nickname and user nicename and will return true on success.
144909 *
144910 * @since 1.5.0
144911 * @uses $wp_query
144912 *
144913 * @param string|int $author Optional. Is current page this author.
144914 * @return bool True if page is author or $author (if set).
144915 */
144916 function is_author ($author = '') {
144917 global $wp_query;
144918
144919 if ( !$wp_query->is_author )
144920 return false;
144921
144922 if ( empty($author) )
144923 return true;
144924
144925 $author_obj = $wp_query->get_queried_object();
144926
144927 $author = (array) $author;
144928
144929 if ( in_array( $author_obj->ID, $author ) )
144930 return true;
144931 elseif ( in_array( $author_obj->nickname, $author ) )
144932 return true;
144933 elseif ( in_array( $author_obj->user_nicename, $author ) )
144934 return true;
144935
144936 return false;
144937 }
144938
144939 /**
144940 * Whether current page query contains a category name or given category name.
144941 *
144942 * The category list can contain category IDs, names, or category slugs. If any
144943 * of them are part of the query, then it will return true.
144944 *
144945 * @since 1.5.0
144946 * @uses $wp_query
144947 *
144948 * @param string|array $category Optional.
144949 * @return bool
144950 */
144951 function is_category ($category = '') {
144952 global $wp_query;
144953
144954 if ( !$wp_query->is_category )
144955 return false;
144956
144957 if ( empty($category) )
144958 return true;
144959
144960 $cat_obj = $wp_query->get_queried_object();
144961
144962 $category = (array) $category;
144963
144964 if ( in_array( $cat_obj->term_id, $category ) )
144965 return true;
144966 elseif ( in_array( $cat_obj->name, $category ) )
144967 return true;
144968 elseif ( in_array( $cat_obj->slug, $category ) )
144969 return true;
144970
144971 return false;
144972 }
144973
144974 /**
144975 * Whether the current page query has the given tag slug or contains tag.
144976 *
144977 * @since 2.3.0
144978 * @uses $wp_query
144979 *
144980 * @param string|array $slug Optional. Single tag or list of tags to check for.
144981 * @return bool
144982 */
144983 function is_tag( $slug = '' ) {
144984 global $wp_query;
144985
144986 if ( !$wp_query->is_tag )
144987 return false;
144988
144989 if ( empty( $slug ) )
144990 return true;
144991
144992 $tag_obj = $wp_query->get_queried_object();
144993
144994 $slug = (array) $slug;
144995
144996 if ( in_array( $tag_obj->slug, $slug ) )
144997 return true;
144998
144999 return false;
145000 }
145001
145002 /**
145003 * Whether the current page query has the given taxonomy slug or contains taxonomy.
145004 *
145005 * @since 2.5.0
145006 * @uses $wp_query
145007 *
145008 * @param string|array $slug Optional. Slug or slugs to check in current query.
145009 * @return bool
145010 */
145011 function is_tax( $slug = '' ) {
145012 global $wp_query;
145013
145014 if ( !$wp_query->is_tax )
145015 return false;
145016
145017 if ( empty($slug) )
145018 return true;
145019
145020 $term = $wp_query->get_queried_object();
145021
145022 $slug = (array) $slug;
145023
145024 if ( in_array( $term->slug, $slug ) )
145025 return true;
145026
145027 return false;
145028 }
145029
145030 /**
145031 * Whether the current URL is within the comments popup window.
145032 *
145033 * @since 1.5.0
145034 * @uses $wp_query
145035 *
145036 * @return bool
145037 */
145038 function is_comments_popup () {
145039 global $wp_query;
145040
145041 return $wp_query->is_comments_popup;
145042 }
145043
145044 /**
145045 * Whether current URL is based on a date.
145046 *
145047 * @since 1.5.0
145048 * @uses $wp_query
145049 *
145050 * @return bool
145051 */
145052 function is_date () {
145053 global $wp_query;
145054
145055 return $wp_query->is_date;
145056 }
145057
145058 /**
145059 * Whether current blog URL contains a day.
145060 *
145061 * @since 1.5.0
145062 * @uses $wp_query
145063 *
145064 * @return bool
145065 */
145066 function is_day () {
145067 global $wp_query;
145068
145069 return $wp_query->is_day;
145070 }
145071
145072 /**
145073 * Whether current page query is feed URL.
145074 *
145075 * @since 1.5.0
145076 * @uses $wp_query
145077 *
145078 * @return bool
145079 */
145080 function is_feed () {
145081 global $wp_query;
145082
145083 return $wp_query->is_feed;
145084 }
145085
145086 /**
145087 * Whether current page query is the front of the site.
145088 *
145089 * @since 2.5.0
145090 * @uses is_home()
145091 * @uses get_option()
145092 *
145093 * @return bool True, if front of site.
145094 */
145095 function is_front_page () {
145096 // most likely case
145097 if ( 'posts' == get_option('show_on_front') && is_home() )
145098 return true;
145099 elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
145100 return true;
145101 else
145102 return false;
145103 }
145104
145105 /**
145106 * Whether current page view is the blog homepage.
145107 *
145108 * @since 1.5.0
145109 * @uses $wp_query
145110 *
145111 * @return bool True if blog view homepage.
145112 */
145113 function is_home () {
145114 global $wp_query;
145115
145116 return $wp_query->is_home;
145117 }
145118
145119 /**
145120 * Whether current page query contains a month.
145121 *
145122 * @since 1.5.0
145123 * @uses $wp_query
145124 *
145125 * @return bool
145126 */
145127 function is_month () {
145128 global $wp_query;
145129
145130 return $wp_query->is_month;
145131 }
145132
145133 /**
145134 * Whether query is page or contains given page(s).
145135 *
145136 * Calls the function without any parameters will only test whether the current
145137 * query is of the page type. Either a list or a single item can be tested
145138 * against for whether the query is a page and also is the value or one of the
145139 * values in the page parameter.
145140 *
145141 * The parameter can contain the page ID, page title, or page name. The
145142 * parameter can also be an array of those three values.
145143 *
145144 * @since 1.5.0
145145 * @uses $wp_query
145146 *
145147 * @param mixed $page Either page or list of pages to test against.
145148 * @return bool
145149 */
145150 function is_page ($page = '') {
145151 global $wp_query;
145152
145153 if ( !$wp_query->is_page )
145154 return false;
145155
145156 if ( empty($page) )
145157 return true;
145158
145159 $page_obj = $wp_query->get_queried_object();
145160
145161 $page = (array) $page;
145162
145163 if ( in_array( $page_obj->ID, $page ) )
145164 return true;
145165 elseif ( in_array( $page_obj->post_title, $page ) )
145166 return true;
145167 else if ( in_array( $page_obj->post_name, $page ) )
145168 return true;
145169
145170 return false;
145171 }
145172
145173 /**
145174 * Whether query contains multiple pages for the results.
145175 *
145176 * @since 1.5.0
145177 * @uses $wp_query
145178 *
145179 * @return bool
145180 */
145181 function is_paged () {
145182 global $wp_query;
145183
145184 return $wp_query->is_paged;
145185 }
145186
145187 /**
145188 * Whether the current page was created by a plugin.
145189 *
145190 * The plugin can set this by using the global $plugin_page and setting it to
145191 * true.
145192 *
145193 * @since 1.5.0
145194 * @global bool $plugin_page Used by plugins to tell the query that current is a plugin page.
145195 *
145196 * @return bool
145197 */
145198 function is_plugin_page() {
145199 global $plugin_page;
145200
145201 if ( isset($plugin_page) )
145202 return true;
145203
145204 return false;
145205 }
145206
145207 /**
145208 * Whether the current query is preview of post or page.
145209 *
145210 * @since 2.0.0
145211 * @uses $wp_query
145212 *
145213 * @return bool
145214 */
145215 function is_preview() {
145216 global $wp_query;
145217
145218 return $wp_query->is_preview;
145219 }
145220
145221 /**
145222 * Whether the current query post is robots.
145223 *
145224 * @since 2.1.0
145225 * @uses $wp_query
145226 *
145227 * @return bool
145228 */
145229 function is_robots() {
145230 global $wp_query;
145231
145232 return $wp_query->is_robots;
145233 }
145234
145235 /**
145236 * Whether current query is the result of a user search.
145237 *
145238 * @since 1.5.0
145239 * @uses $wp_query
145240 *
145241 * @return bool
145242 */
145243 function is_search () {
145244 global $wp_query;
145245
145246 return $wp_query->is_search;
145247 }
145248
145249 /**
145250 * Whether the current page query is single page.
145251 *
145252 * The parameter can contain the post ID, post title, or post name. The
145253 * parameter can also be an array of those three values.
145254 *
145255 * This applies to other post types, attachments, pages, posts. Just means that
145256 * the current query has only a single object.
145257 *
145258 * @since 1.5.0
145259 * @uses $wp_query
145260 *
145261 * @param mixed $post Either post or list of posts to test against.
145262 * @return bool
145263 */
145264 function is_single ($post = '') {
145265 global $wp_query;
145266
145267 if ( !$wp_query->is_single )
145268 return false;
145269
145270 if ( empty( $post) )
145271 return true;
145272
145273 $post_obj = $wp_query->get_queried_object();
145274
145275 $post = (array) $post;
145276
145277 if ( in_array( $post_obj->ID, $post ) )
145278 return true;
145279 elseif ( in_array( $post_obj->post_title, $post ) )
145280 return true;
145281 elseif ( in_array( $post_obj->post_name, $post ) )
145282 return true;
145283
145284 return false;
145285 }
145286
145287 /**
145288 * Whether is single post, is a page, or is an attachment.
145289 *
145290 * @since 1.5.0
145291 * @uses $wp_query
145292 *
145293 * @return bool
145294 */
145295 function is_singular() {
145296 global $wp_query;
145297
145298 return $wp_query->is_singular;
145299 }
145300
145301 /**
145302 * Whether the query contains a time.
145303 *
145304 * @since 1.5.0
145305 * @uses $wp_query
145306 *
145307 * @return bool
145308 */
145309 function is_time () {
145310 global $wp_query;
145311
145312 return $wp_query->is_time;
145313 }
145314
145315 /**
145316 * Whether the query is a trackback.
145317 *
145318 * @since 1.5.0
145319 * @uses $wp_query
145320 *
145321 * @return bool
145322 */
145323 function is_trackback () {
145324 global $wp_query;
145325
145326 return $wp_query->is_trackback;
145327 }
145328
145329 /**
145330 * Whether the query contains a year.
145331 *
145332 * @since 1.5.0
145333 * @uses $wp_query
145334 *
145335 * @return bool
145336 */
145337 function is_year () {
145338 global $wp_query;
145339
145340 return $wp_query->is_year;
145341 }
145342
145343 /**
145344 * Whether current page query is a 404 and no results for WordPress query.
145345 *
145346 * @since 1.5.0
145347 * @uses $wp_query
145348 *
145349 * @return bool True, if nothing is found matching WordPress Query.
145350 */
145351 function is_404 () {
145352 global $wp_query;
145353
145354 return $wp_query->is_404;
145355 }
145356
145357 /*
145358 * The Loop. Post loop control.
145359 */
145360
145361 /**
145362 * Whether current WordPress query has results to loop over.
145363 *
145364 * @see WP_Query::have_posts()
145365 * @since 1.5.0
145366 * @uses $wp_query
145367 *
145368 * @return bool
145369 */
145370 function have_posts() {
145371 global $wp_query;
145372
145373 return $wp_query->have_posts();
145374 }
145375
145376 /**
145377 * Whether the caller is in the Loop.
145378 *
145379 * @since 2.0.0
145380 * @uses $wp_query
145381 *
145382 * @return bool True if caller is within loop, false if loop hasn't started or ended.
145383 */
145384 function in_the_loop() {
145385 global $wp_query;
145386
145387 return $wp_query->in_the_loop;
145388 }
145389
145390 /**
145391 * Rewind the loop posts.
145392 *
145393 * @see WP_Query::rewind_posts()
145394 * @since 1.5.0
145395 * @uses $wp_query
145396 *
145397 * @return null
145398 */
145399 function rewind_posts() {
145400 global $wp_query;
145401
145402 return $wp_query->rewind_posts();
145403 }
145404
145405 /**
145406 * Iterate the post index in the loop.
145407 *
145408 * @see WP_Query::the_post()
145409 * @since 1.5.0
145410 * @uses $wp_query
145411 */
145412 function the_post() {
145413 global $wp_query;
145414
145415 $wp_query->the_post();
145416 }
145417
145418 /*
145419 * Comments loop.
145420 */
145421
145422 /**
145423 * Whether there are comments to loop over.
145424 *
145425 * @see WP_Query::have_comments()
145426 * @since 2.2.0
145427 * @uses $wp_query
145428 *
145429 * @return bool
145430 */
145431 function have_comments() {
145432 global $wp_query;
145433 return $wp_query->have_comments();
145434 }
145435
145436 /**
145437 * Iterate comment index in the comment loop.
145438 *
145439 * @see WP_Query::the_comment()
145440 * @since 2.2.0
145441 * @uses $wp_query
145442 *
145443 * @return object
145444 */
145445 function the_comment() {
145446 global $wp_query;
145447 return $wp_query->the_comment();
145448 }
145449
145450 /*
145451 * WP_Query
145452 */
145453
145454 /**
145455 * The WordPress Query class.
145456 *
145457 * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
145458 *
145459 * @since 1.5.0
145460 */
145461 class WP_Query {
145462
145463 /**
145464 * Query string
145465 *
145466 * @since 1.5.0
145467 * @access public
145468 * @var string
145469 */
145470 var $query;
145471
145472 /**
145473 * Query search variables set by the user.
145474 *
145475 * @since 1.5.0
145476 * @access public
145477 * @var array
145478 */
145479 var $query_vars = array();
145480
145481 /**
145482 * Holds the data for a single object that is queried.
145483 *
145484 * Holds the contents of a post, page, category, attachment.
145485 *
145486 * @since 1.5.0
145487 * @access public
145488 * @var object|array
145489 */
145490 var $queried_object;
145491
145492 /**
145493 * The ID of the queried object.
145494 *
145495 * @since 1.5.0
145496 * @access public
145497 * @var int
145498 */
145499 var $queried_object_id;
145500
145501 /**
145502 * Get post database query.
145503 *
145504 * @since 2.0.1
145505 * @access public
145506 * @var string
145507 */
145508 var $request;
145509
145510 /**
145511 * List of posts.
145512 *
145513 * @since 1.5.0
145514 * @access public
145515 * @var array
145516 */
145517 var $posts;
145518
145519 /**
145520 * The amount of posts for the current query.
145521 *
145522 * @since 1.5.0
145523 * @access public
145524 * @var int
145525 */
145526 var $post_count = 0;
145527
145528 /**
145529 * Index of the current item in the loop.
145530 *
145531 * @since 1.5.0
145532 * @access public
145533 * @var int
145534 */
145535 var $current_post = -1;
145536
145537 /**
145538 * Whether the loop has started and the caller is in the loop.
145539 *
145540 * @since 2.0.0
145541 * @access public
145542 * @var bool
145543 */
145544 var $in_the_loop = false;
145545
145546 /**
145547 * The current post ID.
145548 *
145549 * @since 1.5.0
145550 * @access public
145551 * @var int
145552 */
145553 var $post;
145554
145555 /**
145556 * The list of comments for current post.
145557 *
145558 * @since 2.2.0
145559 * @access public
145560 * @var array
145561 */
145562 var $comments;
145563
145564 /**
145565 * The amount of comments for the posts.
145566 *
145567 * @since 2.2.0
145568 * @access public
145569 * @var int
145570 */
145571 var $comment_count = 0;
145572
145573 /**
145574 * The index of the comment in the comment loop.
145575 *
145576 * @since 2.2.0
145577 * @access public
145578 * @var int
145579 */
145580 var $current_comment = -1;
145581
145582 /**
145583 * Current comment ID.
145584 *
145585 * @since 2.2.0
145586 * @access public
145587 * @var int
145588 */
145589 var $comment;
145590
145591 /**
145592 * Amount of posts if limit clause was not used.
145593 *
145594 * @since 2.1.0
145595 * @access public
145596 * @var int
145597 */
145598 var $found_posts = 0;
145599
145600 /**
145601 * The amount of pages.
145602 *
145603 * @since 2.1.0
145604 * @access public
145605 * @var int
145606 */
145607 var $max_num_pages = 0;
145608
145609 /**
145610 * The amount of comment pages.
145611 *
145612 * @since 2.7.0
145613 * @access public
145614 * @var int
145615 */
145616 var $max_num_comment_pages = 0;
145617
145618 /**
145619 * Set if query is single post.
145620 *
145621 * @since 1.5.0
145622 * @access public
145623 * @var bool
145624 */
145625 var $is_single = false;
145626
145627 /**
145628 * Set if query is preview of blog.
145629 *
145630 * @since 2.0.0
145631 * @access public
145632 * @var bool
145633 */
145634 var $is_preview = false;
145635
145636 /**
145637 * Set if query returns a page.
145638 *
145639 * @since 1.5.0
145640 * @access public
145641 * @var bool
145642 */
145643 var $is_page = false;
145644
145645 /**
145646 * Set if query is an archive list.
145647 *
145648 * @since 1.5.0
145649 * @access public
145650 * @var bool
145651 */
145652 var $is_archive = false;
145653
145654 /**
145655 * Set if query is part of a date.
145656 *
145657 * @since 1.5.0
145658 * @access public
145659 * @var bool
145660 */
145661 var $is_date = false;
145662
145663 /**
145664 * Set if query contains a year.
145665 *
145666 * @since 1.5.0
145667 * @access public
145668 * @var bool
145669 */
145670 var $is_year = false;
145671
145672 /**
145673 * Set if query contains a month.
145674 *
145675 * @since 1.5.0
145676 * @access public
145677 * @var bool
145678 */
145679 var $is_month = false;
145680
145681 /**
145682 * Set if query contains a day.
145683 *
145684 * @since 1.5.0
145685 * @access public
145686 * @var bool
145687 */
145688 var $is_day = false;
145689
145690 /**
145691 * Set if query contains time.
145692 *
145693 * @since 1.5.0
145694 * @access public
145695 * @var bool
145696 */
145697 var $is_time = false;
145698
145699 /**
145700 * Set if query contains an author.
145701 *
145702 * @since 1.5.0
145703 * @access public
145704 * @var bool
145705 */
145706 var $is_author = false;
145707
145708 /**
145709 * Set if query contains category.
145710 *
145711 * @since 1.5.0
145712 * @access public
145713 * @var bool
145714 */
145715 var $is_category = false;
145716
145717 /**
145718 * Set if query contains tag.
145719 *
145720 * @since 2.3.0
145721 * @access public
145722 * @var bool
145723 */
145724 var $is_tag = false;
145725
145726 /**
145727 * Set if query contains taxonomy.
145728 *
145729 * @since 2.5.0
145730 * @access public
145731 * @var bool
145732 */
145733 var $is_tax = false;
145734
145735 /**
145736 * Set if query was part of a search result.
145737 *
145738 * @since 1.5.0
145739 * @access public
145740 * @var bool
145741 */
145742 var $is_search = false;
145743
145744 /**
145745 * Set if query is feed display.
145746 *
145747 * @since 1.5.0
145748 * @access public
145749 * @var bool
145750 */
145751 var $is_feed = false;
145752
145753 /**
145754 * Set if query is comment feed display.
145755 *
145756 * @since 2.2.0
145757 * @access public
145758 * @var bool
145759 */
145760 var $is_comment_feed = false;
145761
145762 /**
145763 * Set if query is trackback.
145764 *
145765 * @since 1.5.0
145766 * @access public
145767 * @var bool
145768 */
145769 var $is_trackback = false;
145770
145771 /**
145772 * Set if query is blog homepage.
145773 *
145774 * @since 1.5.0
145775 * @access public
145776 * @var bool
145777 */
145778 var $is_home = false;
145779
145780 /**
145781 * Set if query couldn't found anything.
145782 *
145783 * @since 1.5.0
145784 * @access public
145785 * @var bool
145786 */
145787 var $is_404 = false;
145788
145789 /**
145790 * Set if query is within comments popup window.
145791 *
145792 * @since 1.5.0
145793 * @access public
145794 * @var bool
145795 */
145796 var $is_comments_popup = false;
145797
145798 /**
145799 * Set if query is part of administration page.
145800 *
145801 * @since 1.5.0
145802 * @access public
145803 * @var bool
145804 */
145805 var $is_admin = false;
145806
145807 /**
145808 * Set if query is an attachment.
145809 *
145810 * @since 2.0.0
145811 * @access public
145812 * @var bool
145813 */
145814 var $is_attachment = false;
145815
145816 /**
145817 * Set if is single, is a page, or is an attachment.
145818 *
145819 * @since 2.1.0
145820 * @access public
145821 * @var bool
145822 */
145823 var $is_singular = false;
145824
145825 /**
145826 * Set if query is for robots.
145827 *
145828 * @since 2.1.0
145829 * @access public
145830 * @var bool
145831 */
145832 var $is_robots = false;
145833
145834 /**
145835 * Set if query contains posts.
145836 *
145837 * Basically, the homepage if the option isn't set for the static homepage.
145838 *
145839 * @since 2.1.0
145840 * @access public
145841 * @var bool
145842 */
145843 var $is_posts_page = false;
145844
145845 /**
145846 * Resets query flags to false.
145847 *
145848 * The query flags are what page info WordPress was able to figure out.
145849 *
145850 * @since 2.0.0
145851 * @access private
145852 */
145853 function init_query_flags() {
145854 $this->is_single = false;
145855 $this->is_page = false;
145856 $this->is_archive = false;
145857 $this->is_date = false;
145858 $this->is_year = false;
145859 $this->is_month = false;
145860 $this->is_day = false;
145861 $this->is_time = false;
145862 $this->is_author = false;
145863 $this->is_category = false;
145864 $this->is_tag = false;
145865 $this->is_tax = false;
145866 $this->is_search = false;
145867 $this->is_feed = false;
145868 $this->is_comment_feed = false;
145869 $this->is_trackback = false;
145870 $this->is_home = false;
145871 $this->is_404 = false;
145872 $this->is_paged = false;
145873 $this->is_admin = false;
145874 $this->is_attachment = false;
145875 $this->is_singular = false;
145876 $this->is_robots = false;
145877 $this->is_posts_page = false;
145878 }
145879
145880 /**
145881 * Initiates object properties and sets default values.
145882 *
145883 * @since 1.5.0
145884 * @access public
145885 */
145886 function init () {
145887 unset($this->posts);
145888 unset($this->query);
145889 $this->query_vars = array();
145890 unset($this->queried_object);
145891 unset($this->queried_object_id);
145892 $this->post_count = 0;
145893 $this->current_post = -1;
145894 $this->in_the_loop = false;
145895
145896 $this->init_query_flags();
145897 }
145898
145899 /**
145900 * Reparse the query vars.
145901 *
145902 * @since 1.5.0
145903 * @access public
145904 */
145905 function parse_query_vars() {
145906 $this->parse_query('');
145907 }
145908
145909 /**
145910 * Fills in the query variables, which do not exist within the parameter.
145911 *
145912 * @since 2.1.0
145913 * @access public
145914 *
145915 * @param array $array Defined query variables.
145916 * @return array Complete query variables with undefined ones filled in empty.
145917 */
145918 function fill_query_vars($array) {
145919 $keys = array(
145920 'error'
145921 , 'm'
145922 , 'p'
145923 , 'post_parent'
145924 , 'subpost'
145925 , 'subpost_id'
145926 , 'attachment'
145927 , 'attachment_id'
145928 , 'name'
145929 , 'hour'
145930 , 'static'
145931 , 'pagename'
145932 , 'page_id'
145933 , 'second'
145934 , 'minute'
145935 , 'hour'
145936 , 'day'
145937 , 'monthnum'
145938 , 'year'
145939 , 'w'
145940 , 'category_name'
145941 , 'tag'
145942 , 'cat'
145943 , 'tag_id'
145944 , 'author_name'
145945 , 'feed'
145946 , 'tb'
145947 , 'paged'
145948 , 'comments_popup'
145949 , 'meta_key'
145950 , 'meta_value'
145951 , 'preview'
145952 );
145953
145954 foreach ($keys as $key) {
145955 if ( !isset($array[$key]))
145956 $array[$key] = '';
145957 }
145958
145959 $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
145960 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
145961
145962 foreach ( $array_keys as $key ) {
145963 if ( !isset($array[$key]))
145964 $array[$key] = array();
145965 }
145966 return $array;
145967 }
145968
145969 /**
145970 * Parse a query string and set query type booleans.
145971 *
145972 * @since 1.5.0
145973 * @access public
145974 *
145975 * @param string|array $query
145976 */
145977 function parse_query ($query) {
145978 if ( !empty($query) || !isset($this->query) ) {
145979 $this->init();
145980 if ( is_array($query) )
145981 $this->query_vars = $query;
145982 else
145983 parse_str($query, $this->query_vars);
145984 $this->query = $query;
145985 }
145986
145987 $this->query_vars = $this->fill_query_vars($this->query_vars);
145988 $qv = &$this->query_vars;
145989
145990 if ( ! empty($qv['robots']) )
145991 $this->is_robots = true;
145992
145993 $qv['p'] = absint($qv['p']);
145994 $qv['page_id'] = absint($qv['page_id']);
145995 $qv['year'] = absint($qv['year']);
145996 $qv['monthnum'] = absint($qv['monthnum']);
145997 $qv['day'] = absint($qv['day']);
145998 $qv['w'] = absint($qv['w']);
145999 $qv['m'] = absint($qv['m']);
146000 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
146001 $qv['pagename'] = trim( $qv['pagename'] );
146002 $qv['name'] = trim( $qv['name'] );
146003 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
146004 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
146005 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
146006
146007 // Compat. Map subpost to attachment.
146008 if ( '' != $qv['subpost'] )
146009 $qv['attachment'] = $qv['subpost'];
146010 if ( '' != $qv['subpost_id'] )
146011 $qv['attachment_id'] = $qv['subpost_id'];
146012
146013 $qv['attachment_id'] = absint($qv['attachment_id']);
146014
146015 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
146016 $this->is_single = true;
146017 $this->is_attachment = true;
146018 } elseif ( '' != $qv['name'] ) {
146019 $this->is_single = true;
146020 } elseif ( $qv['p'] ) {
146021 $this->is_single = true;
146022 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
146023 // If year, month, day, hour, minute, and second are set, a single
146024 // post is being queried.
146025 $this->is_single = true;
146026 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
146027 $this->is_page = true;
146028 $this->is_single = false;
146029 } elseif ( !empty($qv['s']) ) {
146030 $this->is_search = true;
146031 } else {
146032 // Look for archive queries. Dates, categories, authors.
146033
146034 if ( '' !== $qv['second'] ) {
146035 $this->is_time = true;
146036 $this->is_date = true;
146037 }
146038
146039 if ( '' !== $qv['minute'] ) {
146040 $this->is_time = true;
146041 $this->is_date = true;
146042 }
146043
146044 if ( '' !== $qv['hour'] ) {
146045 $this->is_time = true;
146046 $this->is_date = true;
146047 }
146048
146049 if ( $qv['day'] ) {
146050 if (! $this->is_date) {
146051 $this->is_day = true;
146052 $this->is_date = true;
146053 }
146054 }
146055
146056 if ( $qv['monthnum'] ) {
146057 if (! $this->is_date) {
146058 $this->is_month = true;
146059 $this->is_date = true;
146060 }
146061 }
146062
146063 if ( $qv['year'] ) {
146064 if (! $this->is_date) {
146065 $this->is_year = true;
146066 $this->is_date = true;
146067 }
146068 }
146069
146070 if ( $qv['m'] ) {
146071 $this->is_date = true;
146072 if (strlen($qv['m']) > 9) {
146073 $this->is_time = true;
146074 } else if (strlen($qv['m']) > 7) {
146075 $this->is_day = true;
146076 } else if (strlen($qv['m']) > 5) {
146077 $this->is_month = true;
146078 } else {
146079 $this->is_year = true;
146080 }
146081 }
146082
146083 if ('' != $qv['w']) {
146084 $this->is_date = true;
146085 }
146086
146087 if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
146088 $this->is_category = false;
146089 } else {
146090 if (strpos($qv['cat'], '-') !== false) {
146091 $this->is_category = false;
146092 } else {
146093 $this->is_category = true;
146094 }
146095 }
146096
146097 if ( '' != $qv['category_name'] ) {
146098 $this->is_category = true;
146099 }
146100
146101 if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
146102 $qv['category__in'] = array();
146103 } else {
146104 $qv['category__in'] = array_map('absint', $qv['category__in']);
146105 $this->is_category = true;
146106 }
146107
146108 if ( !is_array($qv['category__not_in']) || empty($qv['category__not_in']) ) {
146109 $qv['category__not_in'] = array();
146110 } else {
146111 $qv['category__not_in'] = array_map('absint', $qv['category__not_in']);
146112 }
146113
146114 if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
146115 $qv['category__and'] = array();
146116 } else {
146117 $qv['category__and'] = array_map('absint', $qv['category__and']);
146118 $this->is_category = true;
146119 }
146120
146121 if ( '' != $qv['tag'] )
146122 $this->is_tag = true;
146123
146124 $qv['tag_id'] = absint($qv['tag_id']);
146125 if ( !empty($qv['tag_id']) )
146126 $this->is_tag = true;
146127
146128 if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
146129 $qv['tag__in'] = array();
146130 } else {
146131 $qv['tag__in'] = array_map('absint', $qv['tag__in']);
146132 $this->is_tag = true;
146133 }
146134
146135 if ( !is_array($qv['tag__not_in']) || empty($qv['tag__not_in']) ) {
146136 $qv['tag__not_in'] = array();
146137 } else {
146138 $qv['tag__not_in'] = array_map('absint', $qv['tag__not_in']);
146139 }
146140
146141 if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
146142 $qv['tag__and'] = array();
146143 } else {
146144 $qv['tag__and'] = array_map('absint', $qv['tag__and']);
146145 $this->is_category = true;
146146 }
146147
146148 if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
146149 $qv['tag_slug__in'] = array();
146150 } else {
146151 $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
146152 $this->is_tag = true;
146153 }
146154
146155 if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
146156 $qv['tag_slug__and'] = array();
146157 } else {
146158 $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
146159 $this->is_tag = true;
146160 }
146161
146162 if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
146163 $this->is_tax = false;
146164 foreach ( $GLOBALS['wp_taxonomies'] as $t ) {
146165 if ( isset($t->query_var) && isset($qv[$t->query_var]) && '' != $qv[$t->query_var] ) {
146166 $this->is_tax = true;
146167 break;
146168 }
146169 }
146170 } else {
146171 $this->is_tax = true;
146172 }
146173
146174 if ( empty($qv['author']) || ($qv['author'] == '0') ) {
146175 $this->is_author = false;
146176 } else {
146177 $this->is_author = true;
146178 }
146179
146180 if ( '' != $qv['author_name'] ) {
146181 $this->is_author = true;
146182 }
146183
146184 if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax) )
146185 $this->is_archive = true;
146186 }
146187
146188 if ( '' != $qv['feed'] )
146189 $this->is_feed = true;
146190
146191 if ( '' != $qv['tb'] )
146192 $this->is_trackback = true;
146193
146194 if ( '' != $qv['paged'] )
146195 $this->is_paged = true;
146196
146197 if ( '' != $qv['comments_popup'] )
146198 $this->is_comments_popup = true;
146199
146200 // if we're previewing inside the write screen
146201 if ('' != $qv['preview'])
146202 $this->is_preview = true;
146203
146204 if ( is_admin() )
146205 $this->is_admin = true;
146206
146207 if ( false !== strpos($qv['feed'], 'comments-') ) {
146208 $qv['feed'] = str_replace('comments-', '', $qv['feed']);
146209 $qv['withcomments'] = 1;
146210 }
146211
146212 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
146213
146214 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
146215 $this->is_comment_feed = true;
146216
146217 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 ) )
146218 $this->is_home = true;
146219
146220 // Correct is_* for page_on_front and page_for_posts
146221 if ( $this->is_home && ( empty($this->query) || $qv['preview'] == 'true' ) && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
146222 $this->is_page = true;
146223 $this->is_home = false;
146224 $qv['page_id'] = get_option('page_on_front');
146225 }
146226
146227 if ( '' != $qv['pagename'] ) {
146228 $this->queried_object =& get_page_by_path($qv['pagename']);
146229 if ( !empty($this->queried_object) )
146230 $this->queried_object_id = (int) $this->queried_object->ID;
146231 else
146232 unset($this->queried_object);
146233
146234 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
146235 $this->is_page = false;
146236 $this->is_home = true;
146237 $this->is_posts_page = true;
146238 }
146239 }
146240
146241 if ( $qv['page_id'] ) {
146242 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
146243 $this->is_page = false;
146244 $this->is_home = true;
146245 $this->is_posts_page = true;
146246 }
146247 }
146248
146249 if ( !empty($qv['post_type']) )
146250 $qv['post_type'] = sanitize_user($qv['post_type'], true);
146251
146252 if ( !empty($qv['post_status']) )
146253 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
146254
146255 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
146256 $this->is_comment_feed = false;
146257
146258 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
146259 // Done correcting is_* for page_on_front and page_for_posts
146260
146261 if ('404' == $qv['error'])
146262 $this->set_404();
146263
146264 if ( !empty($query) )
146265 do_action_ref_array('parse_query', array(&$this));
146266 }
146267
146268 /**
146269 * Sets the 404 property and saves whether query is feed.
146270 *
146271 * @since 2.0.0
146272 * @access public
146273 */
146274 function set_404() {
146275 $is_feed = $this->is_feed;
146276
146277 $this->init_query_flags();
146278 $this->is_404 = true;
146279
146280 $this->is_feed = $is_feed;
146281 }
146282
146283 /**
146284 * Retrieve query variable.
146285 *
146286 * @since 1.5.0
146287 * @access public
146288 *
146289 * @param string $query_var Query variable key.
146290 * @return mixed
146291 */
146292 function get($query_var) {
146293 if (isset($this->query_vars[$query_var])) {
146294 return $this->query_vars[$query_var];
146295 }
146296
146297 return '';
146298 }
146299
146300 /**
146301 * Set query variable.
146302 *
146303 * @since 1.5.0
146304 * @access public
146305 *
146306 * @param string $query_var Query variable key.
146307 * @param mixed $value Query variable value.
146308 */
146309 function set($query_var, $value) {
146310 $this->query_vars[$query_var] = $value;
146311 }
146312
146313 /**
146314 * Retrieve the posts based on query variables.
146315 *
146316 * There are a few filters and actions that can be used to modify the post
146317 * database query.
146318 *
146319 * @since 1.5.0
146320 * @access public
146321 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
146322 *
146323 * @return array List of posts.
146324 */
146325 function &get_posts() {
146326 global $wpdb, $user_ID;
146327
146328 do_action_ref_array('pre_get_posts', array(&$this));
146329
146330 // Shorthand.
146331 $q = &$this->query_vars;
146332
146333 $q = $this->fill_query_vars($q);
146334
146335 // First let's clear some variables
146336 $distinct = '';
146337 $whichcat = '';
146338 $whichauthor = '';
146339 $whichmimetype = '';
146340 $where = '';
146341 $limits = '';
146342 $join = '';
146343 $search = '';
146344 $groupby = '';
146345 $fields = "$wpdb->posts.*";
146346 $post_status_join = false;
146347 $page = 1;
146348
146349 if ( !isset($q['caller_get_posts']) )
146350 $q['caller_get_posts'] = false;
146351
146352 if ( !isset($q['suppress_filters']) )
146353 $q['suppress_filters'] = false;
146354
146355 if ( !isset($q['post_type']) ) {
146356 if ( $this->is_search )
146357 $q['post_type'] = 'any';
146358 else
146359 $q['post_type'] = 'post';
146360 }
146361 $post_type = $q['post_type'];
146362 if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
146363 $q['posts_per_page'] = get_option('posts_per_page');
146364 if ( isset($q['showposts']) && $q['showposts'] ) {
146365 $q['showposts'] = (int) $q['showposts'];
146366 $q['posts_per_page'] = $q['showposts'];
146367 }
146368 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
146369 $q['posts_per_page'] = $q['posts_per_archive_page'];
146370 if ( !isset($q['nopaging']) ) {
146371 if ($q['posts_per_page'] == -1) {
146372 $q['nopaging'] = true;
146373 } else {
146374 $q['nopaging'] = false;
146375 }
146376 }
146377 if ( $this->is_feed ) {
146378 $q['posts_per_page'] = get_option('posts_per_rss');
146379 $q['nopaging'] = false;
146380 }
146381 $q['posts_per_page'] = (int) $q['posts_per_page'];
146382 if ( $q['posts_per_page'] < -1 )
146383 $q['posts_per_page'] = abs($q['posts_per_page']);
146384 else if ( $q['posts_per_page'] == 0 )
146385 $q['posts_per_page'] = 1;
146386
146387 if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
146388 $q['comments_per_page'] = get_option('comments_per_page');
146389
146390 if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
146391 $this->is_page = true;
146392 $this->is_home = false;
146393 $q['page_id'] = get_option('page_on_front');
146394 }
146395
146396 if (isset($q['page'])) {
146397 $q['page'] = trim($q['page'], '/');
146398 $q['page'] = absint($q['page']);
146399 }
146400
146401 // If a month is specified in the querystring, load that month
146402 if ( $q['m'] ) {
146403 $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
146404 $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
146405 if (strlen($q['m'])>5)
146406 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
146407 if (strlen($q['m'])>7)
146408 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
146409 if (strlen($q['m'])>9)
146410 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
146411 if (strlen($q['m'])>11)
146412 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
146413 if (strlen($q['m'])>13)
146414 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
146415 }
146416
146417 if ( '' !== $q['hour'] )
146418 $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
146419
146420 if ( '' !== $q['minute'] )
146421 $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
146422
146423 if ( '' !== $q['second'] )
146424 $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
146425
146426 if ( $q['year'] )
146427 $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
146428
146429 if ( $q['monthnum'] )
146430 $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
146431
146432 if ( $q['day'] )
146433 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
146434
146435 if ('' != $q['name']) {
146436 $q['name'] = sanitize_title($q['name']);
146437 $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
146438 } else if ('' != $q['pagename']) {
146439 if ( isset($this->queried_object_id) )
146440 $reqpage = $this->queried_object_id;
146441 else {
146442 $reqpage = get_page_by_path($q['pagename']);
146443 if ( !empty($reqpage) )
146444 $reqpage = $reqpage->ID;
146445 else
146446 $reqpage = 0;
146447 }
146448
146449 $page_for_posts = get_option('page_for_posts');
146450 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
146451 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
146452 $page_paths = '/' . trim($q['pagename'], '/');
146453 $q['pagename'] = sanitize_title(basename($page_paths));
146454 $q['name'] = $q['pagename'];
146455 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
146456 $reqpage_obj = get_page($reqpage);
146457 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
146458 $this->is_attachment = true;
146459 $this->is_page = true;
146460 $q['attachment_id'] = $reqpage;
146461 }
146462 }
146463 } elseif ('' != $q['attachment']) {
146464 $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
146465 $attach_paths = '/' . trim($q['attachment'], '/');
146466 $q['attachment'] = sanitize_title(basename($attach_paths));
146467 $q['name'] = $q['attachment'];
146468 $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
146469 }
146470
146471 if ( $q['w'] )
146472 $where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'";
146473
146474 if ( intval($q['comments_popup']) )
146475 $q['p'] = absint($q['comments_popup']);
146476
146477 // If an attachment is requested by number, let it supercede any post number.
146478 if ( $q['attachment_id'] )
146479 $q['p'] = absint($q['attachment_id']);
146480
146481 // If a post number is specified, load that post
146482 if ( $q['p'] ) {
146483 $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
146484 } elseif ( $q['post__in'] ) {
146485 $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
146486 $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
146487 } elseif ( $q['post__not_in'] ) {
146488 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
146489 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
146490 }
146491
146492 if ( is_numeric($q['post_parent']) )
146493 $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
146494
146495 if ( $q['page_id'] ) {
146496 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
146497 $q['p'] = $q['page_id'];
146498 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
146499 }
146500 }
146501
146502 // If a search pattern is specified, load the posts that match
146503 if ( !empty($q['s']) ) {
146504 // added slashes screw with quote grouping when done early, so done later
146505 $q['s'] = stripslashes($q['s']);
146506 if ( !empty($q['sentence']) ) {
146507 $q['search_terms'] = array($q['s']);
146508 } else {
146509 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
146510 $q['search_terms'] = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
146511 }
146512 $n = !empty($q['exact']) ? '' : '%';
146513 $searchand = '';
146514 foreach( (array) $q['search_terms'] as $term) {
146515 $term = addslashes_gpc($term);
146516 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
146517 $searchand = ' AND ';
146518 }
146519 $term = $wpdb->escape($q['s']);
146520 if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
146521 $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
146522
146523 if ( !empty($search) )
146524 $search = " AND ({$search}) ";
146525 }
146526
146527 // Category stuff
146528
146529 if ( empty($q['cat']) || ($q['cat'] == '0') ||
146530 // Bypass cat checks if fetching specific posts
146531 $this->is_singular ) {
146532 $whichcat = '';
146533 } else {
146534 $q['cat'] = ''.urldecode($q['cat']).'';
146535 $q['cat'] = addslashes_gpc($q['cat']);
146536 $cat_array = preg_split('/[,\s]+/', $q['cat']);
146537 $q['cat'] = '';
146538 $req_cats = array();
146539 foreach ( (array) $cat_array as $cat ) {
146540 $cat = intval($cat);
146541 $req_cats[] = $cat;
146542 $in = ($cat > 0);
146543 $cat = abs($cat);
146544 if ( $in ) {
146545 $q['category__in'][] = $cat;
146546 $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
146547 } else {
146548 $q['category__not_in'][] = $cat;
146549 $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
146550 }
146551 }
146552 $q['cat'] = implode(',', $req_cats);
146553 }
146554
146555 if ( !empty($q['category__in']) ) {
146556 $groupby = "{$wpdb->posts}.ID";
146557 }
146558
146559 if ( !empty($q['category__in']) ) {
146560 $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) ";
146561 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
146562 $include_cats = "'" . implode("', '", $q['category__in']) . "'";
146563 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
146564 }
146565
146566 if ( !empty($q['category__not_in']) ) {
146567 if ( $wpdb->has_cap( 'subqueries' ) ) {
146568 $cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
146569 $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN ($cat_string) )";
146570 } else {
146571 $ids = get_objects_in_term($q['category__not_in'], 'category');
146572 if ( is_wp_error( $ids ) )
146573 $ids = array();
146574 if ( is_array($ids) && count($ids > 0) ) {
146575 $out_posts = "'" . implode("', '", $ids) . "'";
146576 $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
146577 }
146578 }
146579 }
146580
146581 // Category stuff for nice URLs
146582 if ( '' != $q['category_name'] && !$this->is_singular ) {
146583 $reqcat = get_category_by_path($q['category_name']);
146584 $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
146585 $cat_paths = '/' . trim($q['category_name'], '/');
146586 $q['category_name'] = sanitize_title(basename($cat_paths));
146587
146588 $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
146589 $q['category_name'] = sanitize_title(basename($cat_paths));
146590 $cat_paths = explode('/', $cat_paths);
146591 $cat_path = '';
146592 foreach ( (array) $cat_paths as $pathdir )
146593 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
146594
146595 //if we don't match the entire hierarchy fallback on just matching the nicename
146596 if ( empty($reqcat) )
146597 $reqcat = get_category_by_path($q['category_name'], false);
146598
146599 if ( !empty($reqcat) )
146600 $reqcat = $reqcat->term_id;
146601 else
146602 $reqcat = 0;
146603
146604 $q['cat'] = $reqcat;
146605
146606 $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) ";
146607 $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
146608 $in_cats = array($q['cat']);
146609 $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
146610 $in_cats = "'" . implode("', '", $in_cats) . "'";
146611 $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
146612 $groupby = "{$wpdb->posts}.ID";
146613 }
146614
146615 // Tags
146616 if ( '' != $q['tag'] ) {
146617 if ( strpos($q['tag'], ',') !== false ) {
146618 $tags = preg_split('/[,\s]+/', $q['tag']);
146619 foreach ( (array) $tags as $tag ) {
146620 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
146621 $q['tag_slug__in'][] = $tag;
146622 }
146623 } else if ( preg_match('/[+\s]+/', $q['tag']) ) {
146624 $tags = preg_split('/[+\s]+/', $q['tag']);
146625 foreach ( (array) $tags as $tag ) {
146626 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
146627 $q['tag_slug__and'][] = $tag;
146628 }
146629 } else {
146630 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
146631 $q['tag_slug__in'][] = $q['tag'];
146632 }
146633 }
146634
146635 if ( !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) {
146636 $groupby = "{$wpdb->posts}.ID";
146637 }
146638
146639 if ( !empty($q['tag__in']) ) {
146640 $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) ";
146641 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
146642 $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
146643 $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
146644 $reqtag = is_term( $q['tag__in'][0], 'post_tag' );
146645 if ( !empty($reqtag) )
146646 $q['tag_id'] = $reqtag['term_id'];
146647 }
146648
146649 if ( !empty($q['tag_slug__in']) ) {
146650 $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) ";
146651 $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
146652 $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
146653 $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
146654 $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
146655 if ( !empty($reqtag) )
146656 $q['tag_id'] = $reqtag->term_id;
146657 }
146658
146659 if ( !empty($q['tag__not_in']) ) {
146660 if ( $wpdb->has_cap( 'subqueries' ) ) {
146661 $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'";
146662 $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'post_tag' AND tt.term_id IN ($tag_string) )";
146663 } else {
146664 $ids = get_objects_in_term($q['tag__not_in'], 'post_tag');
146665 if ( is_wp_error( $ids ) )
146666 $ids = array();
146667 if ( is_array($ids) && count($ids > 0) ) {
146668 $out_posts = "'" . implode("', '", $ids) . "'";
146669 $whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
146670 }
146671 }
146672 }
146673
146674 // Tag and slug intersections.
146675 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag');
146676 foreach ($intersections as $item => $taxonomy) {
146677 if ( empty($q[$item]) ) continue;
146678
146679 if ( $item != 'category__and' ) {
146680 $reqtag = is_term( $q[$item][0], 'post_tag' );
146681 if ( !empty($reqtag) )
146682 $q['tag_id'] = $reqtag['term_id'];
146683 }
146684
146685 $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id';
146686
146687 $q[$item] = array_unique($q[$item]);
146688 $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)";
146689 $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
146690 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
146691
146692 $post_ids = $wpdb->get_col($tsql);
146693
146694 if ( count($post_ids) )
146695 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
146696 else {
146697 $whichcat = " AND 0 = 1";
146698 break;
146699 }
146700 }
146701
146702 // Taxonomies
146703 if ( $this->is_tax ) {
146704 if ( '' != $q['taxonomy'] ) {
146705 $taxonomy = $q['taxonomy'];
146706 $tt[$taxonomy] = $q['term'];
146707 $terms = get_terms($q['taxonomy'], array('slug'=>$q['term']));
146708 } else {
146709 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
146710 if ( isset($t->query_var) && '' != $q[$t->query_var] ) {
146711 $terms = get_terms($taxonomy, array('slug'=>$q[$t->query_var]));
146712 if ( !is_wp_error($terms) )
146713 break;
146714 }
146715 }
146716 }
146717 if ( is_wp_error($terms) || empty($terms) ) {
146718 $whichcat = " AND 0 ";
146719 } else {
146720 foreach ( $terms as $term )
146721 $term_ids[] = $term->term_id;
146722 $post_ids = get_objects_in_term($term_ids, $taxonomy);
146723 if ( !is_wp_error($post_ids) && count($post_ids) ) {
146724 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
146725 $post_type = 'any';
146726 $q['post_status'] = 'publish';
146727 $post_status_join = true;
146728 } else {
146729 $whichcat = " AND 0 ";
146730 }
146731 }
146732 }
146733
146734 // Author/user stuff
146735
146736 if ( empty($q['author']) || ($q['author'] == '0') ) {
146737 $whichauthor='';
146738 } else {
146739 $q['author'] = ''.urldecode($q['author']).'';
146740 $q['author'] = addslashes_gpc($q['author']);
146741 if (strpos($q['author'], '-') !== false) {
146742 $eq = '!=';
146743 $andor = 'AND';
146744 $q['author'] = explode('-', $q['author']);
146745 $q['author'] = '' . absint($q['author'][1]);
146746 } else {
146747 $eq = '=';
146748 $andor = 'OR';
146749 }
146750 $author_array = preg_split('/[,\s]+/', $q['author']);
146751 $whichauthor .= " AND ($wpdb->posts.post_author ".$eq.' '.absint($author_array[0]);
146752 for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
146753 $whichauthor .= ' '.$andor." $wpdb->posts.post_author ".$eq.' '.absint($author_array[$i]);
146754 }
146755 $whichauthor .= ')';
146756 }
146757
146758 // Author stuff for nice URLs
146759
146760 if ('' != $q['author_name']) {
146761 if (strpos($q['author_name'], '/') !== false) {
146762 $q['author_name'] = explode('/',$q['author_name']);
146763 if ($q['author_name'][count($q['author_name'])-1]) {
146764 $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
146765 } else {
146766 $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
146767 }
146768 }
146769 $q['author_name'] = sanitize_title($q['author_name']);
146770 $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
146771 $whichauthor .= " AND ($wpdb->posts.post_author = ".absint($q['author']).')';
146772 }
146773
146774 // MIME-Type stuff for attachment browsing
146775
146776 if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] )
146777 $whichmimetype = wp_post_mime_type_where($q['post_mime_type']);
146778
146779 $where .= $search.$whichcat.$whichauthor.$whichmimetype;
146780
146781 if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
146782 $q['order'] = 'DESC';
146783
146784 // Order by
146785 if ( empty($q['orderby']) ) {
146786 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
146787 } else {
146788 // Used to filter values
146789 $allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand');
146790 if ( !empty($q['meta_key']) ) {
146791 $allowed_keys[] = $q['meta_key'];
146792 $allowed_keys[] = 'meta_value';
146793 }
146794 $q['orderby'] = urldecode($q['orderby']);
146795 $q['orderby'] = addslashes_gpc($q['orderby']);
146796 $orderby_array = explode(' ',$q['orderby']);
146797 if ( empty($orderby_array) )
146798 $orderby_array[] = $q['orderby'];
146799 $q['orderby'] = '';
146800 for ($i = 0; $i < count($orderby_array); $i++) {
146801 // Only allow certain values for safety
146802 $orderby = $orderby_array[$i];
146803 switch ($orderby) {
146804 case 'menu_order':
146805 break;
146806 case 'ID':
146807 $orderby = "$wpdb->posts.ID";
146808 break;
146809 case 'rand':
146810 $orderby = 'RAND()';
146811 break;
146812 case $q['meta_key']:
146813 case 'meta_value':
146814 $orderby = "$wpdb->postmeta.meta_value";
146815 break;
146816 default:
146817 $orderby = "$wpdb->posts.post_" . $orderby;
146818 }
146819 if ( in_array($orderby_array[$i], $allowed_keys) )
146820 $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
146821 }
146822 // append ASC or DESC at the end
146823 if ( !empty($q['orderby']))
146824 $q['orderby'] .= " {$q['order']}";
146825
146826 if ( empty($q['orderby']) )
146827 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
146828 }
146829
146830 if ( $this->is_attachment ) {
146831 $where .= " AND $wpdb->posts.post_type = 'attachment'";
146832 } elseif ($this->is_page) {
146833 $where .= " AND $wpdb->posts.post_type = 'page'";
146834 } elseif ($this->is_single) {
146835 $where .= " AND $wpdb->posts.post_type = 'post'";
146836 } elseif ( 'any' == $post_type ) {
146837 $where .= '';
146838 } else {
146839 $where .= " AND $wpdb->posts.post_type = '$post_type'";
146840 }
146841
146842 if ( isset($q['post_status']) && '' != $q['post_status'] ) {
146843 $statuswheres = array();
146844 $q_status = explode(',', $q['post_status']);
146845 $r_status = array();
146846 $p_status = array();
146847 if ( in_array( 'draft' , $q_status ) )
146848 $r_status[] = "$wpdb->posts.post_status = 'draft'";
146849 if ( in_array( 'pending', $q_status ) )
146850 $r_status[] = "$wpdb->posts.post_status = 'pending'";
146851 if ( in_array( 'future' , $q_status ) )
146852 $r_status[] = "$wpdb->posts.post_status = 'future'";
146853 if ( in_array( 'inherit' , $q_status ) )
146854 $r_status[] = "$wpdb->posts.post_status = 'inherit'";
146855 if ( in_array( 'private', $q_status ) )
146856 $p_status[] = "$wpdb->posts.post_status = 'private'";
146857 if ( in_array( 'publish', $q_status ) )
146858 $r_status[] = "$wpdb->posts.post_status = 'publish'";
146859
146860 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
146861 $r_status = array_merge($r_status, $p_status);
146862 unset($p_status);
146863 }
146864
146865 if ( !empty($r_status) ) {
146866 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type}s") )
146867 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
146868 else
146869 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
146870 }
146871 if ( !empty($p_status) ) {
146872 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type}s") )
146873 $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
146874 else
146875 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
146876 }
146877 if ( $post_status_join ) {
146878 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
146879 foreach ( $statuswheres as $index => $statuswhere )
146880 $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
146881 }
146882 foreach ( $statuswheres as $statuswhere )
146883 $where .= " AND $statuswhere";
146884 } elseif ( !$this->is_singular ) {
146885 $where .= " AND ($wpdb->posts.post_status = 'publish'";
146886
146887 if ( is_admin() )
146888 $where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'";
146889
146890 if ( is_user_logged_in() ) {
146891 $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'";
146892 }
146893
146894 $where .= ')';
146895 }
146896
146897 // postmeta queries
146898 if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
146899 $join .= " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
146900 if ( ! empty($q['meta_key']) )
146901 $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
146902 if ( ! empty($q['meta_value']) ) {
146903 if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
146904 $q['meta_compare'] = '=';
146905
146906 $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
146907 }
146908
146909 // Apply filters on where and join prior to paging so that any
146910 // manipulations to them are reflected in the paging by day queries.
146911 if ( !$q['suppress_filters'] ) {
146912 $where = apply_filters('posts_where', $where);
146913 $join = apply_filters('posts_join', $join);
146914 }
146915
146916 // Paging
146917 if ( empty($q['nopaging']) && !$this->is_singular ) {
146918 $page = absint($q['paged']);
146919 if (empty($page)) {
146920 $page = 1;
146921 }
146922
146923 if ( empty($q['offset']) ) {
146924 $pgstrt = '';
146925 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
146926 $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
146927 } else { // we're ignoring $page and using 'offset'
146928 $q['offset'] = absint($q['offset']);
146929 $pgstrt = $q['offset'] . ', ';
146930 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
146931 }
146932 }
146933
146934 // Comments feeds
146935 if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
146936 if ( $this->is_archive || $this->is_search ) {
146937 $cjoin = "LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
146938 $cwhere = "WHERE comment_approved = '1' $where";
146939 $cgroupby = "GROUP BY $wpdb->comments.comment_id";
146940 } else { // Other non singular e.g. front
146941 $cjoin = "LEFT JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
146942 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
146943 $cgroupby = '';
146944 }
146945
146946 if ( !$q['suppress_filters'] ) {
146947 $cjoin = apply_filters('comment_feed_join', $cjoin);
146948 $cwhere = apply_filters('comment_feed_where', $cwhere);
146949 $cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
146950 }
146951
146952 $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'));
146953 $this->comment_count = count($this->comments);
146954
146955 $post_ids = array();
146956
146957 foreach ($this->comments as $comment)
146958 $post_ids[] = (int) $comment->comment_post_ID;
146959
146960 $post_ids = join(',', $post_ids);
146961 $join = '';
146962 if ( $post_ids )
146963 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
146964 else
146965 $where = "AND 0";
146966 }
146967
146968 $orderby = $q['orderby'];
146969
146970 // Apply post-paging filters on where and join. Only plugins that
146971 // manipulate paging queries should use these hooks.
146972 if ( !$q['suppress_filters'] ) {
146973 $where = apply_filters('posts_where_paged', $where);
146974 $groupby = apply_filters('posts_groupby', $groupby);
146975 $join = apply_filters('posts_join_paged', $join);
146976 $orderby = apply_filters('posts_orderby', $orderby);
146977 $distinct = apply_filters('posts_distinct', $distinct);
146978 $limits = apply_filters( 'post_limits', $limits );
146979
146980 if ( ! empty($q['meta_key']) )
146981 $fields = "$fields, $wpdb->postmeta.meta_value";
146982
146983 $fields = apply_filters('posts_fields', $fields);
146984 }
146985
146986 // Announce current selection parameters. For use by caching plugins.
146987 do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
146988
146989 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
146990 if ( !$q['suppress_filters'] ) {
146991 $where = apply_filters('posts_where_request', $where);
146992 $groupby = apply_filters('posts_groupby_request', $groupby);
146993 $join = apply_filters('posts_join_request', $join);
146994 $orderby = apply_filters('posts_orderby_request', $orderby);
146995 $distinct = apply_filters('posts_distinct_request', $distinct);
146996 $fields = apply_filters('posts_fields_request', $fields);
146997 $limits = apply_filters( 'post_limits_request', $limits );
146998 }
146999
147000 if ( ! empty($groupby) )
147001 $groupby = 'GROUP BY ' . $groupby;
147002 if ( !empty( $orderby ) )
147003 $orderby = 'ORDER BY ' . $orderby;
147004 $found_rows = '';
147005 if ( !empty($limits) )
147006 $found_rows = 'SQL_CALC_FOUND_ROWS';
147007
147008 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
147009 if ( !$q['suppress_filters'] )
147010 $this->request = apply_filters('posts_request', $this->request);
147011
147012 $this->posts = $wpdb->get_results($this->request);
147013 // Raw results filter. Prior to status checks.
147014 if ( !$q['suppress_filters'] )
147015 $this->posts = apply_filters('posts_results', $this->posts);
147016
147017 if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
147018 $cjoin = apply_filters('comment_feed_join', '');
147019 $cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'");
147020 $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere ORDER BY comment_date_gmt DESC LIMIT " . get_option('posts_per_rss');
147021 $this->comments = $wpdb->get_results($comments_request);
147022 $this->comment_count = count($this->comments);
147023 }
147024
147025 if ( !empty($limits) ) {
147026 $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
147027 $this->found_posts = $wpdb->get_var( $found_posts_query );
147028 $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
147029 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
147030 }
147031
147032 // Check post status to determine if post should be displayed.
147033 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
147034 $status = get_post_status($this->posts[0]);
147035 //$type = get_post_type($this->posts[0]);
147036 if ( ('publish' != $status) ) {
147037 if ( ! is_user_logged_in() ) {
147038 // User must be logged in to view unpublished posts.
147039 $this->posts = array();
147040 } else {
147041 if (in_array($status, array('draft', 'pending')) ) {
147042 // User must have edit permissions on the draft to preview.
147043 if (! current_user_can('edit_post', $this->posts[0]->ID)) {
147044 $this->posts = array();
147045 } else {
147046 $this->is_preview = true;
147047 $this->posts[0]->post_date = current_time('mysql');
147048 }
147049 } else if ('future' == $status) {
147050 $this->is_preview = true;
147051 if (!current_user_can('edit_post', $this->posts[0]->ID)) {
147052 $this->posts = array ( );
147053 }
147054 } else {
147055 if (! current_user_can('read_post', $this->posts[0]->ID))
147056 $this->posts = array();
147057 }
147058 }
147059 }
147060
147061 if ( $this->is_preview && current_user_can( "edit_{$post_type}", $this->posts[0]->ID ) )
147062 $this->posts[0] = apply_filters('the_preview', $this->posts[0]);
147063 }
147064
147065 // Put sticky posts at the top of the posts array
147066 $sticky_posts = get_option('sticky_posts');
147067 if ( $this->is_home && $page <= 1 && !empty($sticky_posts) && !$q['caller_get_posts'] ) {
147068 $num_posts = count($this->posts);
147069 $sticky_offset = 0;
147070 // Loop over posts and relocate stickies to the front.
147071 for ( $i = 0; $i < $num_posts; $i++ ) {
147072 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
147073 $sticky_post = $this->posts[$i];
147074 // Remove sticky from current position
147075 array_splice($this->posts, $i, 1);
147076 // Move to front, after other stickies
147077 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
147078 // Increment the sticky offset. The next sticky will be placed at this offset.
147079 $sticky_offset++;
147080 // Remove post from sticky posts array
147081 $offset = array_search($sticky_post->ID, $sticky_posts);
147082 array_splice($sticky_posts, $offset, 1);
147083 }
147084 }
147085
147086 // Fetch sticky posts that weren't in the query results
147087 if ( !empty($sticky_posts) ) {
147088 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
147089 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in)" );
147090 /** @todo Make sure post is published or viewable by the current user */
147091 foreach ( $stickies as $sticky_post ) {
147092 if ( 'publish' != $sticky_post->post_status )
147093 continue;
147094 array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
147095 $sticky_offset++;
147096 }
147097 }
147098 }
147099
147100 if ( !$q['suppress_filters'] )
147101 $this->posts = apply_filters('the_posts', $this->posts);
147102
147103 update_post_caches($this->posts);
147104
147105 $this->post_count = count($this->posts);
147106 if ($this->post_count > 0) {
147107 $this->post = $this->posts[0];
147108 }
147109
147110 return $this->posts;
147111 }
147112
147113 /**
147114 * Setup the next post and iterate current post index.
147115 *
147116 * @since 1.5.0
147117 * @access public
147118 *
147119 * @return object Next post.
147120 */
147121 function next_post() {
147122
147123 $this->current_post++;
147124
147125 $this->post = $this->posts[$this->current_post];
147126 return $this->post;
147127 }
147128
147129 /**
147130 * Sets up the current post.
147131 *
147132 * Retrieves the next post, sets up the post, sets the 'in the loop'
147133 * property to true.
147134 *
147135 * @since 1.5.0
147136 * @access public
147137 * @uses $post
147138 * @uses do_action() Calls 'loop_start' if loop has just started
147139 */
147140 function the_post() {
147141 global $post;
147142 $this->in_the_loop = true;
147143 $post = $this->next_post();
147144 setup_postdata($post);
147145
147146 if ( $this->current_post == 0 ) // loop has just started
147147 do_action('loop_start');
147148 }
147149
147150 /**
147151 * Whether there are more posts available in the loop.
147152 *
147153 * Calls action 'loop_end', when the loop is complete.
147154 *
147155 * @since 1.5.0
147156 * @access public
147157 * @uses do_action() Calls 'loop_start' if loop has just started
147158 *
147159 * @return bool True if posts are available, false if end of loop.
147160 */
147161 function have_posts() {
147162 if ($this->current_post + 1 < $this->post_count) {
147163 return true;
147164 } elseif ($this->current_post + 1 == $this->post_count && $this->post_count > 0) {
147165 do_action('loop_end');
147166 // Do some cleaning up after the loop
147167 $this->rewind_posts();
147168 }
147169
147170 $this->in_the_loop = false;
147171 return false;
147172 }
147173
147174 /**
147175 * Rewind the posts and reset post index.
147176 *
147177 * @since 1.5.0
147178 * @access public
147179 */
147180 function rewind_posts() {
147181 $this->current_post = -1;
147182 if ($this->post_count > 0) {
147183 $this->post = $this->posts[0];
147184 }
147185 }
147186
147187 /**
147188 * Iterate current comment index and return comment object.
147189 *
147190 * @since 2.2.0
147191 * @access public
147192 *
147193 * @return object Comment object.
147194 */
147195 function next_comment() {
147196 $this->current_comment++;
147197
147198 $this->comment = $this->comments[$this->current_comment];
147199 return $this->comment;
147200 }
147201
147202 /**
147203 * Sets up the current comment.
147204 *
147205 * @since 2.2.0
147206 * @access public
147207 * @global object $comment Current comment.
147208 * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
147209 */
147210 function the_comment() {
147211 global $comment;
147212
147213 $comment = $this->next_comment();
147214
147215 if ($this->current_comment == 0) {
147216 do_action('comment_loop_start');
147217 }
147218 }
147219
147220 /**
147221 * Whether there are more comments available.
147222 *
147223 * Automatically rewinds comments when finished.
147224 *
147225 * @since 2.2.0
147226 * @access public
147227 *
147228 * @return bool True, if more comments. False, if no more posts.
147229 */
147230 function have_comments() {
147231 if ($this->current_comment + 1 < $this->comment_count) {
147232 return true;
147233 } elseif ($this->current_comment + 1 == $this->comment_count) {
147234 $this->rewind_comments();
147235 }
147236
147237 return false;
147238 }
147239
147240 /**
147241 * Rewind the comments, resets the comment index and comment to first.
147242 *
147243 * @since 2.2.0
147244 * @access public
147245 */
147246 function rewind_comments() {
147247 $this->current_comment = -1;
147248 if ($this->comment_count > 0) {
147249 $this->comment = $this->comments[0];
147250 }
147251 }
147252
147253 /**
147254 * Sets up the WordPress query by parsing query string.
147255 *
147256 * @since 1.5.0
147257 * @access public
147258 *
147259 * @param string $query URL query string.
147260 * @return array List of posts.
147261 */
147262 function &query($query) {
147263 $this->parse_query($query);
147264 return $this->get_posts();
147265 }
147266
147267 /**
147268 * Retrieve queried object.
147269 *
147270 * If queried object is not set, then the queried object will be set from
147271 * the category, tag, taxonomy, posts page, single post, page, or author
147272 * query variable. After it is set up, it will be returned.
147273 *
147274 * @since 1.5.0
147275 * @access public
147276 *
147277 * @return object
147278 */
147279 function get_queried_object() {
147280 if (isset($this->queried_object)) {
147281 return $this->queried_object;
147282 }
147283
147284 $this->queried_object = NULL;
147285 $this->queried_object_id = 0;
147286
147287 if ($this->is_category) {
147288 $cat = $this->get('cat');
147289 $category = &get_category($cat);
147290 if ( is_wp_error( $category ) )
147291 return NULL;
147292 $this->queried_object = &$category;
147293 $this->queried_object_id = (int) $cat;
147294 } else if ($this->is_tag) {
147295 $tag_id = $this->get('tag_id');
147296 $tag = &get_term($tag_id, 'post_tag');
147297 if ( is_wp_error( $tag ) )
147298 return NULL;
147299 $this->queried_object = &$tag;
147300 $this->queried_object_id = (int) $tag_id;
147301 } else if ($this->is_tax) {
147302 $tax = $this->get('taxonomy');
147303 $slug = $this->get('term');
147304 $term = &get_terms($tax, array('slug'=>$slug));
147305 if ( is_wp_error($term) || empty($term) )
147306 return NULL;
147307 $term = $term[0];
147308 $this->queried_object = $term;
147309 $this->queried_object_id = $term->term_id;
147310 } else if ($this->is_posts_page) {
147311 $this->queried_object = & get_page(get_option('page_for_posts'));
147312 $this->queried_object_id = (int) $this->queried_object->ID;
147313 } else if ($this->is_single) {
147314 $this->queried_object = $this->post;
147315 $this->queried_object_id = (int) $this->post->ID;
147316 } else if ($this->is_page) {
147317 $this->queried_object = $this->post;
147318 $this->queried_object_id = (int) $this->post->ID;
147319 } else if ($this->is_author) {
147320 $author_id = (int) $this->get('author');
147321 $author = get_userdata($author_id);
147322 $this->queried_object = $author;
147323 $this->queried_object_id = $author_id;
147324 }
147325
147326 return $this->queried_object;
147327 }
147328
147329 /**
147330 * Retrieve ID of the current queried object.
147331 *
147332 * @since 1.5.0
147333 * @access public
147334 *
147335 * @return int
147336 */
147337 function get_queried_object_id() {
147338 $this->get_queried_object();
147339
147340 if (isset($this->queried_object_id)) {
147341 return $this->queried_object_id;
147342 }
147343
147344 return 0;
147345 }
147346
147347 /**
147348 * PHP4 type constructor.
147349 *
147350 * Sets up the WordPress query, if parameter is not empty.
147351 *
147352 * @since 1.5.0
147353 * @access public
147354 *
147355 * @param string $query URL query string.
147356 * @return WP_Query
147357 */
147358 function WP_Query ($query = '') {
147359 if (! empty($query)) {
147360 $this->query($query);
147361 }
147362 }
147363 }
147364
147365 /**
147366 * Redirect old slugs to the correct permalink.
147367 *
147368 * Attempts to find the current slug from the past slugs.
147369 *
147370 * @since 2.1.0
147371 * @uses $wp_query
147372 * @uses $wpdb
147373 *
147374 * @return null If no link is found, null is returned.
147375 */
147376 function wp_old_slug_redirect () {
147377 global $wp_query;
147378 if ( is_404() && '' != $wp_query->query_vars['name'] ) :
147379 global $wpdb;
147380
147381 $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'] . "'";
147382
147383 // if year, monthnum, or day have been specified, make our query more precise
147384 // just in case there are multiple identical _wp_old_slug values
147385 if ( '' != $wp_query->query_vars['year'] )
147386 $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
147387 if ( '' != $wp_query->query_vars['monthnum'] )
147388 $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
147389 if ( '' != $wp_query->query_vars['day'] )
147390 $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
147391
147392 $id = (int) $wpdb->get_var($query);
147393
147394 if ( !$id )
147395 return;
147396
147397 $link = get_permalink($id);
147398
147399 if ( !$link )
147400 return;
147401
147402 wp_redirect($link, '301'); // Permanent redirect
147403 exit;
147404 endif;
147405 }
147406
147407 /**
147408 * Setup global post data.
147409 *
147410 * @since 1.5.0
147411 *
147412 * @param object $post Post data.
147413 * @return bool True when finished.
147414 */
147415 function setup_postdata($post) {
147416 global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
147417
147418 $id = (int) $post->ID;
147419
147420 $authordata = get_userdata($post->post_author);
147421
147422 $day = mysql2date('d.m.y', $post->post_date);
147423 $currentmonth = mysql2date('m', $post->post_date);
147424 $numpages = 1;
147425 $page = get_query_var('page');
147426 if ( !$page )
147427 $page = 1;
147428 if ( is_single() || is_page() || is_feed() )
147429 $more = 1;
147430 $content = $post->post_content;
147431 if ( strpos( $content, '<!--nextpage-->' ) ) {
147432 if ( $page > 1 )
147433 $more = 1;
147434 $multipage = 1;
147435 $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
147436 $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
147437 $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
147438 $pages = explode('<!--nextpage-->', $content);
147439 $numpages = count($pages);
147440 } else {
147441 $pages[0] = $post->post_content;
147442 $multipage = 0;
147443 }
147444 return true;
147445 }
147446
147447 ?>