web/wp-includes/query.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    26 
    26 
    27 	return $wp_query->get($var);
    27 	return $wp_query->get($var);
    28 }
    28 }
    29 
    29 
    30 /**
    30 /**
       
    31  * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
       
    32  *
       
    33  * @uses WP_Query::get_queried_object
       
    34  *
       
    35  * @since 3.1.0
       
    36  * @access public
       
    37  *
       
    38  * @return object
       
    39  */
       
    40 function get_queried_object() {
       
    41 	global $wp_query;
       
    42 	return $wp_query->get_queried_object();
       
    43 }
       
    44 
       
    45 /**
       
    46  * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
       
    47  *
       
    48  * @uses WP_Query::get_queried_object_id()
       
    49  *
       
    50  * @since 3.1.0
       
    51  * @access public
       
    52  *
       
    53  * @return int
       
    54  */
       
    55 function get_queried_object_id() {
       
    56 	global $wp_query;
       
    57 	return $wp_query->get_queried_object_id();
       
    58 }
       
    59 
       
    60 /**
    31  * Set query variable.
    61  * Set query variable.
    32  *
    62  *
    33  * @see WP_Query::set()
    63  * @see WP_Query::set()
    34  * @since 2.2.0
    64  * @since 2.2.0
    35  * @uses $wp_query
    65  * @uses $wp_query
    43 
    73 
    44 	return $wp_query->set($var, $value);
    74 	return $wp_query->set($var, $value);
    45 }
    75 }
    46 
    76 
    47 /**
    77 /**
    48  * Setup The Loop with query parameters.
    78  * Set up The Loop with query parameters.
    49  *
    79  *
    50  * This will override the current WordPress Loop and shouldn't be used more than
    80  * This will override the current WordPress Loop and shouldn't be used more than
    51  * once. This must not be used within the WordPress Loop.
    81  * once. This must not be used within the WordPress Loop.
    52  *
    82  *
    53  * @since 1.5.0
    83  * @since 1.5.0
    56  * @param string $query
    86  * @param string $query
    57  * @return array List of posts
    87  * @return array List of posts
    58  */
    88  */
    59 function &query_posts($query) {
    89 function &query_posts($query) {
    60 	unset($GLOBALS['wp_query']);
    90 	unset($GLOBALS['wp_query']);
    61 	$GLOBALS['wp_query'] =& new WP_Query();
    91 	$GLOBALS['wp_query'] = new WP_Query();
    62 	return $GLOBALS['wp_query']->query($query);
    92 	return $GLOBALS['wp_query']->query($query);
    63 }
    93 }
    64 
    94 
    65 /**
    95 /**
    66  * Destroy the previous query and setup a new query.
    96  * Destroy the previous query and set up a new query.
    67  *
    97  *
    68  * This should be used after {@link query_posts()} and before another {@link
    98  * This should be used after {@link query_posts()} and before another {@link
    69  * query_posts()}. This will remove obscure bugs that occur when the previous
    99  * query_posts()}. This will remove obscure bugs that occur when the previous
    70  * wp_query object is not destroyed properly before another is setup.
   100  * wp_query object is not destroyed properly before another is set up.
    71  *
   101  *
    72  * @since 2.3.0
   102  * @since 2.3.0
    73  * @uses $wp_query
   103  * @uses $wp_query
    74  */
   104  */
    75 function wp_reset_query() {
   105 function wp_reset_query() {
    76 	unset($GLOBALS['wp_query']);
   106 	unset($GLOBALS['wp_query']);
    77 	$GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
   107 	$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
       
   108 	wp_reset_postdata();
       
   109 }
       
   110 
       
   111 /**
       
   112  * After looping through a separate query, this function restores
       
   113  * the $post global to the current post in the main query
       
   114  *
       
   115  * @since 3.0.0
       
   116  * @uses $wp_query
       
   117  */
       
   118 function wp_reset_postdata() {
    78 	global $wp_query;
   119 	global $wp_query;
    79 	if ( !empty($wp_query->post) ) {
   120 	if ( !empty($wp_query->post) ) {
    80 		$GLOBALS['post'] = $wp_query->post;
   121 		$GLOBALS['post'] = $wp_query->post;
    81 		setup_postdata($wp_query->post);
   122 		setup_postdata($wp_query->post);
    82 	}
   123 	}
    85 /*
   126 /*
    86  * Query type checks.
   127  * Query type checks.
    87  */
   128  */
    88 
   129 
    89 /**
   130 /**
    90  * Is query requesting an archive page.
   131  * Is the query for an archive page?
    91  *
   132  *
       
   133  * Month, Year, Category, Author, Post Type archive...
       
   134  *
       
   135  * @see WP_Query::is_archive()
    92  * @since 1.5.0
   136  * @since 1.5.0
    93  * @uses $wp_query
   137  * @uses $wp_query
    94  *
   138  *
    95  * @return bool True if page is archive.
   139  * @return bool
    96  */
   140  */
    97 function is_archive () {
   141 function is_archive() {
    98 	global $wp_query;
   142 	global $wp_query;
    99 
   143 
   100 	return $wp_query->is_archive;
   144 	if ( ! isset( $wp_query ) ) {
   101 }
   145 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   102 
   146 		return false;
   103 /**
   147 	}
   104  * Is query requesting an attachment page.
   148 
   105  *
   149 	return $wp_query->is_archive();
       
   150 }
       
   151 
       
   152 /**
       
   153  * Is the query for a post type archive page?
       
   154  *
       
   155  * @see WP_Query::is_post_type_archive()
       
   156  * @since 3.1.0
       
   157  * @uses $wp_query
       
   158  *
       
   159  * @param mixed $post_types Optional. Post type or array of posts types to check against.
       
   160  * @return bool
       
   161  */
       
   162 function is_post_type_archive( $post_types = '' ) {
       
   163 	global $wp_query;
       
   164 
       
   165 	if ( ! isset( $wp_query ) ) {
       
   166 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
       
   167 		return false;
       
   168 	}
       
   169 
       
   170 	return $wp_query->is_post_type_archive( $post_types );
       
   171 }
       
   172 
       
   173 /**
       
   174  * Is the query for an attachment page?
       
   175  *
       
   176  * @see WP_Query::is_attachment()
   106  * @since 2.0.0
   177  * @since 2.0.0
   107  * @uses $wp_query
   178  * @uses $wp_query
   108  *
   179  *
   109  * @return bool True if page is attachment.
   180  * @return bool
   110  */
   181  */
   111 function is_attachment () {
   182 function is_attachment() {
   112 	global $wp_query;
   183 	global $wp_query;
   113 
   184 
   114 	return $wp_query->is_attachment;
   185 	if ( ! isset( $wp_query ) ) {
   115 }
   186 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   116 
   187 		return false;
   117 /**
   188 	}
   118  * Is query requesting an author page.
   189 
   119  *
   190 	return $wp_query->is_attachment();
   120  * If the $author parameter is specified then the check will be expanded to
   191 }
   121  * include whether the queried author matches the one given in the parameter.
   192 
   122  * You can match against integers and against strings.
   193 /**
   123  *
   194  * Is the query for an author archive page?
   124  * If matching against an integer, the ID should be used of the author for the
   195  *
   125  * test. If the $author is an ID and matches the author page user ID, then
   196  * If the $author parameter is specified, this function will additionally
   126  * 'true' will be returned.
   197  * check if the query is for one of the authors specified.
   127  *
   198  *
   128  * If matching against strings, then the test will be matched against both the
   199  * @see WP_Query::is_author()
   129  * nickname and user nicename and will return true on success.
       
   130  *
       
   131  * @since 1.5.0
   200  * @since 1.5.0
   132  * @uses $wp_query
   201  * @uses $wp_query
   133  *
   202  *
   134  * @param string|int $author Optional. Is current page this author.
   203  * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
   135  * @return bool True if page is author or $author (if set).
   204  * @return bool
   136  */
   205  */
   137 function is_author ($author = '') {
   206 function is_author( $author = '' ) {
   138 	global $wp_query;
   207 	global $wp_query;
   139 
   208 
   140 	if ( !$wp_query->is_author )
   209 	if ( ! isset( $wp_query ) ) {
       
   210 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   141 		return false;
   211 		return false;
   142 
   212 	}
   143 	if ( empty($author) )
   213 
   144 		return true;
   214 	return $wp_query->is_author( $author );
   145 
   215 }
   146 	$author_obj = $wp_query->get_queried_object();
   216 
   147 
   217 /**
   148 	$author = (array) $author;
   218  * Is the query for a category archive page?
   149 
   219  *
   150 	if ( in_array( $author_obj->ID, $author ) )
   220  * If the $category parameter is specified, this function will additionally
   151 		return true;
   221  * check if the query is for one of the categories specified.
   152 	elseif ( in_array( $author_obj->nickname, $author ) )
   222  *
   153 		return true;
   223  * @see WP_Query::is_category()
   154 	elseif ( in_array( $author_obj->user_nicename, $author ) )
       
   155 		return true;
       
   156 
       
   157 	return false;
       
   158 }
       
   159 
       
   160 /**
       
   161  * Whether current page query contains a category name or given category name.
       
   162  *
       
   163  * The category list can contain category IDs, names, or category slugs. If any
       
   164  * of them are part of the query, then it will return true.
       
   165  *
       
   166  * @since 1.5.0
   224  * @since 1.5.0
   167  * @uses $wp_query
   225  * @uses $wp_query
   168  *
   226  *
   169  * @param string|array $category Optional.
   227  * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
   170  * @return bool
   228  * @return bool
   171  */
   229  */
   172 function is_category ($category = '') {
   230 function is_category( $category = '' ) {
   173 	global $wp_query;
   231 	global $wp_query;
   174 
   232 
   175 	if ( !$wp_query->is_category )
   233 	if ( ! isset( $wp_query ) ) {
       
   234 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   176 		return false;
   235 		return false;
   177 
   236 	}
   178 	if ( empty($category) )
   237 
   179 		return true;
   238 	return $wp_query->is_category( $category );
   180 
   239 }
   181 	$cat_obj = $wp_query->get_queried_object();
   240 
   182 
   241 /**
   183 	$category = (array) $category;
   242  * Is the query for a tag archive page?
   184 
   243  *
   185 	if ( in_array( $cat_obj->term_id, $category ) )
   244  * If the $tag parameter is specified, this function will additionally
   186 		return true;
   245  * check if the query is for one of the tags specified.
   187 	elseif ( in_array( $cat_obj->name, $category ) )
   246  *
   188 		return true;
   247  * @see WP_Query::is_tag()
   189 	elseif ( in_array( $cat_obj->slug, $category ) )
       
   190 		return true;
       
   191 
       
   192 	return false;
       
   193 }
       
   194 
       
   195 /**
       
   196  * Whether the current page query has the given tag slug or contains tag.
       
   197  *
       
   198  * @since 2.3.0
   248  * @since 2.3.0
   199  * @uses $wp_query
   249  * @uses $wp_query
   200  *
   250  *
   201  * @param string|array $slug Optional. Single tag or list of tags to check for.
   251  * @param mixed $slug Optional. Tag slug or array of slugs.
   202  * @return bool
   252  * @return bool
   203  */
   253  */
   204 function is_tag( $slug = '' ) {
   254 function is_tag( $slug = '' ) {
   205 	global $wp_query;
   255 	global $wp_query;
   206 
   256 
   207 	if ( !$wp_query->is_tag )
   257 	if ( ! isset( $wp_query ) ) {
       
   258 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   208 		return false;
   259 		return false;
   209 
   260 	}
   210 	if ( empty( $slug ) )
   261 
   211 		return true;
   262 	return $wp_query->is_tag( $slug );
   212 
   263 }
   213 	$tag_obj = $wp_query->get_queried_object();
   264 
   214 
   265 /**
   215 	$slug = (array) $slug;
   266  * Is the query for a taxonomy archive page?
   216 
   267  *
   217 	if ( in_array( $tag_obj->slug, $slug ) )
   268  * If the $taxonomy parameter is specified, this function will additionally
   218 		return true;
   269  * check if the query is for that specific $taxonomy.
   219 
   270  *
   220 	return false;
   271  * If the $term parameter is specified in addition to the $taxonomy parameter,
   221 }
   272  * this function will additionally check if the query is for one of the terms
   222 
   273  * specified.
   223 /**
   274  *
   224  * Whether the current page query has the given taxonomy slug or contains taxonomy.
   275  * @see WP_Query::is_tax()
   225  *
       
   226  * @since 2.5.0
   276  * @since 2.5.0
   227  * @uses $wp_query
   277  * @uses $wp_query
   228  *
   278  *
   229  * @param string|array $slug Optional. Slug or slugs to check in current query.
   279  * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
       
   280  * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
   230  * @return bool
   281  * @return bool
   231  */
   282  */
   232 function is_tax( $slug = '' ) {
   283 function is_tax( $taxonomy = '', $term = '' ) {
   233 	global $wp_query;
   284 	global $wp_query;
   234 
   285 
   235 	if ( !$wp_query->is_tax )
   286 	if ( ! isset( $wp_query ) ) {
       
   287 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   236 		return false;
   288 		return false;
   237 
   289 	}
   238 	if ( empty($slug) )
   290 
   239 		return true;
   291 	return $wp_query->is_tax( $taxonomy, $term );
   240 
       
   241 	return in_array( get_query_var('taxonomy'), (array) $slug );
       
   242 }
   292 }
   243 
   293 
   244 /**
   294 /**
   245  * Whether the current URL is within the comments popup window.
   295  * Whether the current URL is within the comments popup window.
   246  *
   296  *
       
   297  * @see WP_Query::is_comments_popup()
   247  * @since 1.5.0
   298  * @since 1.5.0
   248  * @uses $wp_query
   299  * @uses $wp_query
   249  *
   300  *
   250  * @return bool
   301  * @return bool
   251  */
   302  */
   252 function is_comments_popup () {
   303 function is_comments_popup() {
   253 	global $wp_query;
   304 	global $wp_query;
   254 
   305 
   255 	return $wp_query->is_comments_popup;
   306 	if ( ! isset( $wp_query ) ) {
   256 }
   307 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   257 
   308 		return false;
   258 /**
   309 	}
   259  * Whether current URL is based on a date.
   310 
   260  *
   311 	return $wp_query->is_comments_popup();
       
   312 }
       
   313 
       
   314 /**
       
   315  * Is the query for a date archive?
       
   316  *
       
   317  * @see WP_Query::is_date()
   261  * @since 1.5.0
   318  * @since 1.5.0
   262  * @uses $wp_query
   319  * @uses $wp_query
   263  *
   320  *
   264  * @return bool
   321  * @return bool
   265  */
   322  */
   266 function is_date () {
   323 function is_date() {
   267 	global $wp_query;
   324 	global $wp_query;
   268 
   325 
   269 	return $wp_query->is_date;
   326 	if ( ! isset( $wp_query ) ) {
   270 }
   327 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   271 
   328 		return false;
   272 /**
   329 	}
   273  * Whether current blog URL contains a day.
   330 
   274  *
   331 	return $wp_query->is_date();
       
   332 }
       
   333 
       
   334 /**
       
   335  * Is the query for a day archive?
       
   336  *
       
   337  * @see WP_Query::is_day()
   275  * @since 1.5.0
   338  * @since 1.5.0
   276  * @uses $wp_query
   339  * @uses $wp_query
   277  *
   340  *
   278  * @return bool
   341  * @return bool
   279  */
   342  */
   280 function is_day () {
   343 function is_day() {
   281 	global $wp_query;
   344 	global $wp_query;
   282 
   345 
   283 	return $wp_query->is_day;
   346 	if ( ! isset( $wp_query ) ) {
   284 }
   347 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   285 
   348 		return false;
   286 /**
   349 	}
   287  * Whether current page query is feed URL.
   350 
   288  *
   351 	return $wp_query->is_day();
       
   352 }
       
   353 
       
   354 /**
       
   355  * Is the query for a feed?
       
   356  *
       
   357  * @see WP_Query::is_feed()
   289  * @since 1.5.0
   358  * @since 1.5.0
   290  * @uses $wp_query
   359  * @uses $wp_query
   291  *
   360  *
       
   361  * @param string|array $feeds Optional feed types to check.
   292  * @return bool
   362  * @return bool
   293  */
   363  */
   294 function is_feed () {
   364 function is_feed( $feeds = '' ) {
   295 	global $wp_query;
   365 	global $wp_query;
   296 
   366 
   297 	return $wp_query->is_feed;
   367 	if ( ! isset( $wp_query ) ) {
   298 }
   368 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   299 
   369 		return false;
   300 /**
   370 	}
   301  * Whether current page query is the front of the site.
   371 
   302  *
   372 	return $wp_query->is_feed( $feeds );
       
   373 }
       
   374 
       
   375 /**
       
   376  * Is the query for a comments feed?
       
   377  *
       
   378  * @see WP_Query::is_comments_feed()
       
   379  * @since 3.0.0
       
   380  * @uses $wp_query
       
   381  *
       
   382  * @return bool
       
   383  */
       
   384 function is_comment_feed() {
       
   385 	global $wp_query;
       
   386 
       
   387 	if ( ! isset( $wp_query ) ) {
       
   388 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
       
   389 		return false;
       
   390 	}
       
   391 
       
   392 	return $wp_query->is_comment_feed();
       
   393 }
       
   394 
       
   395 /**
       
   396  * Is the query for the front page of the site?
       
   397  *
       
   398  * This is for what is displayed at your site's main URL.
       
   399  *
       
   400  * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
       
   401  *
       
   402  * If you set a static page for the front page of your site, this function will return
       
   403  * true when viewing that page.
       
   404  *
       
   405  * Otherwise the same as @see is_home()
       
   406  *
       
   407  * @see WP_Query::is_front_page()
   303  * @since 2.5.0
   408  * @since 2.5.0
   304  * @uses is_home()
   409  * @uses is_home()
   305  * @uses get_option()
   410  * @uses get_option()
   306  *
   411  *
   307  * @return bool True, if front of site.
   412  * @return bool True, if front of site.
   308  */
   413  */
   309 function is_front_page () {
   414 function is_front_page() {
   310 	// most likely case
   415 	global $wp_query;
   311 	if ( 'posts' == get_option('show_on_front') && is_home() )
   416 
   312 		return true;
   417 	if ( ! isset( $wp_query ) ) {
   313 	elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
   418 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   314 		return true;
       
   315 	else
       
   316 		return false;
   419 		return false;
   317 }
   420 	}
   318 
   421 
   319 /**
   422 	return $wp_query->is_front_page();
   320  * Whether current page view is the blog homepage.
   423 }
   321  *
   424 
       
   425 /**
       
   426  * Is the query for the blog homepage?
       
   427  *
       
   428  * This is the page which shows the time based blog content of your site.
       
   429  *
       
   430  * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
       
   431  *
       
   432  * If you set a static page for the front page of your site, this function will return
       
   433  * true only on the page you set as the "Posts page".
       
   434  *
       
   435  * @see is_front_page()
       
   436  *
       
   437  * @see WP_Query::is_home()
   322  * @since 1.5.0
   438  * @since 1.5.0
   323  * @uses $wp_query
   439  * @uses $wp_query
   324  *
   440  *
   325  * @return bool True if blog view homepage.
   441  * @return bool True if blog view homepage.
   326  */
   442  */
   327 function is_home () {
   443 function is_home() {
   328 	global $wp_query;
   444 	global $wp_query;
   329 
   445 
   330 	return $wp_query->is_home;
   446 	if ( ! isset( $wp_query ) ) {
   331 }
   447 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   332 
   448 		return false;
   333 /**
   449 	}
   334  * Whether current page query contains a month.
   450 
   335  *
   451 	return $wp_query->is_home();
       
   452 }
       
   453 
       
   454 /**
       
   455  * Is the query for a month archive?
       
   456  *
       
   457  * @see WP_Query::is_month()
   336  * @since 1.5.0
   458  * @since 1.5.0
   337  * @uses $wp_query
   459  * @uses $wp_query
   338  *
   460  *
   339  * @return bool
   461  * @return bool
   340  */
   462  */
   341 function is_month () {
   463 function is_month() {
   342 	global $wp_query;
   464 	global $wp_query;
   343 
   465 
   344 	return $wp_query->is_month;
   466 	if ( ! isset( $wp_query ) ) {
   345 }
   467 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   346 
   468 		return false;
   347 /**
   469 	}
   348  * Whether query is page or contains given page(s).
   470 
   349  *
   471 	return $wp_query->is_month();
   350  * Calls the function without any parameters will only test whether the current
   472 }
   351  * query is of the page type. Either a list or a single item can be tested
   473 
   352  * against for whether the query is a page and also is the value or one of the
   474 /**
   353  * values in the page parameter.
   475  * Is the query for a single page?
   354  *
   476  *
   355  * The parameter can contain the page ID, page title, or page name. The
   477  * If the $page parameter is specified, this function will additionally
   356  * parameter can also be an array of those three values.
   478  * check if the query is for one of the pages specified.
   357  *
   479  *
       
   480  * @see is_single()
       
   481  * @see is_singular()
       
   482  *
       
   483  * @see WP_Query::is_page()
   358  * @since 1.5.0
   484  * @since 1.5.0
   359  * @uses $wp_query
   485  * @uses $wp_query
   360  *
   486  *
   361  * @param mixed $page Either page or list of pages to test against.
   487  * @param mixed $page Page ID, title, slug, or array of such.
   362  * @return bool
   488  * @return bool
   363  */
   489  */
   364 function is_page ($page = '') {
   490 function is_page( $page = '' ) {
   365 	global $wp_query;
   491 	global $wp_query;
   366 
   492 
   367 	if ( !$wp_query->is_page )
   493 	if ( ! isset( $wp_query ) ) {
       
   494 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   368 		return false;
   495 		return false;
   369 
   496 	}
   370 	if ( empty($page) )
   497 
   371 		return true;
   498 	return $wp_query->is_page( $page );
   372 
   499 }
   373 	$page_obj = $wp_query->get_queried_object();
   500 
   374 
   501 /**
   375 	$page = (array) $page;
   502  * Is the query for paged result and not for the first page?
   376 
   503  *
   377 	if ( in_array( $page_obj->ID, $page ) )
   504  * @see WP_Query::is_paged()
   378 		return true;
       
   379 	elseif ( in_array( $page_obj->post_title, $page ) )
       
   380 		return true;
       
   381 	else if ( in_array( $page_obj->post_name, $page ) )
       
   382 		return true;
       
   383 
       
   384 	return false;
       
   385 }
       
   386 
       
   387 /**
       
   388  * Whether query contains multiple pages for the results.
       
   389  *
       
   390  * @since 1.5.0
   505  * @since 1.5.0
   391  * @uses $wp_query
   506  * @uses $wp_query
   392  *
   507  *
   393  * @return bool
   508  * @return bool
   394  */
   509  */
   395 function is_paged () {
   510 function is_paged() {
   396 	global $wp_query;
   511 	global $wp_query;
   397 
   512 
   398 	return $wp_query->is_paged;
   513 	if ( ! isset( $wp_query ) ) {
   399 }
   514 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   400 
   515 		return false;
   401 /**
   516 	}
   402  * Whether the current page was created by a plugin.
   517 
   403  *
   518 	return $wp_query->is_paged();
   404  * The plugin can set this by using the global $plugin_page and setting it to
   519 }
   405  * true.
   520 
   406  *
   521 /**
   407  * @since 1.5.0
   522  * Is the query for a post or page preview?
   408  * @global bool $plugin_page Used by plugins to tell the query that current is a plugin page.
   523  *
   409  *
   524  * @see WP_Query::is_preview()
   410  * @return bool
       
   411  */
       
   412 function is_plugin_page() {
       
   413 	global $plugin_page;
       
   414 
       
   415 	if ( isset($plugin_page) )
       
   416 		return true;
       
   417 
       
   418 	return false;
       
   419 }
       
   420 
       
   421 /**
       
   422  * Whether the current query is preview of post or page.
       
   423  *
       
   424  * @since 2.0.0
   525  * @since 2.0.0
   425  * @uses $wp_query
   526  * @uses $wp_query
   426  *
   527  *
   427  * @return bool
   528  * @return bool
   428  */
   529  */
   429 function is_preview() {
   530 function is_preview() {
   430 	global $wp_query;
   531 	global $wp_query;
   431 
   532 
   432 	return $wp_query->is_preview;
   533 	if ( ! isset( $wp_query ) ) {
   433 }
   534 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   434 
   535 		return false;
   435 /**
   536 	}
   436  * Whether the current query post is robots.
   537 
   437  *
   538 	return $wp_query->is_preview();
       
   539 }
       
   540 
       
   541 /**
       
   542  * Is the query for the robots file?
       
   543  *
       
   544  * @see WP_Query::is_robots()
   438  * @since 2.1.0
   545  * @since 2.1.0
   439  * @uses $wp_query
   546  * @uses $wp_query
   440  *
   547  *
   441  * @return bool
   548  * @return bool
   442  */
   549  */
   443 function is_robots() {
   550 function is_robots() {
   444 	global $wp_query;
   551 	global $wp_query;
   445 
   552 
   446 	return $wp_query->is_robots;
   553 	if ( ! isset( $wp_query ) ) {
   447 }
   554 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   448 
   555 		return false;
   449 /**
   556 	}
   450  * Whether current query is the result of a user search.
   557 
   451  *
   558 	return $wp_query->is_robots();
       
   559 }
       
   560 
       
   561 /**
       
   562  * Is the query for a search?
       
   563  *
       
   564  * @see WP_Query::is_search()
   452  * @since 1.5.0
   565  * @since 1.5.0
   453  * @uses $wp_query
   566  * @uses $wp_query
   454  *
   567  *
   455  * @return bool
   568  * @return bool
   456  */
   569  */
   457 function is_search () {
   570 function is_search() {
   458 	global $wp_query;
   571 	global $wp_query;
   459 
   572 
   460 	return $wp_query->is_search;
   573 	if ( ! isset( $wp_query ) ) {
   461 }
   574 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   462 
   575 		return false;
   463 /**
   576 	}
   464  * Whether the current page query is single page.
   577 
   465  *
   578 	return $wp_query->is_search();
   466  * The parameter can contain the post ID, post title, or post name. The
   579 }
   467  * parameter can also be an array of those three values.
   580 
   468  *
   581 /**
   469  * This applies to other post types, attachments, pages, posts. Just means that
   582  * Is the query for a single post?
   470  * the current query has only a single object.
   583  *
   471  *
   584  * Works for any post type, except attachments and pages
       
   585  *
       
   586  * If the $post parameter is specified, this function will additionally
       
   587  * check if the query is for one of the Posts specified.
       
   588  *
       
   589  * @see is_page()
       
   590  * @see is_singular()
       
   591  *
       
   592  * @see WP_Query::is_single()
   472  * @since 1.5.0
   593  * @since 1.5.0
   473  * @uses $wp_query
   594  * @uses $wp_query
   474  *
   595  *
   475  * @param mixed $post Either post or list of posts to test against.
   596  * @param mixed $post Post ID, title, slug, or array of such.
   476  * @return bool
   597  * @return bool
   477  */
   598  */
   478 function is_single ($post = '') {
   599 function is_single( $post = '' ) {
   479 	global $wp_query;
   600 	global $wp_query;
   480 
   601 
   481 	if ( !$wp_query->is_single )
   602 	if ( ! isset( $wp_query ) ) {
       
   603 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   482 		return false;
   604 		return false;
   483 
   605 	}
   484 	if ( empty( $post) )
   606 
   485 		return true;
   607 	return $wp_query->is_single( $post );
   486 
   608 }
   487 	$post_obj = $wp_query->get_queried_object();
   609 
   488 
   610 /**
   489 	$post = (array) $post;
   611  * Is the query for a single post of any post type (post, attachment, page, ... )?
   490 
   612  *
   491 	if ( in_array( $post_obj->ID, $post ) )
   613  * If the $post_types parameter is specified, this function will additionally
   492 		return true;
   614  * check if the query is for one of the Posts Types specified.
   493 	elseif ( in_array( $post_obj->post_title, $post ) )
   615  *
   494 		return true;
   616  * @see is_page()
   495 	elseif ( in_array( $post_obj->post_name, $post ) )
   617  * @see is_single()
   496 		return true;
   618  *
   497 
   619  * @see WP_Query::is_singular()
   498 	return false;
       
   499 }
       
   500 
       
   501 /**
       
   502  * Whether is single post, is a page, or is an attachment.
       
   503  *
       
   504  * @since 1.5.0
   620  * @since 1.5.0
   505  * @uses $wp_query
   621  * @uses $wp_query
   506  *
   622  *
       
   623  * @param mixed $post_types Optional. Post Type or array of Post Types
   507  * @return bool
   624  * @return bool
   508  */
   625  */
   509 function is_singular() {
   626 function is_singular( $post_types = '' ) {
   510 	global $wp_query;
   627 	global $wp_query;
   511 
   628 
   512 	return $wp_query->is_singular;
   629 	if ( ! isset( $wp_query ) ) {
   513 }
   630 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   514 
   631 		return false;
   515 /**
   632 	}
   516  * Whether the query contains a time.
   633 
   517  *
   634 	return $wp_query->is_singular( $post_types );
       
   635 }
       
   636 
       
   637 /**
       
   638  * Is the query for a specific time?
       
   639  *
       
   640  * @see WP_Query::is_time()
   518  * @since 1.5.0
   641  * @since 1.5.0
   519  * @uses $wp_query
   642  * @uses $wp_query
   520  *
   643  *
   521  * @return bool
   644  * @return bool
   522  */
   645  */
   523 function is_time () {
   646 function is_time() {
   524 	global $wp_query;
   647 	global $wp_query;
   525 
   648 
   526 	return $wp_query->is_time;
   649 	if ( ! isset( $wp_query ) ) {
   527 }
   650 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   528 
   651 		return false;
   529 /**
   652 	}
   530  * Whether the query is a trackback.
   653 
   531  *
   654 	return $wp_query->is_time();
       
   655 }
       
   656 
       
   657 /**
       
   658  * Is the query for a trackback endpoint call?
       
   659  *
       
   660  * @see WP_Query::is_trackback()
   532  * @since 1.5.0
   661  * @since 1.5.0
   533  * @uses $wp_query
   662  * @uses $wp_query
   534  *
   663  *
   535  * @return bool
   664  * @return bool
   536  */
   665  */
   537 function is_trackback () {
   666 function is_trackback() {
   538 	global $wp_query;
   667 	global $wp_query;
   539 
   668 
   540 	return $wp_query->is_trackback;
   669 	if ( ! isset( $wp_query ) ) {
   541 }
   670 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   542 
   671 		return false;
   543 /**
   672 	}
   544  * Whether the query contains a year.
   673 
   545  *
   674 	return $wp_query->is_trackback();
       
   675 }
       
   676 
       
   677 /**
       
   678  * Is the query for a specific year?
       
   679  *
       
   680  * @see WP_Query::is_year()
   546  * @since 1.5.0
   681  * @since 1.5.0
   547  * @uses $wp_query
   682  * @uses $wp_query
   548  *
   683  *
   549  * @return bool
   684  * @return bool
   550  */
   685  */
   551 function is_year () {
   686 function is_year() {
   552 	global $wp_query;
   687 	global $wp_query;
   553 
   688 
   554 	return $wp_query->is_year;
   689 	if ( ! isset( $wp_query ) ) {
   555 }
   690 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
   556 
   691 		return false;
   557 /**
   692 	}
   558  * Whether current page query is a 404 and no results for WordPress query.
   693 
   559  *
   694 	return $wp_query->is_year();
       
   695 }
       
   696 
       
   697 /**
       
   698  * Is the query a 404 (returns no results)?
       
   699  *
       
   700  * @see WP_Query::is_404()
   560  * @since 1.5.0
   701  * @since 1.5.0
   561  * @uses $wp_query
   702  * @uses $wp_query
   562  *
   703  *
   563  * @return bool True, if nothing is found matching WordPress Query.
   704  * @return bool
   564  */
   705  */
   565 function is_404 () {
   706 function is_404() {
   566 	global $wp_query;
   707 	global $wp_query;
   567 
   708 
   568 	return $wp_query->is_404;
   709 	if ( ! isset( $wp_query ) ) {
       
   710 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
       
   711 		return false;
       
   712 	}
       
   713 
       
   714 	return $wp_query->is_404();
       
   715 }
       
   716 
       
   717 /**
       
   718  * Is the query the main query?
       
   719  *
       
   720  * @since 3.3.0
       
   721  *
       
   722  * @return bool
       
   723  */
       
   724 function is_main_query() {
       
   725 	global $wp_query;
       
   726 	return $wp_query->is_main_query();
   569 }
   727 }
   570 
   728 
   571 /*
   729 /*
   572  * The Loop.  Post loop control.
   730  * The Loop. Post loop control.
   573  */
   731  */
   574 
   732 
   575 /**
   733 /**
   576  * Whether current WordPress query has results to loop over.
   734  * Whether current WordPress query has results to loop over.
   577  *
   735  *
   673  * @since 1.5.0
   831  * @since 1.5.0
   674  */
   832  */
   675 class WP_Query {
   833 class WP_Query {
   676 
   834 
   677 	/**
   835 	/**
   678 	 * Query string
   836 	 * Query vars set by the user
   679 	 *
   837 	 *
   680 	 * @since 1.5.0
   838 	 * @since 1.5.0
       
   839 	 * @access public
       
   840 	 * @var array
       
   841 	 */
       
   842 	var $query;
       
   843 
       
   844 	/**
       
   845 	 * Query vars, after parsing
       
   846 	 *
       
   847 	 * @since 1.5.0
       
   848 	 * @access public
       
   849 	 * @var array
       
   850 	 */
       
   851 	var $query_vars = array();
       
   852 
       
   853 	/**
       
   854 	 * Taxonomy query, as passed to get_tax_sql()
       
   855 	 *
       
   856 	 * @since 3.1.0
       
   857 	 * @access public
       
   858 	 * @var object WP_Tax_Query
       
   859 	 */
       
   860 	var $tax_query;
       
   861 
       
   862 	/**
       
   863 	 * Metadata query container
       
   864 	 *
       
   865 	 * @since 3.2.0
       
   866 	 * @access public
       
   867 	 * @var object WP_Meta_Query
       
   868 	 */
       
   869 	var $meta_query = false;
       
   870 
       
   871 	/**
       
   872 	 * Holds the data for a single object that is queried.
       
   873 	 *
       
   874 	 * Holds the contents of a post, page, category, attachment.
       
   875 	 *
       
   876 	 * @since 1.5.0
       
   877 	 * @access public
       
   878 	 * @var object|array
       
   879 	 */
       
   880 	var $queried_object;
       
   881 
       
   882 	/**
       
   883 	 * The ID of the queried object.
       
   884 	 *
       
   885 	 * @since 1.5.0
       
   886 	 * @access public
       
   887 	 * @var int
       
   888 	 */
       
   889 	var $queried_object_id;
       
   890 
       
   891 	/**
       
   892 	 * Get post database query.
       
   893 	 *
       
   894 	 * @since 2.0.1
   681 	 * @access public
   895 	 * @access public
   682 	 * @var string
   896 	 * @var string
   683 	 */
   897 	 */
   684 	var $query;
   898 	var $request;
   685 
   899 
   686 	/**
   900 	/**
   687 	 * Query search variables set by the user.
   901 	 * List of posts.
   688 	 *
   902 	 *
   689 	 * @since 1.5.0
   903 	 * @since 1.5.0
   690 	 * @access public
   904 	 * @access public
   691 	 * @var array
   905 	 * @var array
   692 	 */
   906 	 */
   693 	var $query_vars = array();
   907 	var $posts;
   694 
   908 
   695 	/**
   909 	/**
   696 	 * Holds the data for a single object that is queried.
   910 	 * The amount of posts for the current query.
   697 	 *
       
   698 	 * Holds the contents of a post, page, category, attachment.
       
   699 	 *
       
   700 	 * @since 1.5.0
       
   701 	 * @access public
       
   702 	 * @var object|array
       
   703 	 */
       
   704 	var $queried_object;
       
   705 
       
   706 	/**
       
   707 	 * The ID of the queried object.
       
   708 	 *
   911 	 *
   709 	 * @since 1.5.0
   912 	 * @since 1.5.0
   710 	 * @access public
   913 	 * @access public
   711 	 * @var int
   914 	 * @var int
   712 	 */
   915 	 */
   713 	var $queried_object_id;
   916 	var $post_count = 0;
   714 
   917 
   715 	/**
   918 	/**
   716 	 * Get post database query.
   919 	 * Index of the current item in the loop.
   717 	 *
   920 	 *
   718 	 * @since 2.0.1
   921 	 * @since 1.5.0
   719 	 * @access public
   922 	 * @access public
   720 	 * @var string
   923 	 * @var int
   721 	 */
   924 	 */
   722 	var $request;
   925 	var $current_post = -1;
   723 
   926 
   724 	/**
   927 	/**
   725 	 * List of posts.
   928 	 * Whether the loop has started and the caller is in the loop.
   726 	 *
   929 	 *
   727 	 * @since 1.5.0
   930 	 * @since 2.0.0
       
   931 	 * @access public
       
   932 	 * @var bool
       
   933 	 */
       
   934 	var $in_the_loop = false;
       
   935 
       
   936 	/**
       
   937 	 * The current post ID.
       
   938 	 *
       
   939 	 * @since 1.5.0
       
   940 	 * @access public
       
   941 	 * @var object
       
   942 	 */
       
   943 	var $post;
       
   944 
       
   945 	/**
       
   946 	 * The list of comments for current post.
       
   947 	 *
       
   948 	 * @since 2.2.0
   728 	 * @access public
   949 	 * @access public
   729 	 * @var array
   950 	 * @var array
   730 	 */
   951 	 */
   731 	var $posts;
   952 	var $comments;
   732 
   953 
   733 	/**
   954 	/**
   734 	 * The amount of posts for the current query.
   955 	 * The amount of comments for the posts.
   735 	 *
   956 	 *
   736 	 * @since 1.5.0
   957 	 * @since 2.2.0
   737 	 * @access public
   958 	 * @access public
   738 	 * @var int
   959 	 * @var int
   739 	 */
   960 	 */
   740 	var $post_count = 0;
   961 	var $comment_count = 0;
   741 
   962 
   742 	/**
   963 	/**
   743 	 * Index of the current item in the loop.
   964 	 * The index of the comment in the comment loop.
   744 	 *
   965 	 *
   745 	 * @since 1.5.0
   966 	 * @since 2.2.0
   746 	 * @access public
   967 	 * @access public
   747 	 * @var int
   968 	 * @var int
   748 	 */
   969 	 */
   749 	var $current_post = -1;
   970 	var $current_comment = -1;
   750 
   971 
   751 	/**
   972 	/**
   752 	 * Whether the loop has started and the caller is in the loop.
   973 	 * Current comment ID.
       
   974 	 *
       
   975 	 * @since 2.2.0
       
   976 	 * @access public
       
   977 	 * @var int
       
   978 	 */
       
   979 	var $comment;
       
   980 
       
   981 	/**
       
   982 	 * Amount of posts if limit clause was not used.
       
   983 	 *
       
   984 	 * @since 2.1.0
       
   985 	 * @access public
       
   986 	 * @var int
       
   987 	 */
       
   988 	var $found_posts = 0;
       
   989 
       
   990 	/**
       
   991 	 * The amount of pages.
       
   992 	 *
       
   993 	 * @since 2.1.0
       
   994 	 * @access public
       
   995 	 * @var int
       
   996 	 */
       
   997 	var $max_num_pages = 0;
       
   998 
       
   999 	/**
       
  1000 	 * The amount of comment pages.
       
  1001 	 *
       
  1002 	 * @since 2.7.0
       
  1003 	 * @access public
       
  1004 	 * @var int
       
  1005 	 */
       
  1006 	var $max_num_comment_pages = 0;
       
  1007 
       
  1008 	/**
       
  1009 	 * Set if query is single post.
       
  1010 	 *
       
  1011 	 * @since 1.5.0
       
  1012 	 * @access public
       
  1013 	 * @var bool
       
  1014 	 */
       
  1015 	var $is_single = false;
       
  1016 
       
  1017 	/**
       
  1018 	 * Set if query is preview of blog.
   753 	 *
  1019 	 *
   754 	 * @since 2.0.0
  1020 	 * @since 2.0.0
   755 	 * @access public
  1021 	 * @access public
   756 	 * @var bool
  1022 	 * @var bool
   757 	 */
  1023 	 */
   758 	var $in_the_loop = false;
  1024 	var $is_preview = false;
   759 
  1025 
   760 	/**
  1026 	/**
   761 	 * The current post ID.
  1027 	 * Set if query returns a page.
   762 	 *
  1028 	 *
   763 	 * @since 1.5.0
  1029 	 * @since 1.5.0
   764 	 * @access public
  1030 	 * @access public
   765 	 * @var int
  1031 	 * @var bool
   766 	 */
  1032 	 */
   767 	var $post;
  1033 	var $is_page = false;
   768 
  1034 
   769 	/**
  1035 	/**
   770 	 * The list of comments for current post.
  1036 	 * Set if query is an archive list.
       
  1037 	 *
       
  1038 	 * @since 1.5.0
       
  1039 	 * @access public
       
  1040 	 * @var bool
       
  1041 	 */
       
  1042 	var $is_archive = false;
       
  1043 
       
  1044 	/**
       
  1045 	 * Set if query is part of a date.
       
  1046 	 *
       
  1047 	 * @since 1.5.0
       
  1048 	 * @access public
       
  1049 	 * @var bool
       
  1050 	 */
       
  1051 	var $is_date = false;
       
  1052 
       
  1053 	/**
       
  1054 	 * Set if query contains a year.
       
  1055 	 *
       
  1056 	 * @since 1.5.0
       
  1057 	 * @access public
       
  1058 	 * @var bool
       
  1059 	 */
       
  1060 	var $is_year = false;
       
  1061 
       
  1062 	/**
       
  1063 	 * Set if query contains a month.
       
  1064 	 *
       
  1065 	 * @since 1.5.0
       
  1066 	 * @access public
       
  1067 	 * @var bool
       
  1068 	 */
       
  1069 	var $is_month = false;
       
  1070 
       
  1071 	/**
       
  1072 	 * Set if query contains a day.
       
  1073 	 *
       
  1074 	 * @since 1.5.0
       
  1075 	 * @access public
       
  1076 	 * @var bool
       
  1077 	 */
       
  1078 	var $is_day = false;
       
  1079 
       
  1080 	/**
       
  1081 	 * Set if query contains time.
       
  1082 	 *
       
  1083 	 * @since 1.5.0
       
  1084 	 * @access public
       
  1085 	 * @var bool
       
  1086 	 */
       
  1087 	var $is_time = false;
       
  1088 
       
  1089 	/**
       
  1090 	 * Set if query contains an author.
       
  1091 	 *
       
  1092 	 * @since 1.5.0
       
  1093 	 * @access public
       
  1094 	 * @var bool
       
  1095 	 */
       
  1096 	var $is_author = false;
       
  1097 
       
  1098 	/**
       
  1099 	 * Set if query contains category.
       
  1100 	 *
       
  1101 	 * @since 1.5.0
       
  1102 	 * @access public
       
  1103 	 * @var bool
       
  1104 	 */
       
  1105 	var $is_category = false;
       
  1106 
       
  1107 	/**
       
  1108 	 * Set if query contains tag.
       
  1109 	 *
       
  1110 	 * @since 2.3.0
       
  1111 	 * @access public
       
  1112 	 * @var bool
       
  1113 	 */
       
  1114 	var $is_tag = false;
       
  1115 
       
  1116 	/**
       
  1117 	 * Set if query contains taxonomy.
       
  1118 	 *
       
  1119 	 * @since 2.5.0
       
  1120 	 * @access public
       
  1121 	 * @var bool
       
  1122 	 */
       
  1123 	var $is_tax = false;
       
  1124 
       
  1125 	/**
       
  1126 	 * Set if query was part of a search result.
       
  1127 	 *
       
  1128 	 * @since 1.5.0
       
  1129 	 * @access public
       
  1130 	 * @var bool
       
  1131 	 */
       
  1132 	var $is_search = false;
       
  1133 
       
  1134 	/**
       
  1135 	 * Set if query is feed display.
       
  1136 	 *
       
  1137 	 * @since 1.5.0
       
  1138 	 * @access public
       
  1139 	 * @var bool
       
  1140 	 */
       
  1141 	var $is_feed = false;
       
  1142 
       
  1143 	/**
       
  1144 	 * Set if query is comment feed display.
   771 	 *
  1145 	 *
   772 	 * @since 2.2.0
  1146 	 * @since 2.2.0
   773 	 * @access public
  1147 	 * @access public
   774 	 * @var array
  1148 	 * @var bool
   775 	 */
  1149 	 */
   776 	var $comments;
  1150 	var $is_comment_feed = false;
   777 
  1151 
   778 	/**
  1152 	/**
   779 	 * The amount of comments for the posts.
  1153 	 * Set if query is trackback.
   780 	 *
  1154 	 *
   781 	 * @since 2.2.0
  1155 	 * @since 1.5.0
   782 	 * @access public
  1156 	 * @access public
   783 	 * @var int
  1157 	 * @var bool
   784 	 */
  1158 	 */
   785 	var $comment_count = 0;
  1159 	var $is_trackback = false;
   786 
  1160 
   787 	/**
  1161 	/**
   788 	 * The index of the comment in the comment loop.
  1162 	 * Set if query is blog homepage.
   789 	 *
  1163 	 *
   790 	 * @since 2.2.0
  1164 	 * @since 1.5.0
   791 	 * @access public
  1165 	 * @access public
   792 	 * @var int
  1166 	 * @var bool
   793 	 */
  1167 	 */
   794 	var $current_comment = -1;
  1168 	var $is_home = false;
   795 
  1169 
   796 	/**
  1170 	/**
   797 	 * Current comment ID.
  1171 	 * Set if query couldn't found anything.
   798 	 *
  1172 	 *
   799 	 * @since 2.2.0
  1173 	 * @since 1.5.0
   800 	 * @access public
  1174 	 * @access public
   801 	 * @var int
  1175 	 * @var bool
   802 	 */
  1176 	 */
   803 	var $comment;
  1177 	var $is_404 = false;
   804 
  1178 
   805 	/**
  1179 	/**
   806 	 * Amount of posts if limit clause was not used.
  1180 	 * Set if query is within comments popup window.
       
  1181 	 *
       
  1182 	 * @since 1.5.0
       
  1183 	 * @access public
       
  1184 	 * @var bool
       
  1185 	 */
       
  1186 	var $is_comments_popup = false;
       
  1187 
       
  1188 	/**
       
  1189 	 * Set if query is paged
       
  1190 	 *
       
  1191 	 * @since 1.5.0
       
  1192 	 * @access public
       
  1193 	 * @var bool
       
  1194 	 */
       
  1195 	var $is_paged = false;
       
  1196 
       
  1197 	/**
       
  1198 	 * Set if query is part of administration page.
       
  1199 	 *
       
  1200 	 * @since 1.5.0
       
  1201 	 * @access public
       
  1202 	 * @var bool
       
  1203 	 */
       
  1204 	var $is_admin = false;
       
  1205 
       
  1206 	/**
       
  1207 	 * Set if query is an attachment.
       
  1208 	 *
       
  1209 	 * @since 2.0.0
       
  1210 	 * @access public
       
  1211 	 * @var bool
       
  1212 	 */
       
  1213 	var $is_attachment = false;
       
  1214 
       
  1215 	/**
       
  1216 	 * Set if is single, is a page, or is an attachment.
   807 	 *
  1217 	 *
   808 	 * @since 2.1.0
  1218 	 * @since 2.1.0
   809 	 * @access public
  1219 	 * @access public
   810 	 * @var int
  1220 	 * @var bool
   811 	 */
  1221 	 */
   812 	var $found_posts = 0;
  1222 	var $is_singular = false;
   813 
  1223 
   814 	/**
  1224 	/**
   815 	 * The amount of pages.
  1225 	 * Set if query is for robots.
   816 	 *
  1226 	 *
   817 	 * @since 2.1.0
  1227 	 * @since 2.1.0
   818 	 * @access public
  1228 	 * @access public
   819 	 * @var int
       
   820 	 */
       
   821 	var $max_num_pages = 0;
       
   822 
       
   823 	/**
       
   824 	 * The amount of comment pages.
       
   825 	 *
       
   826 	 * @since 2.7.0
       
   827 	 * @access public
       
   828 	 * @var int
       
   829 	 */
       
   830 	var $max_num_comment_pages = 0;
       
   831 
       
   832 	/**
       
   833 	 * Set if query is single post.
       
   834 	 *
       
   835 	 * @since 1.5.0
       
   836 	 * @access public
       
   837 	 * @var bool
  1229 	 * @var bool
   838 	 */
  1230 	 */
   839 	var $is_single = false;
  1231 	var $is_robots = false;
   840 
  1232 
   841 	/**
  1233 	/**
   842 	 * Set if query is preview of blog.
  1234 	 * Set if query contains posts.
   843 	 *
  1235 	 *
   844 	 * @since 2.0.0
  1236 	 * Basically, the homepage if the option isn't set for the static homepage.
       
  1237 	 *
       
  1238 	 * @since 2.1.0
   845 	 * @access public
  1239 	 * @access public
   846 	 * @var bool
  1240 	 * @var bool
   847 	 */
  1241 	 */
   848 	var $is_preview = false;
  1242 	var $is_posts_page = false;
   849 
  1243 
   850 	/**
  1244 	/**
   851 	 * Set if query returns a page.
  1245 	 * Set if query is for a post type archive.
   852 	 *
  1246 	 *
   853 	 * @since 1.5.0
  1247 	 * @since 3.1.0
   854 	 * @access public
  1248 	 * @access public
   855 	 * @var bool
  1249 	 * @var bool
   856 	 */
  1250 	 */
   857 	var $is_page = false;
  1251 	var $is_post_type_archive = false;
   858 
  1252 
   859 	/**
  1253 	/**
   860 	 * Set if query is an archive list.
  1254 	 * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
   861 	 *
  1255 	 * whether we have to re-parse because something has changed
   862 	 * @since 1.5.0
  1256 	 *
       
  1257 	 * @since 3.1.0
       
  1258 	 * @access private
       
  1259 	 */
       
  1260 	var $query_vars_hash = false;
       
  1261 
       
  1262 	/**
       
  1263 	 * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
       
  1264 	 * via pre_get_posts hooks.
       
  1265 	 *
       
  1266 	 * @since 3.1.1
       
  1267 	 * @access private
       
  1268 	 */
       
  1269 	var $query_vars_changed = true;
       
  1270 
       
  1271 	/**
       
  1272 	 * Set if post thumbnails are cached
       
  1273 	 *
       
  1274 	 * @since 3.2.0
   863 	 * @access public
  1275 	 * @access public
   864 	 * @var bool
  1276 	 * @var bool
   865 	 */
  1277 	 */
   866 	var $is_archive = false;
  1278 	 var $thumbnails_cached = false;
   867 
       
   868 	/**
       
   869 	 * Set if query is part of a date.
       
   870 	 *
       
   871 	 * @since 1.5.0
       
   872 	 * @access public
       
   873 	 * @var bool
       
   874 	 */
       
   875 	var $is_date = false;
       
   876 
       
   877 	/**
       
   878 	 * Set if query contains a year.
       
   879 	 *
       
   880 	 * @since 1.5.0
       
   881 	 * @access public
       
   882 	 * @var bool
       
   883 	 */
       
   884 	var $is_year = false;
       
   885 
       
   886 	/**
       
   887 	 * Set if query contains a month.
       
   888 	 *
       
   889 	 * @since 1.5.0
       
   890 	 * @access public
       
   891 	 * @var bool
       
   892 	 */
       
   893 	var $is_month = false;
       
   894 
       
   895 	/**
       
   896 	 * Set if query contains a day.
       
   897 	 *
       
   898 	 * @since 1.5.0
       
   899 	 * @access public
       
   900 	 * @var bool
       
   901 	 */
       
   902 	var $is_day = false;
       
   903 
       
   904 	/**
       
   905 	 * Set if query contains time.
       
   906 	 *
       
   907 	 * @since 1.5.0
       
   908 	 * @access public
       
   909 	 * @var bool
       
   910 	 */
       
   911 	var $is_time = false;
       
   912 
       
   913 	/**
       
   914 	 * Set if query contains an author.
       
   915 	 *
       
   916 	 * @since 1.5.0
       
   917 	 * @access public
       
   918 	 * @var bool
       
   919 	 */
       
   920 	var $is_author = false;
       
   921 
       
   922 	/**
       
   923 	 * Set if query contains category.
       
   924 	 *
       
   925 	 * @since 1.5.0
       
   926 	 * @access public
       
   927 	 * @var bool
       
   928 	 */
       
   929 	var $is_category = false;
       
   930 
       
   931 	/**
       
   932 	 * Set if query contains tag.
       
   933 	 *
       
   934 	 * @since 2.3.0
       
   935 	 * @access public
       
   936 	 * @var bool
       
   937 	 */
       
   938 	var $is_tag = false;
       
   939 
       
   940 	/**
       
   941 	 * Set if query contains taxonomy.
       
   942 	 *
       
   943 	 * @since 2.5.0
       
   944 	 * @access public
       
   945 	 * @var bool
       
   946 	 */
       
   947 	var $is_tax = false;
       
   948 
       
   949 	/**
       
   950 	 * Set if query was part of a search result.
       
   951 	 *
       
   952 	 * @since 1.5.0
       
   953 	 * @access public
       
   954 	 * @var bool
       
   955 	 */
       
   956 	var $is_search = false;
       
   957 
       
   958 	/**
       
   959 	 * Set if query is feed display.
       
   960 	 *
       
   961 	 * @since 1.5.0
       
   962 	 * @access public
       
   963 	 * @var bool
       
   964 	 */
       
   965 	var $is_feed = false;
       
   966 
       
   967 	/**
       
   968 	 * Set if query is comment feed display.
       
   969 	 *
       
   970 	 * @since 2.2.0
       
   971 	 * @access public
       
   972 	 * @var bool
       
   973 	 */
       
   974 	var $is_comment_feed = false;
       
   975 
       
   976 	/**
       
   977 	 * Set if query is trackback.
       
   978 	 *
       
   979 	 * @since 1.5.0
       
   980 	 * @access public
       
   981 	 * @var bool
       
   982 	 */
       
   983 	var $is_trackback = false;
       
   984 
       
   985 	/**
       
   986 	 * Set if query is blog homepage.
       
   987 	 *
       
   988 	 * @since 1.5.0
       
   989 	 * @access public
       
   990 	 * @var bool
       
   991 	 */
       
   992 	var $is_home = false;
       
   993 
       
   994 	/**
       
   995 	 * Set if query couldn't found anything.
       
   996 	 *
       
   997 	 * @since 1.5.0
       
   998 	 * @access public
       
   999 	 * @var bool
       
  1000 	 */
       
  1001 	var $is_404 = false;
       
  1002 
       
  1003 	/**
       
  1004 	 * Set if query is within comments popup window.
       
  1005 	 *
       
  1006 	 * @since 1.5.0
       
  1007 	 * @access public
       
  1008 	 * @var bool
       
  1009 	 */
       
  1010 	var $is_comments_popup = false;
       
  1011 
       
  1012 	/**
       
  1013 	 * Set if query is part of administration page.
       
  1014 	 *
       
  1015 	 * @since 1.5.0
       
  1016 	 * @access public
       
  1017 	 * @var bool
       
  1018 	 */
       
  1019 	var $is_admin = false;
       
  1020 
       
  1021 	/**
       
  1022 	 * Set if query is an attachment.
       
  1023 	 *
       
  1024 	 * @since 2.0.0
       
  1025 	 * @access public
       
  1026 	 * @var bool
       
  1027 	 */
       
  1028 	var $is_attachment = false;
       
  1029 
       
  1030 	/**
       
  1031 	 * Set if is single, is a page, or is an attachment.
       
  1032 	 *
       
  1033 	 * @since 2.1.0
       
  1034 	 * @access public
       
  1035 	 * @var bool
       
  1036 	 */
       
  1037 	var $is_singular = false;
       
  1038 
       
  1039 	/**
       
  1040 	 * Set if query is for robots.
       
  1041 	 *
       
  1042 	 * @since 2.1.0
       
  1043 	 * @access public
       
  1044 	 * @var bool
       
  1045 	 */
       
  1046 	var $is_robots = false;
       
  1047 
       
  1048 	/**
       
  1049 	 * Set if query contains posts.
       
  1050 	 *
       
  1051 	 * Basically, the homepage if the option isn't set for the static homepage.
       
  1052 	 *
       
  1053 	 * @since 2.1.0
       
  1054 	 * @access public
       
  1055 	 * @var bool
       
  1056 	 */
       
  1057 	var $is_posts_page = false;
       
  1058 
  1279 
  1059 	/**
  1280 	/**
  1060 	 * Resets query flags to false.
  1281 	 * Resets query flags to false.
  1061 	 *
  1282 	 *
  1062 	 * The query flags are what page info WordPress was able to figure out.
  1283 	 * The query flags are what page info WordPress was able to figure out.
  1064 	 * @since 2.0.0
  1285 	 * @since 2.0.0
  1065 	 * @access private
  1286 	 * @access private
  1066 	 */
  1287 	 */
  1067 	function init_query_flags() {
  1288 	function init_query_flags() {
  1068 		$this->is_single = false;
  1289 		$this->is_single = false;
       
  1290 		$this->is_preview = false;
  1069 		$this->is_page = false;
  1291 		$this->is_page = false;
  1070 		$this->is_archive = false;
  1292 		$this->is_archive = false;
  1071 		$this->is_date = false;
  1293 		$this->is_date = false;
  1072 		$this->is_year = false;
  1294 		$this->is_year = false;
  1073 		$this->is_month = false;
  1295 		$this->is_month = false;
  1081 		$this->is_feed = false;
  1303 		$this->is_feed = false;
  1082 		$this->is_comment_feed = false;
  1304 		$this->is_comment_feed = false;
  1083 		$this->is_trackback = false;
  1305 		$this->is_trackback = false;
  1084 		$this->is_home = false;
  1306 		$this->is_home = false;
  1085 		$this->is_404 = false;
  1307 		$this->is_404 = false;
       
  1308 		$this->is_comments_popup = false;
  1086 		$this->is_paged = false;
  1309 		$this->is_paged = false;
  1087 		$this->is_admin = false;
  1310 		$this->is_admin = false;
  1088 		$this->is_attachment = false;
  1311 		$this->is_attachment = false;
  1089 		$this->is_singular = false;
  1312 		$this->is_singular = false;
  1090 		$this->is_robots = false;
  1313 		$this->is_robots = false;
  1091 		$this->is_posts_page = false;
  1314 		$this->is_posts_page = false;
       
  1315 		$this->is_post_type_archive = false;
  1092 	}
  1316 	}
  1093 
  1317 
  1094 	/**
  1318 	/**
  1095 	 * Initiates object properties and sets default values.
  1319 	 * Initiates object properties and sets default values.
  1096 	 *
  1320 	 *
  1097 	 * @since 1.5.0
  1321 	 * @since 1.5.0
  1098 	 * @access public
  1322 	 * @access public
  1099 	 */
  1323 	 */
  1100 	function init () {
  1324 	function init() {
  1101 		unset($this->posts);
  1325 		unset($this->posts);
  1102 		unset($this->query);
  1326 		unset($this->query);
  1103 		$this->query_vars = array();
  1327 		$this->query_vars = array();
  1104 		unset($this->queried_object);
  1328 		unset($this->queried_object);
  1105 		unset($this->queried_object_id);
  1329 		unset($this->queried_object_id);
  1106 		$this->post_count = 0;
  1330 		$this->post_count = 0;
  1107 		$this->current_post = -1;
  1331 		$this->current_post = -1;
  1108 		$this->in_the_loop = false;
  1332 		$this->in_the_loop = false;
       
  1333 		unset( $this->request );
       
  1334 		unset( $this->post );
       
  1335 		unset( $this->comments );
       
  1336 		unset( $this->comment );
       
  1337 		$this->comment_count = 0;
       
  1338 		$this->current_comment = -1;
       
  1339 		$this->found_posts = 0;
       
  1340 		$this->max_num_pages = 0;
       
  1341 		$this->max_num_comment_pages = 0;
  1109 
  1342 
  1110 		$this->init_query_flags();
  1343 		$this->init_query_flags();
  1111 	}
  1344 	}
  1112 
  1345 
  1113 	/**
  1346 	/**
  1115 	 *
  1348 	 *
  1116 	 * @since 1.5.0
  1349 	 * @since 1.5.0
  1117 	 * @access public
  1350 	 * @access public
  1118 	 */
  1351 	 */
  1119 	function parse_query_vars() {
  1352 	function parse_query_vars() {
  1120 		$this->parse_query('');
  1353 		$this->parse_query();
  1121 	}
  1354 	}
  1122 
  1355 
  1123 	/**
  1356 	/**
  1124 	 * Fills in the query variables, which do not exist within the parameter.
  1357 	 * Fills in the query variables, which do not exist within the parameter.
  1125 	 *
  1358 	 *
  1138 			, 'subpost'
  1371 			, 'subpost'
  1139 			, 'subpost_id'
  1372 			, 'subpost_id'
  1140 			, 'attachment'
  1373 			, 'attachment'
  1141 			, 'attachment_id'
  1374 			, 'attachment_id'
  1142 			, 'name'
  1375 			, 'name'
  1143 			, 'hour'
       
  1144 			, 'static'
  1376 			, 'static'
  1145 			, 'pagename'
  1377 			, 'pagename'
  1146 			, 'page_id'
  1378 			, 'page_id'
  1147 			, 'second'
  1379 			, 'second'
  1148 			, 'minute'
  1380 			, 'minute'
  1161 			, 'paged'
  1393 			, 'paged'
  1162 			, 'comments_popup'
  1394 			, 'comments_popup'
  1163 			, 'meta_key'
  1395 			, 'meta_key'
  1164 			, 'meta_value'
  1396 			, 'meta_value'
  1165 			, 'preview'
  1397 			, 'preview'
       
  1398 			, 's'
       
  1399 			, 'sentence'
       
  1400 			, 'fields'
  1166 		);
  1401 		);
  1167 
  1402 
  1168 		foreach ($keys as $key) {
  1403 		foreach ( $keys as $key ) {
  1169 			if ( !isset($array[$key]))
  1404 			if ( !isset($array[$key]) )
  1170 				$array[$key] = '';
  1405 				$array[$key] = '';
  1171 		}
  1406 		}
  1172 
  1407 
  1173 		$array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1408 		$array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1174 			'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  1409 			'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  1175 
  1410 
  1176 		foreach ( $array_keys as $key ) {
  1411 		foreach ( $array_keys as $key ) {
  1177 			if ( !isset($array[$key]))
  1412 			if ( !isset($array[$key]) )
  1178 				$array[$key] = array();
  1413 				$array[$key] = array();
  1179 		}
  1414 		}
  1180 		return $array;
  1415 		return $array;
  1181 	}
  1416 	}
  1182 
  1417 
  1184 	 * Parse a query string and set query type booleans.
  1419 	 * Parse a query string and set query type booleans.
  1185 	 *
  1420 	 *
  1186 	 * @since 1.5.0
  1421 	 * @since 1.5.0
  1187 	 * @access public
  1422 	 * @access public
  1188 	 *
  1423 	 *
  1189 	 * @param string|array $query
  1424 	 * @param string|array $query Optional query.
  1190 	 */
  1425 	 */
  1191 	function parse_query ($query) {
  1426 	function parse_query( $query =  '' ) {
  1192 		if ( !empty($query) || !isset($this->query) ) {
  1427 		if ( ! empty( $query ) ) {
  1193 			$this->init();
  1428 			$this->init();
  1194 			if ( is_array($query) )
  1429 			$this->query = $this->query_vars = wp_parse_args( $query );
  1195 				$this->query_vars = $query;
  1430 		} elseif ( ! isset( $this->query ) ) {
  1196 			else
  1431 			$this->query = $this->query_vars;
  1197 				parse_str($query, $this->query_vars);
       
  1198 			$this->query = $query;
       
  1199 		}
  1432 		}
  1200 
  1433 
  1201 		$this->query_vars = $this->fill_query_vars($this->query_vars);
  1434 		$this->query_vars = $this->fill_query_vars($this->query_vars);
  1202 		$qv = &$this->query_vars;
  1435 		$qv = &$this->query_vars;
       
  1436 		$this->query_vars_changed = true;
  1203 
  1437 
  1204 		if ( ! empty($qv['robots']) )
  1438 		if ( ! empty($qv['robots']) )
  1205 			$this->is_robots = true;
  1439 			$this->is_robots = true;
  1206 
  1440 
  1207 		$qv['p'] =  absint($qv['p']);
  1441 		$qv['p'] =  absint($qv['p']);
  1217 		$qv['name'] = trim( $qv['name'] );
  1451 		$qv['name'] = trim( $qv['name'] );
  1218 		if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1452 		if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1219 		if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1453 		if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1220 		if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1454 		if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1221 
  1455 
  1222 		// Compat.  Map subpost to attachment.
  1456 		// Compat. Map subpost to attachment.
  1223 		if ( '' != $qv['subpost'] )
  1457 		if ( '' != $qv['subpost'] )
  1224 			$qv['attachment'] = $qv['subpost'];
  1458 			$qv['attachment'] = $qv['subpost'];
  1225 		if ( '' != $qv['subpost_id'] )
  1459 		if ( '' != $qv['subpost_id'] )
  1226 			$qv['attachment_id'] = $qv['subpost_id'];
  1460 			$qv['attachment_id'] = $qv['subpost_id'];
  1227 
  1461 
  1239 			// post is being queried.
  1473 			// post is being queried.
  1240 			$this->is_single = true;
  1474 			$this->is_single = true;
  1241 		} elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1475 		} elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1242 			$this->is_page = true;
  1476 			$this->is_page = true;
  1243 			$this->is_single = false;
  1477 			$this->is_single = false;
  1244 		} elseif ( !empty($qv['s']) ) {
       
  1245 			$this->is_search = true;
       
  1246 		} else {
  1478 		} else {
  1247 		// Look for archive queries.  Dates, categories, authors.
  1479 		// Look for archive queries. Dates, categories, authors, search, post type archives.
       
  1480 
       
  1481 			if ( !empty($qv['s']) ) {
       
  1482 				$this->is_search = true;
       
  1483 			}
  1248 
  1484 
  1249 			if ( '' !== $qv['second'] ) {
  1485 			if ( '' !== $qv['second'] ) {
  1250 				$this->is_time = true;
  1486 				$this->is_time = true;
  1251 				$this->is_date = true;
  1487 				$this->is_date = true;
  1252 			}
  1488 			}
  1260 				$this->is_time = true;
  1496 				$this->is_time = true;
  1261 				$this->is_date = true;
  1497 				$this->is_date = true;
  1262 			}
  1498 			}
  1263 
  1499 
  1264 			if ( $qv['day'] ) {
  1500 			if ( $qv['day'] ) {
  1265 				if (! $this->is_date) {
  1501 				if ( ! $this->is_date ) {
  1266 					$this->is_day = true;
  1502 					$this->is_day = true;
  1267 					$this->is_date = true;
  1503 					$this->is_date = true;
  1268 				}
  1504 				}
  1269 			}
  1505 			}
  1270 
  1506 
  1271 			if ( $qv['monthnum'] ) {
  1507 			if ( $qv['monthnum'] ) {
  1272 				if (! $this->is_date) {
  1508 				if ( ! $this->is_date ) {
  1273 					$this->is_month = true;
  1509 					$this->is_month = true;
  1274 					$this->is_date = true;
  1510 					$this->is_date = true;
  1275 				}
  1511 				}
  1276 			}
  1512 			}
  1277 
  1513 
  1278 			if ( $qv['year'] ) {
  1514 			if ( $qv['year'] ) {
  1279 				if (! $this->is_date) {
  1515 				if ( ! $this->is_date ) {
  1280 					$this->is_year = true;
  1516 					$this->is_year = true;
  1281 					$this->is_date = true;
  1517 					$this->is_date = true;
  1282 				}
  1518 				}
  1283 			}
  1519 			}
  1284 
  1520 
  1285 			if ( $qv['m'] ) {
  1521 			if ( $qv['m'] ) {
  1286 				$this->is_date = true;
  1522 				$this->is_date = true;
  1287 				if (strlen($qv['m']) > 9) {
  1523 				if ( strlen($qv['m']) > 9 ) {
  1288 					$this->is_time = true;
  1524 					$this->is_time = true;
  1289 				} else if (strlen($qv['m']) > 7) {
  1525 				} else if ( strlen($qv['m']) > 7 ) {
  1290 					$this->is_day = true;
  1526 					$this->is_day = true;
  1291 				} else if (strlen($qv['m']) > 5) {
  1527 				} else if ( strlen($qv['m']) > 5 ) {
  1292 					$this->is_month = true;
  1528 					$this->is_month = true;
  1293 				} else {
  1529 				} else {
  1294 					$this->is_year = true;
  1530 					$this->is_year = true;
  1295 				}
  1531 				}
  1296 			}
  1532 			}
  1297 
  1533 
  1298 			if ('' != $qv['w']) {
  1534 			if ( '' != $qv['w'] ) {
  1299 				$this->is_date = true;
  1535 				$this->is_date = true;
  1300 			}
  1536 			}
  1301 
  1537 
  1302 			if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
  1538 			$this->query_vars_hash = false;
  1303 				$this->is_category = false;
  1539 			$this->parse_tax_query( $qv );
  1304 			} else {
  1540 
  1305 				if (strpos($qv['cat'], '-') !== false) {
  1541 			foreach ( $this->tax_query->queries as $tax_query ) {
  1306 					$this->is_category = false;
  1542 				if ( 'NOT IN' != $tax_query['operator'] ) {
  1307 				} else {
  1543 					switch ( $tax_query['taxonomy'] ) {
  1308 					$this->is_category = true;
  1544 						case 'category':
  1309 				}
  1545 							$this->is_category = true;
  1310 			}
  1546 							break;
  1311 
  1547 						case 'post_tag':
  1312 			if ( '' != $qv['category_name'] ) {
  1548 							$this->is_tag = true;
  1313 				$this->is_category = true;
  1549 							break;
  1314 			}
  1550 						default:
  1315 
  1551 							$this->is_tax = true;
  1316 			if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
       
  1317 				$qv['category__in'] = array();
       
  1318 			} else {
       
  1319 				$qv['category__in'] = array_map('absint', $qv['category__in']);
       
  1320 				$this->is_category = true;
       
  1321 			}
       
  1322 
       
  1323 			if ( !is_array($qv['category__not_in']) || empty($qv['category__not_in']) ) {
       
  1324 				$qv['category__not_in'] = array();
       
  1325 			} else {
       
  1326 				$qv['category__not_in'] = array_map('absint', $qv['category__not_in']);
       
  1327 			}
       
  1328 
       
  1329 			if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
       
  1330 				$qv['category__and'] = array();
       
  1331 			} else {
       
  1332 				$qv['category__and'] = array_map('absint', $qv['category__and']);
       
  1333 				$this->is_category = true;
       
  1334 			}
       
  1335 
       
  1336 			if (  '' != $qv['tag'] )
       
  1337 				$this->is_tag = true;
       
  1338 
       
  1339 			$qv['tag_id'] = absint($qv['tag_id']);
       
  1340 			if (  !empty($qv['tag_id']) )
       
  1341 				$this->is_tag = true;
       
  1342 
       
  1343 			if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
       
  1344 				$qv['tag__in'] = array();
       
  1345 			} else {
       
  1346 				$qv['tag__in'] = array_map('absint', $qv['tag__in']);
       
  1347 				$this->is_tag = true;
       
  1348 			}
       
  1349 
       
  1350 			if ( !is_array($qv['tag__not_in']) || empty($qv['tag__not_in']) ) {
       
  1351 				$qv['tag__not_in'] = array();
       
  1352 			} else {
       
  1353 				$qv['tag__not_in'] = array_map('absint', $qv['tag__not_in']);
       
  1354 			}
       
  1355 
       
  1356 			if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
       
  1357 				$qv['tag__and'] = array();
       
  1358 			} else {
       
  1359 				$qv['tag__and'] = array_map('absint', $qv['tag__and']);
       
  1360 				$this->is_category = true;
       
  1361 			}
       
  1362 
       
  1363 			if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
       
  1364 				$qv['tag_slug__in'] = array();
       
  1365 			} else {
       
  1366 				$qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
       
  1367 				$this->is_tag = true;
       
  1368 			}
       
  1369 
       
  1370 			if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
       
  1371 				$qv['tag_slug__and'] = array();
       
  1372 			} else {
       
  1373 				$qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
       
  1374 				$this->is_tag = true;
       
  1375 			}
       
  1376 
       
  1377 			if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
       
  1378 				$this->is_tax = false;
       
  1379 				foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
       
  1380 					if ( $t->query_var && isset($qv[$t->query_var]) && '' != $qv[$t->query_var] ) {
       
  1381 						$qv['taxonomy'] = $taxonomy;
       
  1382 						$qv['term'] = $qv[$t->query_var];
       
  1383 						$this->is_tax = true;
       
  1384 						break;
       
  1385 					}
  1552 					}
  1386 				}
  1553 				}
  1387 			} else {
  1554 			}
  1388 				$this->is_tax = true;
  1555 			unset( $tax_query );
  1389 			}
       
  1390 
  1556 
  1391 			if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1557 			if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1392 				$this->is_author = false;
  1558 				$this->is_author = false;
  1393 			} else {
  1559 			} else {
  1394 				$this->is_author = true;
  1560 				$this->is_author = true;
  1395 			}
  1561 			}
  1396 
  1562 
  1397 			if ( '' != $qv['author_name'] ) {
  1563 			if ( '' != $qv['author_name'] )
  1398 				$this->is_author = true;
  1564 				$this->is_author = true;
  1399 			}
  1565 
  1400 
  1566 			if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1401 			if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax) )
  1567 				$post_type_obj = get_post_type_object( $qv['post_type'] );
       
  1568 				if ( ! empty( $post_type_obj->has_archive ) )
       
  1569 					$this->is_post_type_archive = true;
       
  1570 			}
       
  1571 
       
  1572 			if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1402 				$this->is_archive = true;
  1573 				$this->is_archive = true;
  1403 		}
  1574 		}
  1404 
  1575 
  1405 		if ( '' != $qv['feed'] )
  1576 		if ( '' != $qv['feed'] )
  1406 			$this->is_feed = true;
  1577 			$this->is_feed = true;
  1413 
  1584 
  1414 		if ( '' != $qv['comments_popup'] )
  1585 		if ( '' != $qv['comments_popup'] )
  1415 			$this->is_comments_popup = true;
  1586 			$this->is_comments_popup = true;
  1416 
  1587 
  1417 		// if we're previewing inside the write screen
  1588 		// if we're previewing inside the write screen
  1418 		if ('' != $qv['preview'])
  1589 		if ( '' != $qv['preview'] )
  1419 			$this->is_preview = true;
  1590 			$this->is_preview = true;
  1420 
  1591 
  1421 		if ( is_admin() )
  1592 		if ( is_admin() )
  1422 			$this->is_admin = true;
  1593 			$this->is_admin = true;
  1423 
  1594 
  1433 
  1604 
  1434 		if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup || $this->is_robots ) )
  1605 		if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup || $this->is_robots ) )
  1435 			$this->is_home = true;
  1606 			$this->is_home = true;
  1436 
  1607 
  1437 		// Correct is_* for page_on_front and page_for_posts
  1608 		// Correct is_* for page_on_front and page_for_posts
  1438 		if ( $this->is_home && ( empty($this->query) || $qv['preview'] == 'true' ) && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1609 		if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1439 			$this->is_page = true;
  1610 			$_query = wp_parse_args($this->query);
  1440 			$this->is_home = false;
  1611 			// pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1441 			$qv['page_id'] = get_option('page_on_front');
  1612 			if ( isset($_query['pagename']) && '' == $_query['pagename'] )
       
  1613 				unset($_query['pagename']);
       
  1614 			if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
       
  1615 				$this->is_page = true;
       
  1616 				$this->is_home = false;
       
  1617 				$qv['page_id'] = get_option('page_on_front');
       
  1618 				// Correct <!--nextpage--> for page_on_front
       
  1619 				if ( !empty($qv['paged']) ) {
       
  1620 					$qv['page'] = $qv['paged'];
       
  1621 					unset($qv['paged']);
       
  1622 				}
       
  1623 			}
  1442 		}
  1624 		}
  1443 
  1625 
  1444 		if ( '' != $qv['pagename'] ) {
  1626 		if ( '' != $qv['pagename'] ) {
  1445 			$this->queried_object =& get_page_by_path($qv['pagename']);
  1627 			$this->queried_object = get_page_by_path($qv['pagename']);
  1446 			if ( !empty($this->queried_object) )
  1628 			if ( !empty($this->queried_object) )
  1447 				$this->queried_object_id = (int) $this->queried_object->ID;
  1629 				$this->queried_object_id = (int) $this->queried_object->ID;
  1448 			else
  1630 			else
  1449 				unset($this->queried_object);
  1631 				unset($this->queried_object);
  1450 
  1632 
  1461 				$this->is_home = true;
  1643 				$this->is_home = true;
  1462 				$this->is_posts_page = true;
  1644 				$this->is_posts_page = true;
  1463 			}
  1645 			}
  1464 		}
  1646 		}
  1465 
  1647 
  1466 		if ( !empty($qv['post_type']) )	{
  1648 		if ( !empty($qv['post_type']) ) {
  1467 			if(is_array($qv['post_type']))
  1649 			if ( is_array($qv['post_type']) )
  1468 				$qv['post_type'] = array_map('sanitize_user', $qv['post_type'], array(true));
  1650 				$qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1469 			else
  1651 			else
  1470 				$qv['post_type'] = sanitize_user($qv['post_type'], true);
  1652 				$qv['post_type'] = sanitize_key($qv['post_type']);
  1471 		}
  1653 		}
  1472 
  1654 
  1473 		if ( !empty($qv['post_status']) )
  1655 		if ( ! empty( $qv['post_status'] ) ) {
  1474 			$qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1656 			if ( is_array( $qv['post_status'] ) )
       
  1657 				$qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
       
  1658 			else
       
  1659 				$qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
       
  1660 		}
  1475 
  1661 
  1476 		if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1662 		if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1477 			$this->is_comment_feed = false;
  1663 			$this->is_comment_feed = false;
  1478 
  1664 
  1479 		$this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1665 		$this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1480 		// Done correcting is_* for page_on_front and page_for_posts
  1666 		// Done correcting is_* for page_on_front and page_for_posts
  1481 
  1667 
  1482 		if ('404' == $qv['error'])
  1668 		if ( '404' == $qv['error'] )
  1483 			$this->set_404();
  1669 			$this->set_404();
  1484 
  1670 
  1485 		if ( !empty($query) )
  1671 		$this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1486 			do_action_ref_array('parse_query', array(&$this));
  1672 		$this->query_vars_changed = false;
  1487 	}
  1673 
  1488 
  1674 		do_action_ref_array('parse_query', array(&$this));
  1489 	/**
  1675 	}
  1490 	 * Sets the 404 property and saves whether query is feed.
  1676 
  1491 	 *
  1677 	/*
  1492 	 * @since 2.0.0
  1678 	 * Parses various taxonomy related query vars.
  1493 	 * @access public
  1679 	 *
  1494 	 */
  1680 	 * @access protected
  1495 	function set_404() {
  1681 	 * @since 3.1.0
  1496 		$is_feed = $this->is_feed;
  1682 	 *
  1497 
  1683 	 * @param array &$q The query variables
  1498 		$this->init_query_flags();
  1684 	 */
  1499 		$this->is_404 = true;
  1685 	function parse_tax_query( &$q ) {
  1500 
  1686 		if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1501 		$this->is_feed = $is_feed;
  1687 			$tax_query = $q['tax_query'];
  1502 	}
  1688 		} else {
  1503 
  1689 			$tax_query = array();
  1504 	/**
  1690 		}
  1505 	 * Retrieve query variable.
  1691 
  1506 	 *
  1692 		if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1507 	 * @since 1.5.0
  1693 			$tax_query[] = array(
  1508 	 * @access public
  1694 				'taxonomy' => $q['taxonomy'],
  1509 	 *
  1695 				'terms' => array( $q['term'] ),
  1510 	 * @param string $query_var Query variable key.
  1696 				'field' => 'slug',
  1511 	 * @return mixed
  1697 			);
  1512 	 */
  1698 		}
  1513 	function get($query_var) {
  1699 
  1514 		if (isset($this->query_vars[$query_var])) {
  1700 		foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1515 			return $this->query_vars[$query_var];
  1701 			if ( 'post_tag' == $taxonomy )
  1516 		}
  1702 				continue;	// Handled further down in the $q['tag'] block
  1517 
  1703 
  1518 		return '';
  1704 			if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1519 	}
  1705 				$tax_query_defaults = array(
  1520 
  1706 					'taxonomy' => $taxonomy,
  1521 	/**
  1707 					'field' => 'slug',
  1522 	 * Set query variable.
  1708 				);
  1523 	 *
  1709 
  1524 	 * @since 1.5.0
  1710  				if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1525 	 * @access public
  1711 					$q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1526 	 *
       
  1527 	 * @param string $query_var Query variable key.
       
  1528 	 * @param mixed $value Query variable value.
       
  1529 	 */
       
  1530 	function set($query_var, $value) {
       
  1531 		$this->query_vars[$query_var] = $value;
       
  1532 	}
       
  1533 
       
  1534 	/**
       
  1535 	 * Retrieve the posts based on query variables.
       
  1536 	 *
       
  1537 	 * There are a few filters and actions that can be used to modify the post
       
  1538 	 * database query.
       
  1539 	 *
       
  1540 	 * @since 1.5.0
       
  1541 	 * @access public
       
  1542 	 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
       
  1543 	 *
       
  1544 	 * @return array List of posts.
       
  1545 	 */
       
  1546 	function &get_posts() {
       
  1547 		global $wpdb, $user_ID;
       
  1548 
       
  1549 		do_action_ref_array('pre_get_posts', array(&$this));
       
  1550 
       
  1551 		// Shorthand.
       
  1552 		$q = &$this->query_vars;
       
  1553 
       
  1554 		$q = $this->fill_query_vars($q);
       
  1555 
       
  1556 		// First let's clear some variables
       
  1557 		$distinct = '';
       
  1558 		$whichcat = '';
       
  1559 		$whichauthor = '';
       
  1560 		$whichmimetype = '';
       
  1561 		$where = '';
       
  1562 		$limits = '';
       
  1563 		$join = '';
       
  1564 		$search = '';
       
  1565 		$groupby = '';
       
  1566 		$fields = "$wpdb->posts.*";
       
  1567 		$post_status_join = false;
       
  1568 		$page = 1;
       
  1569 
       
  1570 		if ( !isset($q['caller_get_posts']) )
       
  1571 			$q['caller_get_posts'] = false;
       
  1572 
       
  1573 		if ( !isset($q['suppress_filters']) )
       
  1574 			$q['suppress_filters'] = false;
       
  1575 
       
  1576 		if ( !isset($q['post_type']) ) {
       
  1577 			if ( $this->is_search )
       
  1578 				$q['post_type'] = 'any';
       
  1579 			else
       
  1580 				$q['post_type'] = '';
       
  1581 		}
       
  1582 		$post_type = $q['post_type'];
       
  1583 		if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
       
  1584 			$q['posts_per_page'] = get_option('posts_per_page');
       
  1585 		if ( isset($q['showposts']) && $q['showposts'] ) {
       
  1586 			$q['showposts'] = (int) $q['showposts'];
       
  1587 			$q['posts_per_page'] = $q['showposts'];
       
  1588 		}
       
  1589 		if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
       
  1590 			$q['posts_per_page'] = $q['posts_per_archive_page'];
       
  1591 		if ( !isset($q['nopaging']) ) {
       
  1592 			if ($q['posts_per_page'] == -1) {
       
  1593 				$q['nopaging'] = true;
       
  1594 			} else {
       
  1595 				$q['nopaging'] = false;
       
  1596 			}
       
  1597 		}
       
  1598 		if ( $this->is_feed ) {
       
  1599 			$q['posts_per_page'] = get_option('posts_per_rss');
       
  1600 			$q['nopaging'] = false;
       
  1601 		}
       
  1602 		$q['posts_per_page'] = (int) $q['posts_per_page'];
       
  1603 		if ( $q['posts_per_page'] < -1 )
       
  1604 			$q['posts_per_page'] = abs($q['posts_per_page']);
       
  1605 		else if ( $q['posts_per_page'] == 0 )
       
  1606 			$q['posts_per_page'] = 1;
       
  1607 
       
  1608 		if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
       
  1609 			$q['comments_per_page'] = get_option('comments_per_page');
       
  1610 
       
  1611 		if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
       
  1612 			$this->is_page = true;
       
  1613 			$this->is_home = false;
       
  1614 			$q['page_id'] = get_option('page_on_front');
       
  1615 		}
       
  1616 
       
  1617 		if (isset($q['page'])) {
       
  1618 			$q['page'] = trim($q['page'], '/');
       
  1619 			$q['page'] = absint($q['page']);
       
  1620 		}
       
  1621 
       
  1622 		// If a month is specified in the querystring, load that month
       
  1623 		if ( $q['m'] ) {
       
  1624 			$q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
       
  1625 			$where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
       
  1626 			if (strlen($q['m'])>5)
       
  1627 				$where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
       
  1628 			if (strlen($q['m'])>7)
       
  1629 				$where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
       
  1630 			if (strlen($q['m'])>9)
       
  1631 				$where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
       
  1632 			if (strlen($q['m'])>11)
       
  1633 				$where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
       
  1634 			if (strlen($q['m'])>13)
       
  1635 				$where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
       
  1636 		}
       
  1637 
       
  1638 		if ( '' !== $q['hour'] )
       
  1639 			$where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
       
  1640 
       
  1641 		if ( '' !== $q['minute'] )
       
  1642 			$where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
       
  1643 
       
  1644 		if ( '' !== $q['second'] )
       
  1645 			$where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
       
  1646 
       
  1647 		if ( $q['year'] )
       
  1648 			$where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
       
  1649 
       
  1650 		if ( $q['monthnum'] )
       
  1651 			$where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
       
  1652 
       
  1653 		if ( $q['day'] )
       
  1654 			$where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
       
  1655 
       
  1656 		if ('' != $q['name']) {
       
  1657 			$q['name'] = sanitize_title($q['name']);
       
  1658 			$where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
       
  1659 		} else if ('' != $q['pagename']) {
       
  1660 			if ( isset($this->queried_object_id) )
       
  1661 				$reqpage = $this->queried_object_id;
       
  1662 			else {
       
  1663 				$reqpage = get_page_by_path($q['pagename']);
       
  1664 				if ( !empty($reqpage) )
       
  1665 					$reqpage = $reqpage->ID;
       
  1666 				else
       
  1667 					$reqpage = 0;
       
  1668 			}
       
  1669 
       
  1670 			$page_for_posts = get_option('page_for_posts');
       
  1671 			if  ( ('page' != get_option('show_on_front') ) ||  empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
       
  1672 				$q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
       
  1673 				$page_paths = '/' . trim($q['pagename'], '/');
       
  1674 				$q['pagename'] = sanitize_title(basename($page_paths));
       
  1675 				$q['name'] = $q['pagename'];
       
  1676 				$where .= " AND ($wpdb->posts.ID = '$reqpage')";
       
  1677 				$reqpage_obj = get_page($reqpage);
       
  1678 				if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
       
  1679 					$this->is_attachment = true;
       
  1680 					$this->is_page = true;
       
  1681 					$q['attachment_id'] = $reqpage;
       
  1682 				}
  1712 				}
  1683 			}
  1713 
  1684 		} elseif ('' != $q['attachment']) {
  1714 				$term = $q[$t->query_var];
  1685 			$q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
  1715 
  1686 			$attach_paths = '/' . trim($q['attachment'], '/');
  1716 				if ( strpos($term, '+') !== false ) {
  1687 			$q['attachment'] = sanitize_title(basename($attach_paths));
  1717 					$terms = preg_split( '/[+]+/', $term );
  1688 			$q['name'] = $q['attachment'];
  1718 					foreach ( $terms as $term ) {
  1689 			$where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  1719 						$tax_query[] = array_merge( $tax_query_defaults, array(
  1690 		}
  1720 							'terms' => array( $term )
  1691 
  1721 						) );
  1692 		if ( $q['w'] )
  1722 					}
  1693 			$where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'";
  1723 				} else {
  1694 
  1724 					$tax_query[] = array_merge( $tax_query_defaults, array(
  1695 		if ( intval($q['comments_popup']) )
  1725 						'terms' => preg_split( '/[,]+/', $term )
  1696 			$q['p'] = absint($q['comments_popup']);
  1726 					) );
  1697 
  1727 				}
  1698 		// If an attachment is requested by number, let it supercede any post number.
       
  1699 		if ( $q['attachment_id'] )
       
  1700 			$q['p'] = absint($q['attachment_id']);
       
  1701 
       
  1702 		// If a post number is specified, load that post
       
  1703 		if ( $q['p'] ) {
       
  1704 			$where .= " AND {$wpdb->posts}.ID = " . $q['p'];
       
  1705 		} elseif ( $q['post__in'] ) {
       
  1706 			$post__in = implode(',', array_map( 'absint', $q['post__in'] ));
       
  1707 			$where .= " AND {$wpdb->posts}.ID IN ($post__in)";
       
  1708 		} elseif ( $q['post__not_in'] ) {
       
  1709 			$post__not_in = implode(',',  array_map( 'absint', $q['post__not_in'] ));
       
  1710 			$where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
       
  1711 		}
       
  1712 
       
  1713 		if ( is_numeric($q['post_parent']) )
       
  1714 			$where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
       
  1715 
       
  1716 		if ( $q['page_id'] ) {
       
  1717 			if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
       
  1718 				$q['p'] = $q['page_id'];
       
  1719 				$where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
       
  1720 			}
       
  1721 		}
       
  1722 
       
  1723 		// If a search pattern is specified, load the posts that match
       
  1724 		if ( !empty($q['s']) ) {
       
  1725 			// added slashes screw with quote grouping when done early, so done later
       
  1726 			$q['s'] = stripslashes($q['s']);
       
  1727 			if ( !empty($q['sentence']) ) {
       
  1728 				$q['search_terms'] = array($q['s']);
       
  1729 			} else {
       
  1730 				preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
       
  1731 				$q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
       
  1732 			}
       
  1733 			$n = !empty($q['exact']) ? '' : '%';
       
  1734 			$searchand = '';
       
  1735 			foreach( (array) $q['search_terms'] as $term) {
       
  1736 				$term = addslashes_gpc($term);
       
  1737 				$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
       
  1738 				$searchand = ' AND ';
       
  1739 			}
       
  1740 			$term = esc_sql($q['s']);
       
  1741 			if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
       
  1742 				$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
       
  1743 
       
  1744 			if ( !empty($search) ) {
       
  1745 				$search = " AND ({$search}) ";
       
  1746 				if ( !is_user_logged_in() )
       
  1747 					$search .= " AND ($wpdb->posts.post_password = '') ";
       
  1748 			}
  1728 			}
  1749 		}
  1729 		}
  1750 
  1730 
  1751 		// Category stuff
  1731 		// Category stuff
  1752 
  1732 		if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
  1753 		if ( empty($q['cat']) || ($q['cat'] == '0') ||
       
  1754 				// Bypass cat checks if fetching specific posts
       
  1755 				$this->is_singular ) {
       
  1756 			$whichcat = '';
       
  1757 		} else {
       
  1758 			$q['cat'] = ''.urldecode($q['cat']).'';
  1733 			$q['cat'] = ''.urldecode($q['cat']).'';
  1759 			$q['cat'] = addslashes_gpc($q['cat']);
  1734 			$q['cat'] = addslashes_gpc($q['cat']);
  1760 			$cat_array = preg_split('/[,\s]+/', $q['cat']);
  1735 			$cat_array = preg_split('/[,\s]+/', $q['cat']);
  1761 			$q['cat'] = '';
  1736 			$q['cat'] = '';
  1762 			$req_cats = array();
  1737 			$req_cats = array();
  1765 				$req_cats[] = $cat;
  1740 				$req_cats[] = $cat;
  1766 				$in = ($cat > 0);
  1741 				$in = ($cat > 0);
  1767 				$cat = abs($cat);
  1742 				$cat = abs($cat);
  1768 				if ( $in ) {
  1743 				if ( $in ) {
  1769 					$q['category__in'][] = $cat;
  1744 					$q['category__in'][] = $cat;
  1770 					$q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
  1745 					$q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
  1771 				} else {
  1746 				} else {
  1772 					$q['category__not_in'][] = $cat;
  1747 					$q['category__not_in'][] = $cat;
  1773 					$q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
  1748 					$q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
  1774 				}
  1749 				}
  1775 			}
  1750 			}
  1776 			$q['cat'] = implode(',', $req_cats);
  1751 			$q['cat'] = implode(',', $req_cats);
  1777 		}
  1752 		}
  1778 
  1753 
  1779 		if ( !empty($q['category__in']) ) {
  1754 		if ( !empty($q['category__in']) ) {
  1780 			$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) ";
  1755 			$q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
  1781 			$whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
  1756 			$tax_query[] = array(
  1782 			$include_cats = "'" . implode("', '", $q['category__in']) . "'";
  1757 				'taxonomy' => 'category',
  1783 			$whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
  1758 				'terms' => $q['category__in'],
       
  1759 				'field' => 'term_id',
       
  1760 				'include_children' => false
       
  1761 			);
  1784 		}
  1762 		}
  1785 
  1763 
  1786 		if ( !empty($q['category__not_in']) ) {
  1764 		if ( !empty($q['category__not_in']) ) {
  1787 			$cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
  1765 			$q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
  1788 			$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) )";
  1766 			$tax_query[] = array(
  1789 		}
  1767 				'taxonomy' => 'category',
  1790 
  1768 				'terms' => $q['category__not_in'],
  1791 		// Category stuff for nice URLs
  1769 				'operator' => 'NOT IN',
  1792 		if ( '' != $q['category_name'] && !$this->is_singular ) {
  1770 				'include_children' => false
  1793 			$q['category_name'] = implode('/', array_map('sanitize_title', explode('/', $q['category_name'])));
  1771 			);
  1794 			$reqcat = get_category_by_path($q['category_name']);
  1772 		}
  1795 			$q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
  1773 
  1796 			$cat_paths = '/' . trim($q['category_name'], '/');
  1774 		if ( !empty($q['category__and']) ) {
  1797 			$q['category_name'] = sanitize_title(basename($cat_paths));
  1775 			$q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
  1798 
  1776 			$tax_query[] = array(
  1799 			$cat_paths = '/' . trim(urldecode($q['category_name']), '/');
  1777 				'taxonomy' => 'category',
  1800 			$q['category_name'] = sanitize_title(basename($cat_paths));
  1778 				'terms' => $q['category__and'],
  1801 			$cat_paths = explode('/', $cat_paths);
  1779 				'field' => 'term_id',
  1802 			$cat_path = '';
  1780 				'operator' => 'AND',
  1803 			foreach ( (array) $cat_paths as $pathdir )
  1781 				'include_children' => false
  1804 				$cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
  1782 			);
  1805 
  1783 		}
  1806 			//if we don't match the entire hierarchy fallback on just matching the nicename
  1784 
  1807 			if ( empty($reqcat) )
  1785 		// Tag stuff
  1808 				$reqcat = get_category_by_path($q['category_name'], false);
  1786 		if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1809 
       
  1810 			if ( !empty($reqcat) )
       
  1811 				$reqcat = $reqcat->term_id;
       
  1812 			else
       
  1813 				$reqcat = 0;
       
  1814 
       
  1815 			$q['cat'] = $reqcat;
       
  1816 
       
  1817 			$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) ";
       
  1818 			$whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
       
  1819 			$in_cats = array($q['cat']);
       
  1820 			$in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
       
  1821 			$in_cats = "'" . implode("', '", $in_cats) . "'";
       
  1822 			$whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
       
  1823 			$groupby = "{$wpdb->posts}.ID";
       
  1824 		}
       
  1825 
       
  1826 		// Tags
       
  1827 		if ( '' != $q['tag'] ) {
       
  1828 			if ( strpos($q['tag'], ',') !== false ) {
  1787 			if ( strpos($q['tag'], ',') !== false ) {
  1829 				$tags = preg_split('/[,\s]+/', $q['tag']);
  1788 				$tags = preg_split('/[,\s]+/', $q['tag']);
  1830 				foreach ( (array) $tags as $tag ) {
  1789 				foreach ( (array) $tags as $tag ) {
  1831 					$tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1790 					$tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1832 					$q['tag_slug__in'][] = $tag;
  1791 					$q['tag_slug__in'][] = $tag;
  1841 				$q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1800 				$q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1842 				$q['tag_slug__in'][] = $q['tag'];
  1801 				$q['tag_slug__in'][] = $q['tag'];
  1843 			}
  1802 			}
  1844 		}
  1803 		}
  1845 
  1804 
  1846 		if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) {
  1805 		if ( !empty($q['tag_id']) ) {
  1847 			$groupby = "{$wpdb->posts}.ID";
  1806 			$q['tag_id'] = absint( $q['tag_id'] );
  1848 		}
  1807 			$tax_query[] = array(
  1849 
  1808 				'taxonomy' => 'post_tag',
  1850 		if ( !empty($q['tag__in']) && empty($q['cat']) ) {
  1809 				'terms' => $q['tag_id']
  1851 			$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) ";
  1810 			);
  1852 			$whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
  1811 		}
  1853 			$include_tags = "'" . implode("', '", $q['tag__in']) . "'";
  1812 
  1854 			$whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
  1813 		if ( !empty($q['tag__in']) ) {
  1855 			$reqtag = is_term( $q['tag__in'][0], 'post_tag' );
  1814 			$q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1856 			if ( !empty($reqtag) )
  1815 			$tax_query[] = array(
  1857 				$q['tag_id'] = $reqtag['term_id'];
  1816 				'taxonomy' => 'post_tag',
  1858 		}
  1817 				'terms' => $q['tag__in']
  1859 
  1818 			);
  1860 		if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {
       
  1861 			$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) ";
       
  1862 			$whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
       
  1863 			$include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
       
  1864 			$whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
       
  1865 			$reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
       
  1866 			if ( !empty($reqtag) )
       
  1867 				$q['tag_id'] = $reqtag->term_id;
       
  1868 		}
  1819 		}
  1869 
  1820 
  1870 		if ( !empty($q['tag__not_in']) ) {
  1821 		if ( !empty($q['tag__not_in']) ) {
  1871 			$tag_string = "'" . implode("', '", $q['tag__not_in']) . "'";
  1822 			$q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1872 			$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) )";
  1823 			$tax_query[] = array(
  1873 		}
  1824 				'taxonomy' => 'post_tag',
  1874 
  1825 				'terms' => $q['tag__not_in'],
  1875 		// Tag and slug intersections.
  1826 				'operator' => 'NOT IN'
  1876 		$intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
  1827 			);
  1877 		$tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
  1828 		}
  1878 		foreach ($intersections as $item => $taxonomy) {
  1829 
  1879 			if ( empty($q[$item]) ) continue;
  1830 		if ( !empty($q['tag__and']) ) {
  1880 			if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
  1831 			$q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1881 
  1832 			$tax_query[] = array(
  1882 			if ( $item != 'category__and' ) {
  1833 				'taxonomy' => 'post_tag',
  1883 				$reqtag = is_term( $q[$item][0], 'post_tag' );
  1834 				'terms' => $q['tag__and'],
  1884 				if ( !empty($reqtag) )
  1835 				'operator' => 'AND'
  1885 					$q['tag_id'] = $reqtag['term_id'];
  1836 			);
  1886 			}
  1837 		}
  1887 
  1838 
  1888 			if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) )
  1839 		if ( !empty($q['tag_slug__in']) ) {
  1889 				$taxonomy_field = 'slug';
  1840 			$q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
       
  1841 			$tax_query[] = array(
       
  1842 				'taxonomy' => 'post_tag',
       
  1843 				'terms' => $q['tag_slug__in'],
       
  1844 				'field' => 'slug'
       
  1845 			);
       
  1846 		}
       
  1847 
       
  1848 		if ( !empty($q['tag_slug__and']) ) {
       
  1849 			$q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
       
  1850 			$tax_query[] = array(
       
  1851 				'taxonomy' => 'post_tag',
       
  1852 				'terms' => $q['tag_slug__and'],
       
  1853 				'field' => 'slug',
       
  1854 				'operator' => 'AND'
       
  1855 			);
       
  1856 		}
       
  1857 
       
  1858 		$this->tax_query = new WP_Tax_Query( $tax_query );
       
  1859 	}
       
  1860 
       
  1861 	/**
       
  1862 	 * Sets the 404 property and saves whether query is feed.
       
  1863 	 *
       
  1864 	 * @since 2.0.0
       
  1865 	 * @access public
       
  1866 	 */
       
  1867 	function set_404() {
       
  1868 		$is_feed = $this->is_feed;
       
  1869 
       
  1870 		$this->init_query_flags();
       
  1871 		$this->is_404 = true;
       
  1872 
       
  1873 		$this->is_feed = $is_feed;
       
  1874 	}
       
  1875 
       
  1876 	/**
       
  1877 	 * Retrieve query variable.
       
  1878 	 *
       
  1879 	 * @since 1.5.0
       
  1880 	 * @access public
       
  1881 	 *
       
  1882 	 * @param string $query_var Query variable key.
       
  1883 	 * @return mixed
       
  1884 	 */
       
  1885 	function get($query_var) {
       
  1886 		if ( isset($this->query_vars[$query_var]) )
       
  1887 			return $this->query_vars[$query_var];
       
  1888 
       
  1889 		return '';
       
  1890 	}
       
  1891 
       
  1892 	/**
       
  1893 	 * Set query variable.
       
  1894 	 *
       
  1895 	 * @since 1.5.0
       
  1896 	 * @access public
       
  1897 	 *
       
  1898 	 * @param string $query_var Query variable key.
       
  1899 	 * @param mixed $value Query variable value.
       
  1900 	 */
       
  1901 	function set($query_var, $value) {
       
  1902 		$this->query_vars[$query_var] = $value;
       
  1903 	}
       
  1904 
       
  1905 	/**
       
  1906 	 * Retrieve the posts based on query variables.
       
  1907 	 *
       
  1908 	 * There are a few filters and actions that can be used to modify the post
       
  1909 	 * database query.
       
  1910 	 *
       
  1911 	 * @since 1.5.0
       
  1912 	 * @access public
       
  1913 	 * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
       
  1914 	 *
       
  1915 	 * @return array List of posts.
       
  1916 	 */
       
  1917 	function &get_posts() {
       
  1918 		global $wpdb, $user_ID, $_wp_using_ext_object_cache;
       
  1919 
       
  1920 		$this->parse_query();
       
  1921 
       
  1922 		do_action_ref_array('pre_get_posts', array(&$this));
       
  1923 
       
  1924 		// Shorthand.
       
  1925 		$q = &$this->query_vars;
       
  1926 
       
  1927 		// Fill again in case pre_get_posts unset some vars.
       
  1928 		$q = $this->fill_query_vars($q);
       
  1929 
       
  1930 		// Parse meta query
       
  1931 		$this->meta_query = new WP_Meta_Query();
       
  1932 		$this->meta_query->parse_query_vars( $q );
       
  1933 
       
  1934 		// Set a flag if a pre_get_posts hook changed the query vars.
       
  1935 		$hash = md5( serialize( $this->query_vars ) );
       
  1936 		if ( $hash != $this->query_vars_hash ) {
       
  1937 			$this->query_vars_changed = true;
       
  1938 			$this->query_vars_hash = $hash;
       
  1939 		}
       
  1940 		unset($hash);
       
  1941 
       
  1942 		// First let's clear some variables
       
  1943 		$distinct = '';
       
  1944 		$whichauthor = '';
       
  1945 		$whichmimetype = '';
       
  1946 		$where = '';
       
  1947 		$limits = '';
       
  1948 		$join = '';
       
  1949 		$search = '';
       
  1950 		$groupby = '';
       
  1951 		$fields = '';
       
  1952 		$post_status_join = false;
       
  1953 		$page = 1;
       
  1954 
       
  1955 		if ( isset( $q['caller_get_posts'] ) ) {
       
  1956 			_deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
       
  1957 			if ( !isset( $q['ignore_sticky_posts'] ) )
       
  1958 				$q['ignore_sticky_posts'] = $q['caller_get_posts'];
       
  1959 		}
       
  1960 
       
  1961 		if ( !isset( $q['ignore_sticky_posts'] ) )
       
  1962 			$q['ignore_sticky_posts'] = false;
       
  1963 
       
  1964 		if ( !isset($q['suppress_filters']) )
       
  1965 			$q['suppress_filters'] = false;
       
  1966 
       
  1967 		if ( !isset($q['cache_results']) ) {
       
  1968 			if ( $_wp_using_ext_object_cache )
       
  1969 				$q['cache_results'] = false;
  1890 			else
  1970 			else
  1891 				$taxonomy_field = 'term_id';
  1971 				$q['cache_results'] = true;
  1892 
  1972 		}
  1893 			$q[$item] = array_unique($q[$item]);
  1973 
  1894 			$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)";
  1974 		if ( !isset($q['update_post_term_cache']) )
  1895 			$tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
  1975 			$q['update_post_term_cache'] = true;
  1896 			if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
  1976 
  1897 				$tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
  1977 		if ( !isset($q['update_post_meta_cache']) )
  1898 			}
  1978 			$q['update_post_meta_cache'] = true;
  1899 			$post_ids = $wpdb->get_col($tsql);
  1979 
  1900 
  1980 		if ( !isset($q['post_type']) ) {
  1901 			if ( count($post_ids) )
  1981 			if ( $this->is_search )
  1902 				$whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
  1982 				$q['post_type'] = 'any';
  1903 			else {
  1983 			else
  1904 				$whichcat = " AND 0 = 1";
  1984 				$q['post_type'] = '';
       
  1985 		}
       
  1986 		$post_type = $q['post_type'];
       
  1987 		if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
       
  1988 			$q['posts_per_page'] = get_option('posts_per_page');
       
  1989 		if ( isset($q['showposts']) && $q['showposts'] ) {
       
  1990 			$q['showposts'] = (int) $q['showposts'];
       
  1991 			$q['posts_per_page'] = $q['showposts'];
       
  1992 		}
       
  1993 		if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
       
  1994 			$q['posts_per_page'] = $q['posts_per_archive_page'];
       
  1995 		if ( !isset($q['nopaging']) ) {
       
  1996 			if ( $q['posts_per_page'] == -1 ) {
       
  1997 				$q['nopaging'] = true;
       
  1998 			} else {
       
  1999 				$q['nopaging'] = false;
       
  2000 			}
       
  2001 		}
       
  2002 		if ( $this->is_feed ) {
       
  2003 			$q['posts_per_page'] = get_option('posts_per_rss');
       
  2004 			$q['nopaging'] = false;
       
  2005 		}
       
  2006 		$q['posts_per_page'] = (int) $q['posts_per_page'];
       
  2007 		if ( $q['posts_per_page'] < -1 )
       
  2008 			$q['posts_per_page'] = abs($q['posts_per_page']);
       
  2009 		else if ( $q['posts_per_page'] == 0 )
       
  2010 			$q['posts_per_page'] = 1;
       
  2011 
       
  2012 		if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
       
  2013 			$q['comments_per_page'] = get_option('comments_per_page');
       
  2014 
       
  2015 		if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
       
  2016 			$this->is_page = true;
       
  2017 			$this->is_home = false;
       
  2018 			$q['page_id'] = get_option('page_on_front');
       
  2019 		}
       
  2020 
       
  2021 		if ( isset($q['page']) ) {
       
  2022 			$q['page'] = trim($q['page'], '/');
       
  2023 			$q['page'] = absint($q['page']);
       
  2024 		}
       
  2025 
       
  2026 		// If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
       
  2027 		if ( isset($q['no_found_rows']) )
       
  2028 			$q['no_found_rows'] = (bool) $q['no_found_rows'];
       
  2029 		else
       
  2030 			$q['no_found_rows'] = false;
       
  2031 
       
  2032 		switch ( $q['fields'] ) {
       
  2033 			case 'ids':
       
  2034 				$fields = "$wpdb->posts.ID";
  1905 				break;
  2035 				break;
  1906 			}
  2036 			case 'id=>parent':
  1907 		}
  2037 				$fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  1908 
  2038 				break;
  1909 		// Taxonomies
  2039 			default:
  1910 		if ( $this->is_tax ) {
  2040 				$fields = "$wpdb->posts.*";
  1911 			if ( '' != $q['taxonomy'] ) {
  2041 		}
  1912 				$taxonomy = $q['taxonomy'];
  2042 
  1913 				$tt[$taxonomy] = $q['term'];
  2043 		// If a month is specified in the querystring, load that month
  1914 				$terms = get_terms($q['taxonomy'], array('slug'=>$q['term']));
  2044 		if ( $q['m'] ) {
       
  2045 			$q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
       
  2046 			$where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
       
  2047 			if ( strlen($q['m']) > 5 )
       
  2048 				$where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
       
  2049 			if ( strlen($q['m']) > 7 )
       
  2050 				$where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
       
  2051 			if ( strlen($q['m']) > 9 )
       
  2052 				$where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
       
  2053 			if ( strlen($q['m']) > 11 )
       
  2054 				$where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
       
  2055 			if ( strlen($q['m']) > 13 )
       
  2056 				$where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
       
  2057 		}
       
  2058 
       
  2059 		if ( '' !== $q['hour'] )
       
  2060 			$where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
       
  2061 
       
  2062 		if ( '' !== $q['minute'] )
       
  2063 			$where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
       
  2064 
       
  2065 		if ( '' !== $q['second'] )
       
  2066 			$where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
       
  2067 
       
  2068 		if ( $q['year'] )
       
  2069 			$where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
       
  2070 
       
  2071 		if ( $q['monthnum'] )
       
  2072 			$where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
       
  2073 
       
  2074 		if ( $q['day'] )
       
  2075 			$where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
       
  2076 
       
  2077 		// If we've got a post_type AND its not "any" post_type.
       
  2078 		if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
       
  2079 			foreach ( (array)$q['post_type'] as $_post_type ) {
       
  2080 				$ptype_obj = get_post_type_object($_post_type);
       
  2081 				if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
       
  2082 					continue;
       
  2083 
       
  2084 				if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
       
  2085 					// Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
       
  2086 					$q['name'] = $q[ $ptype_obj->query_var ];
       
  2087 				} else {
       
  2088 					// Hierarchical post_types will operate through the
       
  2089 					$q['pagename'] = $q[ $ptype_obj->query_var ];
       
  2090 					$q['name'] = '';
       
  2091 				}
       
  2092 
       
  2093 				// Only one request for a slug is possible, this is why name & pagename are overwritten above.
       
  2094 				break;
       
  2095 			} //end foreach
       
  2096 			unset($ptype_obj);
       
  2097 		}
       
  2098 
       
  2099 		if ( '' != $q['name'] ) {
       
  2100 			$q['name'] = sanitize_title_for_query( $q['name'] );
       
  2101 			$where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
       
  2102 		} elseif ( '' != $q['pagename'] ) {
       
  2103 			if ( isset($this->queried_object_id) ) {
       
  2104 				$reqpage = $this->queried_object_id;
  1915 			} else {
  2105 			} else {
  1916 				foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  2106 				if ( 'page' != $q['post_type'] ) {
  1917 					if ( $t->query_var && '' != $q[$t->query_var] ) {
  2107 					foreach ( (array)$q['post_type'] as $_post_type ) {
  1918 						$terms = get_terms($taxonomy, array('slug'=>$q[$t->query_var]));
  2108 						$ptype_obj = get_post_type_object($_post_type);
  1919 						if ( !is_wp_error($terms) )
  2109 						if ( !$ptype_obj || !$ptype_obj->hierarchical )
       
  2110 							continue;
       
  2111 
       
  2112 						$reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
       
  2113 						if ( $reqpage )
  1920 							break;
  2114 							break;
  1921 					}
  2115 					}
       
  2116 					unset($ptype_obj);
       
  2117 				} else {
       
  2118 					$reqpage = get_page_by_path($q['pagename']);
  1922 				}
  2119 				}
  1923 			}
  2120 				if ( !empty($reqpage) )
  1924 			if ( is_wp_error($terms) || empty($terms) ) {
  2121 					$reqpage = $reqpage->ID;
  1925 				$whichcat = " AND 0 ";
  2122 				else
       
  2123 					$reqpage = 0;
       
  2124 			}
       
  2125 
       
  2126 			$page_for_posts = get_option('page_for_posts');
       
  2127 			if  ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
       
  2128 				$q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
       
  2129 				$q['name'] = $q['pagename'];
       
  2130 				$where .= " AND ($wpdb->posts.ID = '$reqpage')";
       
  2131 				$reqpage_obj = get_page($reqpage);
       
  2132 				if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
       
  2133 					$this->is_attachment = true;
       
  2134 					$post_type = $q['post_type'] = 'attachment';
       
  2135 					$this->is_page = true;
       
  2136 					$q['attachment_id'] = $reqpage;
       
  2137 				}
       
  2138 			}
       
  2139 		} elseif ( '' != $q['attachment'] ) {
       
  2140 			$q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
       
  2141 			$q['name'] = $q['attachment'];
       
  2142 			$where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
       
  2143 		}
       
  2144 
       
  2145 		if ( $q['w'] )
       
  2146 			$where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
       
  2147 
       
  2148 		if ( intval($q['comments_popup']) )
       
  2149 			$q['p'] = absint($q['comments_popup']);
       
  2150 
       
  2151 		// If an attachment is requested by number, let it supersede any post number.
       
  2152 		if ( $q['attachment_id'] )
       
  2153 			$q['p'] = absint($q['attachment_id']);
       
  2154 
       
  2155 		// If a post number is specified, load that post
       
  2156 		if ( $q['p'] ) {
       
  2157 			$where .= " AND {$wpdb->posts}.ID = " . $q['p'];
       
  2158 		} elseif ( $q['post__in'] ) {
       
  2159 			$post__in = implode(',', array_map( 'absint', $q['post__in'] ));
       
  2160 			$where .= " AND {$wpdb->posts}.ID IN ($post__in)";
       
  2161 		} elseif ( $q['post__not_in'] ) {
       
  2162 			$post__not_in = implode(',',  array_map( 'absint', $q['post__not_in'] ));
       
  2163 			$where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
       
  2164 		}
       
  2165 
       
  2166 		if ( is_numeric($q['post_parent']) )
       
  2167 			$where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
       
  2168 
       
  2169 		if ( $q['page_id'] ) {
       
  2170 			if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
       
  2171 				$q['p'] = $q['page_id'];
       
  2172 				$where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
       
  2173 			}
       
  2174 		}
       
  2175 
       
  2176 		// If a search pattern is specified, load the posts that match
       
  2177 		if ( !empty($q['s']) ) {
       
  2178 			// added slashes screw with quote grouping when done early, so done later
       
  2179 			$q['s'] = stripslashes($q['s']);
       
  2180 			if ( !empty($q['sentence']) ) {
       
  2181 				$q['search_terms'] = array($q['s']);
  1926 			} else {
  2182 			} else {
  1927 				foreach ( $terms as $term )
  2183 				preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
  1928 					$term_ids[] = $term->term_id;
  2184 				$q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
  1929 				$post_ids = get_objects_in_term($term_ids, $taxonomy);
  2185 			}
  1930 				if ( !is_wp_error($post_ids) && count($post_ids) ) {
  2186 			$n = !empty($q['exact']) ? '' : '%';
  1931 					$whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
  2187 			$searchand = '';
  1932 					$post_type = 'any';
  2188 			foreach( (array) $q['search_terms'] as $term ) {
  1933 					$q['post_status'] = 'publish';
  2189 				$term = esc_sql( like_escape( $term ) );
  1934 					$post_status_join = true;
  2190 				$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  1935 				} else {
  2191 				$searchand = ' AND ';
  1936 					$whichcat = " AND 0 ";
  2192 			}
       
  2193 
       
  2194 			if ( !empty($search) ) {
       
  2195 				$search = " AND ({$search}) ";
       
  2196 				if ( !is_user_logged_in() )
       
  2197 					$search .= " AND ($wpdb->posts.post_password = '') ";
       
  2198 			}
       
  2199 		}
       
  2200 
       
  2201 		// Allow plugins to contextually add/remove/modify the search section of the database query
       
  2202 		$search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
       
  2203 
       
  2204 		// Taxonomies
       
  2205 		if ( !$this->is_singular ) {
       
  2206 			$this->parse_tax_query( $q );
       
  2207 
       
  2208 			$clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
       
  2209 
       
  2210 			$join .= $clauses['join'];
       
  2211 			$where .= $clauses['where'];
       
  2212 		}
       
  2213 
       
  2214 		if ( $this->is_tax ) {
       
  2215 			if ( empty($post_type) ) {
       
  2216 				$post_type = 'any';
       
  2217 				$post_status_join = true;
       
  2218 			} elseif ( in_array('attachment', (array) $post_type) ) {
       
  2219 				$post_status_join = true;
       
  2220 			}
       
  2221 		}
       
  2222 
       
  2223 		// Back-compat
       
  2224 		if ( !empty($this->tax_query->queries) ) {
       
  2225 			$tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
       
  2226 			if ( !empty( $tax_query_in_and ) ) {
       
  2227 				if ( !isset( $q['taxonomy'] ) ) {
       
  2228 					foreach ( $tax_query_in_and as $a_tax_query ) {
       
  2229 						if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
       
  2230 							$q['taxonomy'] = $a_tax_query['taxonomy'];
       
  2231 							if ( 'slug' == $a_tax_query['field'] )
       
  2232 								$q['term'] = $a_tax_query['terms'][0];
       
  2233 							else
       
  2234 								$q['term_id'] = $a_tax_query['terms'][0];
       
  2235 
       
  2236 							break;
       
  2237 						}
       
  2238 					}
  1937 				}
  2239 				}
  1938 			}
  2240 
       
  2241 				$cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
       
  2242 				if ( !empty( $cat_query ) ) {
       
  2243 					$cat_query = reset( $cat_query );
       
  2244 					$the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
       
  2245 					if ( $the_cat ) {
       
  2246 						$this->set( 'cat', $the_cat->term_id );
       
  2247 						$this->set( 'category_name', $the_cat->slug );
       
  2248 					}
       
  2249 					unset( $the_cat );
       
  2250 				}
       
  2251 				unset( $cat_query );
       
  2252 
       
  2253 				$tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
       
  2254 				if ( !empty( $tag_query ) ) {
       
  2255 					$tag_query = reset( $tag_query );
       
  2256 					$the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
       
  2257 					if ( $the_tag ) {
       
  2258 						$this->set( 'tag_id', $the_tag->term_id );
       
  2259 					}
       
  2260 					unset( $the_tag );
       
  2261 				}
       
  2262 				unset( $tag_query );
       
  2263 			}
       
  2264 		}
       
  2265 
       
  2266 		if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
       
  2267 			$groupby = "{$wpdb->posts}.ID";
  1939 		}
  2268 		}
  1940 
  2269 
  1941 		// Author/user stuff
  2270 		// Author/user stuff
  1942 
  2271 
  1943 		if ( empty($q['author']) || ($q['author'] == '0') ) {
  2272 		if ( empty($q['author']) || ($q['author'] == '0') ) {
  1944 			$whichauthor='';
  2273 			$whichauthor = '';
  1945 		} else {
  2274 		} else {
  1946 			$q['author'] = ''.urldecode($q['author']).'';
  2275 			$q['author'] = (string)urldecode($q['author']);
  1947 			$q['author'] = addslashes_gpc($q['author']);
  2276 			$q['author'] = addslashes_gpc($q['author']);
  1948 			if (strpos($q['author'], '-') !== false) {
  2277 			if ( strpos($q['author'], '-') !== false ) {
  1949 				$eq = '!=';
  2278 				$eq = '!=';
  1950 				$andor = 'AND';
  2279 				$andor = 'AND';
  1951 				$q['author'] = explode('-', $q['author']);
  2280 				$q['author'] = explode('-', $q['author']);
  1952 				$q['author'] = '' . absint($q['author'][1]);
  2281 				$q['author'] = (string)absint($q['author'][1]);
  1953 			} else {
  2282 			} else {
  1954 				$eq = '=';
  2283 				$eq = '=';
  1955 				$andor = 'OR';
  2284 				$andor = 'OR';
  1956 			}
  2285 			}
  1957 			$author_array = preg_split('/[,\s]+/', $q['author']);
  2286 			$author_array = preg_split('/[,\s]+/', $q['author']);
  1958 			$whichauthor .= " AND ($wpdb->posts.post_author ".$eq.' '.absint($author_array[0]);
  2287 			$_author_array = array();
  1959 			for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
  2288 			foreach ( $author_array as $key => $_author )
  1960 				$whichauthor .= ' '.$andor." $wpdb->posts.post_author ".$eq.' '.absint($author_array[$i]);
  2289 				$_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
  1961 			}
  2290 			$whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
  1962 			$whichauthor .= ')';
  2291 			unset($author_array, $_author_array);
  1963 		}
  2292 		}
  1964 
  2293 
  1965 		// Author stuff for nice URLs
  2294 		// Author stuff for nice URLs
  1966 
  2295 
  1967 		if ('' != $q['author_name']) {
  2296 		if ( '' != $q['author_name'] ) {
  1968 			if (strpos($q['author_name'], '/') !== false) {
  2297 			if ( strpos($q['author_name'], '/') !== false ) {
  1969 				$q['author_name'] = explode('/',$q['author_name']);
  2298 				$q['author_name'] = explode('/', $q['author_name']);
  1970 				if ($q['author_name'][count($q['author_name'])-1]) {
  2299 				if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  1971 					$q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
  2300 					$q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  1972 				} else {
  2301 				} else {
  1973 					$q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
  2302 					$q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
  1974 				}
  2303 				}
  1975 			}
  2304 			}
  1976 			$q['author_name'] = sanitize_title($q['author_name']);
  2305 			$q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  1977 			$q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
       
  1978 			$q['author'] = get_user_by('slug', $q['author_name']);
  2306 			$q['author'] = get_user_by('slug', $q['author_name']);
  1979 			if ( $q['author'] )
  2307 			if ( $q['author'] )
  1980 				$q['author'] = $q['author']->ID;
  2308 				$q['author'] = $q['author']->ID;
  1981 			$whichauthor .= " AND ($wpdb->posts.post_author = ".absint($q['author']).')';
  2309 			$whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  1982 		}
  2310 		}
  1983 
  2311 
  1984 		// MIME-Type stuff for attachment browsing
  2312 		// MIME-Type stuff for attachment browsing
  1985 
  2313 
  1986 		if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] )
  2314 		if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
  1987 			$whichmimetype = wp_post_mime_type_where($q['post_mime_type']);
  2315 			$whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
  1988 
  2316 
  1989 		$where .= $search.$whichcat.$whichauthor.$whichmimetype;
  2317 		$where .= $search . $whichauthor . $whichmimetype;
  1990 
  2318 
  1991 		if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  2319 		if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  1992 			$q['order'] = 'DESC';
  2320 			$q['order'] = 'DESC';
  1993 
  2321 
  1994 		// Order by
  2322 		// Order by
  1995 		if ( empty($q['orderby']) ) {
  2323 		if ( empty($q['orderby']) ) {
  1996 			$q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
  2324 			$orderby = "$wpdb->posts.post_date " . $q['order'];
  1997 		} elseif ( 'none' == $q['orderby'] ) {
  2325 		} elseif ( 'none' == $q['orderby'] ) {
  1998 			$q['orderby'] = '';
  2326 			$orderby = '';
  1999 		} else {
  2327 		} else {
  2000 			// Used to filter values
  2328 			// Used to filter values
  2001 			$allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2329 			$allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2002 			if ( !empty($q['meta_key']) ) {
  2330 			if ( !empty($q['meta_key']) ) {
  2003 				$allowed_keys[] = $q['meta_key'];
  2331 				$allowed_keys[] = $q['meta_key'];
  2004 				$allowed_keys[] = 'meta_value';
  2332 				$allowed_keys[] = 'meta_value';
       
  2333 				$allowed_keys[] = 'meta_value_num';
  2005 			}
  2334 			}
  2006 			$q['orderby'] = urldecode($q['orderby']);
  2335 			$q['orderby'] = urldecode($q['orderby']);
  2007 			$q['orderby'] = addslashes_gpc($q['orderby']);
  2336 			$q['orderby'] = addslashes_gpc($q['orderby']);
  2008 			$orderby_array = explode(' ',$q['orderby']);
  2337 
  2009 			if ( empty($orderby_array) )
  2338 			$orderby_array = array();
  2010 				$orderby_array[] = $q['orderby'];
  2339 			foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  2011 			$q['orderby'] = '';
       
  2012 			for ($i = 0; $i < count($orderby_array); $i++) {
       
  2013 				// Only allow certain values for safety
  2340 				// Only allow certain values for safety
  2014 				$orderby = $orderby_array[$i];
  2341 				if ( ! in_array($orderby, $allowed_keys) )
  2015 				switch ($orderby) {
  2342 					continue;
       
  2343 
       
  2344 				switch ( $orderby ) {
  2016 					case 'menu_order':
  2345 					case 'menu_order':
       
  2346 						$orderby = "$wpdb->posts.menu_order";
  2017 						break;
  2347 						break;
  2018 					case 'ID':
  2348 					case 'ID':
  2019 						$orderby = "$wpdb->posts.ID";
  2349 						$orderby = "$wpdb->posts.ID";
  2020 						break;
  2350 						break;
  2021 					case 'rand':
  2351 					case 'rand':
  2023 						break;
  2353 						break;
  2024 					case $q['meta_key']:
  2354 					case $q['meta_key']:
  2025 					case 'meta_value':
  2355 					case 'meta_value':
  2026 						$orderby = "$wpdb->postmeta.meta_value";
  2356 						$orderby = "$wpdb->postmeta.meta_value";
  2027 						break;
  2357 						break;
       
  2358 					case 'meta_value_num':
       
  2359 						$orderby = "$wpdb->postmeta.meta_value+0";
       
  2360 						break;
  2028 					case 'comment_count':
  2361 					case 'comment_count':
  2029 						$orderby = "$wpdb->posts.comment_count";
  2362 						$orderby = "$wpdb->posts.comment_count";
  2030 						break;
  2363 						break;
  2031 					default:
  2364 					default:
  2032 						$orderby = "$wpdb->posts.post_" . $orderby;
  2365 						$orderby = "$wpdb->posts.post_" . $orderby;
  2033 				}
  2366 				}
  2034 				if ( in_array($orderby_array[$i], $allowed_keys) )
  2367 
  2035 					$q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
  2368 				$orderby_array[] = $orderby;
  2036 			}
  2369 			}
  2037 			// append ASC or DESC at the end
  2370 			$orderby = implode( ',', $orderby_array );
  2038 			if ( !empty($q['orderby']))
  2371 
  2039 				$q['orderby'] .= " {$q['order']}";
  2372 			if ( empty( $orderby ) )
  2040 
  2373 				$orderby = "$wpdb->posts.post_date ".$q['order'];
  2041 			if ( empty($q['orderby']) )
  2374 			else
  2042 				$q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
  2375 				$orderby .= " {$q['order']}";
  2043 		}
  2376 		}
  2044 
  2377 
  2045 		if ( is_array($post_type) )
  2378 		if ( is_array( $post_type ) ) {
  2046 			$post_type_cap = 'multiple_post_type';
  2379 			$post_type_cap = 'multiple_post_type';
  2047 		else
  2380 		} else {
  2048 			$post_type_cap = $post_type;
  2381 			$post_type_object = get_post_type_object( $post_type );
  2049 
  2382 			if ( empty( $post_type_object ) )
  2050 		$exclude_post_types = '';
  2383 				$post_type_cap = $post_type;
  2051 		foreach ( get_post_types( array('exclude_from_search' => true) ) as $_wp_post_type )
  2384 		}
  2052 			$exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type != %s", $_wp_post_type);
       
  2053 
  2385 
  2054 		if ( 'any' == $post_type ) {
  2386 		if ( 'any' == $post_type ) {
  2055 			$where .= $exclude_post_types;
  2387 			$in_search_post_types = get_post_types( array('exclude_from_search' => false) );
       
  2388 			if ( ! empty( $in_search_post_types ) )
       
  2389 				$where .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
  2056 		} elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2390 		} elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2057 			$where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')"; 
  2391 			$where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2058 		} elseif ( ! empty( $post_type ) ) {
  2392 		} elseif ( ! empty( $post_type ) ) {
  2059 			$where .= " AND $wpdb->posts.post_type = '$post_type'";
  2393 			$where .= " AND $wpdb->posts.post_type = '$post_type'";
       
  2394 			$post_type_object = get_post_type_object ( $post_type );
  2060 		} elseif ( $this->is_attachment ) {
  2395 		} elseif ( $this->is_attachment ) {
  2061 			$where .= " AND $wpdb->posts.post_type = 'attachment'";
  2396 			$where .= " AND $wpdb->posts.post_type = 'attachment'";
  2062 			$post_type_cap = 'post';
  2397 			$post_type_object = get_post_type_object ( 'attachment' );
  2063 		} elseif ($this->is_page) {
  2398 		} elseif ( $this->is_page ) {
  2064 			$where .= " AND $wpdb->posts.post_type = 'page'";
  2399 			$where .= " AND $wpdb->posts.post_type = 'page'";
  2065 			$post_type_cap = 'page';
  2400 			$post_type_object = get_post_type_object ( 'page' );
  2066 		} else {
  2401 		} else {
  2067 			$where .= " AND $wpdb->posts.post_type = 'post'";
  2402 			$where .= " AND $wpdb->posts.post_type = 'post'";
  2068 			$post_type_cap = 'post';
  2403 			$post_type_object = get_post_type_object ( 'post' );
  2069 		}
  2404 		}
  2070 
  2405 
  2071 		if ( isset($q['post_status']) && '' != $q['post_status'] ) {
  2406 		if ( ! empty( $post_type_object ) ) {
       
  2407 			$edit_cap = $post_type_object->cap->edit_post;
       
  2408 			$read_cap = $post_type_object->cap->read_post;
       
  2409 			$edit_others_cap = $post_type_object->cap->edit_others_posts;
       
  2410 			$read_private_cap = $post_type_object->cap->read_private_posts;
       
  2411 		} else {
       
  2412 			$edit_cap = 'edit_' . $post_type_cap;
       
  2413 			$read_cap = 'read_' . $post_type_cap;
       
  2414 			$edit_others_cap = 'edit_others_' . $post_type_cap . 's';
       
  2415 			$read_private_cap = 'read_private_' . $post_type_cap . 's';
       
  2416 		}
       
  2417 
       
  2418 		if ( ! empty( $q['post_status'] ) ) {
  2072 			$statuswheres = array();
  2419 			$statuswheres = array();
  2073 			$q_status = explode(',', $q['post_status']);
  2420 			$q_status = $q['post_status'];
       
  2421 			if ( ! is_array( $q_status ) )
       
  2422 				$q_status = explode(',', $q_status);
  2074 			$r_status = array();
  2423 			$r_status = array();
  2075 			$p_status = array();
  2424 			$p_status = array();
  2076 			if ( $q['post_status'] == 'any' ) {
  2425 			$e_status = array();
  2077 				// @todo Use register_post_status() data to determine which states should be excluded.
  2426 			if ( in_array('any', $q_status) ) {
  2078 				$r_status[] = "$wpdb->posts.post_status <> 'trash'";
  2427 				foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
       
  2428 					$e_status[] = "$wpdb->posts.post_status <> '$status'";
  2079 			} else {
  2429 			} else {
  2080 				if ( in_array( 'draft'  , $q_status ) )
  2430 				foreach ( get_post_stati() as $status ) {
  2081 					$r_status[] = "$wpdb->posts.post_status = 'draft'";
  2431 					if ( in_array( $status, $q_status ) ) {
  2082 				if ( in_array( 'pending', $q_status ) )
  2432 						if ( 'private' == $status )
  2083 					$r_status[] = "$wpdb->posts.post_status = 'pending'";
  2433 							$p_status[] = "$wpdb->posts.post_status = '$status'";
  2084 				if ( in_array( 'future' , $q_status ) )
  2434 						else
  2085 					$r_status[] = "$wpdb->posts.post_status = 'future'";
  2435 							$r_status[] = "$wpdb->posts.post_status = '$status'";
  2086 				if ( in_array( 'inherit' , $q_status ) )
  2436 					}
  2087 					$r_status[] = "$wpdb->posts.post_status = 'inherit'";
  2437 				}
  2088 				if ( in_array( 'private', $q_status ) )
       
  2089 					$p_status[] = "$wpdb->posts.post_status = 'private'";
       
  2090 				if ( in_array( 'publish', $q_status ) )
       
  2091 					$r_status[] = "$wpdb->posts.post_status = 'publish'";
       
  2092 				if ( in_array( 'trash', $q_status ) )
       
  2093 					$r_status[] = "$wpdb->posts.post_status = 'trash'";
       
  2094 			}
  2438 			}
  2095 
  2439 
  2096 			if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2440 			if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2097 				$r_status = array_merge($r_status, $p_status);
  2441 				$r_status = array_merge($r_status, $p_status);
  2098 				unset($p_status);
  2442 				unset($p_status);
  2099 			}
  2443 			}
  2100 
  2444 
       
  2445 			if ( !empty($e_status) ) {
       
  2446 				$statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
       
  2447 			}
  2101 			if ( !empty($r_status) ) {
  2448 			if ( !empty($r_status) ) {
  2102 				if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type_cap}s") )
  2449 				if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2103 					$statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $r_status ) . "))";
  2450 					$statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
  2104 				else
  2451 				else
  2105 					$statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2452 					$statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2106 			}
  2453 			}
  2107 			if ( !empty($p_status) ) {
  2454 			if ( !empty($p_status) ) {
  2108 				if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type_cap}s") )
  2455 				if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2109 					$statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $p_status ) . "))";
  2456 					$statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
  2110 				else
  2457 				else
  2111 					$statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2458 					$statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2112 			}
  2459 			}
  2113 			if ( $post_status_join ) {
  2460 			if ( $post_status_join ) {
  2114 				$join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2461 				$join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2118 			foreach ( $statuswheres as $statuswhere )
  2465 			foreach ( $statuswheres as $statuswhere )
  2119 				$where .= " AND $statuswhere";
  2466 				$where .= " AND $statuswhere";
  2120 		} elseif ( !$this->is_singular ) {
  2467 		} elseif ( !$this->is_singular ) {
  2121 			$where .= " AND ($wpdb->posts.post_status = 'publish'";
  2468 			$where .= " AND ($wpdb->posts.post_status = 'publish'";
  2122 
  2469 
  2123 			if ( is_admin() )
  2470 			// Add public states.
  2124 				$where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'";
  2471 			$public_states = get_post_stati( array('public' => true) );
       
  2472 			foreach ( (array) $public_states as $state ) {
       
  2473 				if ( 'publish' == $state ) // Publish is hard-coded above.
       
  2474 					continue;
       
  2475 				$where .= " OR $wpdb->posts.post_status = '$state'";
       
  2476 			}
       
  2477 
       
  2478 			if ( $this->is_admin ) {
       
  2479 				// Add protected states that should show in the admin all list.
       
  2480 				$admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
       
  2481 				foreach ( (array) $admin_all_states as $state )
       
  2482 					$where .= " OR $wpdb->posts.post_status = '$state'";
       
  2483 			}
  2125 
  2484 
  2126 			if ( is_user_logged_in() ) {
  2485 			if ( is_user_logged_in() ) {
  2127 				$where .= current_user_can( "read_private_{$post_type_cap}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'";
  2486 				// Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
       
  2487 				$private_states = get_post_stati( array('private' => true) );
       
  2488 				foreach ( (array) $private_states as $state )
       
  2489 					$where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = '$state'";
  2128 			}
  2490 			}
  2129 
  2491 
  2130 			$where .= ')';
  2492 			$where .= ')';
  2131 		}
  2493 		}
  2132 
  2494 
  2133 		// postmeta queries
  2495 		if ( !empty( $this->meta_query->queries ) ) {
  2134 		if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
  2496 			$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  2135 			$join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
  2497 			$join .= $clauses['join'];
  2136 		if ( ! empty($q['meta_key']) )
  2498 			$where .= $clauses['where'];
  2137 			$where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
       
  2138 		if ( ! empty($q['meta_value']) ) {
       
  2139 			if ( ! isset($q['meta_compare']) || empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
       
  2140 				$q['meta_compare'] = '=';
       
  2141 
       
  2142 			$where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
       
  2143 		}
  2499 		}
  2144 
  2500 
  2145 		// Apply filters on where and join prior to paging so that any
  2501 		// Apply filters on where and join prior to paging so that any
  2146 		// manipulations to them are reflected in the paging by day queries.
  2502 		// manipulations to them are reflected in the paging by day queries.
  2147 		if ( !$q['suppress_filters'] ) {
  2503 		if ( !$q['suppress_filters'] ) {
  2148 			$where = apply_filters('posts_where', $where);
  2504 			$where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
  2149 			$join = apply_filters('posts_join', $join);
  2505 			$join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
  2150 		}
  2506 		}
  2151 
  2507 
  2152 		// Paging
  2508 		// Paging
  2153 		if ( empty($q['nopaging']) && !$this->is_singular ) {
  2509 		if ( empty($q['nopaging']) && !$this->is_singular ) {
  2154 			$page = absint($q['paged']);
  2510 			$page = absint($q['paged']);
  2155 			if (empty($page)) {
  2511 			if ( !$page )
  2156 				$page = 1;
  2512 				$page = 1;
  2157 			}
       
  2158 
  2513 
  2159 			if ( empty($q['offset']) ) {
  2514 			if ( empty($q['offset']) ) {
  2160 				$pgstrt = '';
       
  2161 				$pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2515 				$pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2162 				$limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
       
  2163 			} else { // we're ignoring $page and using 'offset'
  2516 			} else { // we're ignoring $page and using 'offset'
  2164 				$q['offset'] = absint($q['offset']);
  2517 				$q['offset'] = absint($q['offset']);
  2165 				$pgstrt = $q['offset'] . ', ';
  2518 				$pgstrt = $q['offset'] . ', ';
  2166 				$limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2519 			}
  2167 			}
  2520 			$limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2168 		}
  2521 		}
  2169 
  2522 
  2170 		// Comments feeds
  2523 		// Comments feeds
  2171 		if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2524 		if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2172 			if ( $this->is_archive || $this->is_search ) {
  2525 			if ( $this->is_archive || $this->is_search ) {
  2178 				$cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2531 				$cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2179 				$cgroupby = '';
  2532 				$cgroupby = '';
  2180 			}
  2533 			}
  2181 
  2534 
  2182 			if ( !$q['suppress_filters'] ) {
  2535 			if ( !$q['suppress_filters'] ) {
  2183 				$cjoin = apply_filters('comment_feed_join', $cjoin);
  2536 				$cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
  2184 				$cwhere = apply_filters('comment_feed_where', $cwhere);
  2537 				$cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
  2185 				$cgroupby = apply_filters('comment_feed_groupby', $cgroupby);
  2538 				$cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
  2186 				$corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC');
  2539 				$corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2187 				$climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss'));
  2540 				$climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2188 			}
  2541 			}
  2189 			$cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2542 			$cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2190 			$corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2543 			$corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2191 
  2544 
  2192 			$this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2545 			$this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2193 			$this->comment_count = count($this->comments);
  2546 			$this->comment_count = count($this->comments);
  2194 
  2547 
  2195 			$post_ids = array();
  2548 			$post_ids = array();
  2196 
  2549 
  2197 			foreach ($this->comments as $comment)
  2550 			foreach ( $this->comments as $comment )
  2198 				$post_ids[] = (int) $comment->comment_post_ID;
  2551 				$post_ids[] = (int) $comment->comment_post_ID;
  2199 
  2552 
  2200 			$post_ids = join(',', $post_ids);
  2553 			$post_ids = join(',', $post_ids);
  2201 			$join = '';
  2554 			$join = '';
  2202 			if ( $post_ids )
  2555 			if ( $post_ids )
  2203 				$where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2556 				$where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2204 			else
  2557 			else
  2205 				$where = "AND 0";
  2558 				$where = "AND 0";
  2206 		}
  2559 		}
  2207 
  2560 
  2208 		$orderby = $q['orderby'];
  2561 		$pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2209 
  2562 
  2210 		// Apply post-paging filters on where and join.  Only plugins that
  2563 		// Apply post-paging filters on where and join. Only plugins that
  2211 		// manipulate paging queries should use these hooks.
  2564 		// manipulate paging queries should use these hooks.
  2212 		if ( !$q['suppress_filters'] ) {
  2565 		if ( !$q['suppress_filters'] ) {
  2213 			$where = apply_filters('posts_where_paged', $where);
  2566 			$where		= apply_filters_ref_array( 'posts_where_paged',	array( $where, &$this ) );
  2214 			$groupby = apply_filters('posts_groupby', $groupby);
  2567 			$groupby	= apply_filters_ref_array( 'posts_groupby',		array( $groupby, &$this ) );
  2215 			$join = apply_filters('posts_join_paged', $join);
  2568 			$join		= apply_filters_ref_array( 'posts_join_paged',	array( $join, &$this ) );
  2216 			$orderby = apply_filters('posts_orderby', $orderby);
  2569 			$orderby	= apply_filters_ref_array( 'posts_orderby',		array( $orderby, &$this ) );
  2217 			$distinct = apply_filters('posts_distinct', $distinct);
  2570 			$distinct	= apply_filters_ref_array( 'posts_distinct',	array( $distinct, &$this ) );
  2218 			$limits = apply_filters( 'post_limits', $limits );
  2571 			$limits		= apply_filters_ref_array( 'post_limits',		array( $limits, &$this ) );
  2219 
  2572 			$fields		= apply_filters_ref_array( 'posts_fields',		array( $fields, &$this ) );
  2220 			$fields = apply_filters('posts_fields', $fields);
  2573 
  2221 		}
  2574 			// Filter all clauses at once, for convenience
  2222 
  2575 			$clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2223 		// Announce current selection parameters.  For use by caching plugins.
  2576 			foreach ( $pieces as $piece )
       
  2577 				$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
       
  2578 		}
       
  2579 
       
  2580 		// Announce current selection parameters. For use by caching plugins.
  2224 		do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2581 		do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2225 
  2582 
  2226 		// Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
  2583 		// Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
  2227 		if ( !$q['suppress_filters'] ) {
  2584 		if ( !$q['suppress_filters'] ) {
  2228 			$where = apply_filters('posts_where_request', $where);
  2585 			$where		= apply_filters_ref_array( 'posts_where_request',		array( $where, &$this ) );
  2229 			$groupby = apply_filters('posts_groupby_request', $groupby);
  2586 			$groupby	= apply_filters_ref_array( 'posts_groupby_request',		array( $groupby, &$this ) );
  2230 			$join = apply_filters('posts_join_request', $join);
  2587 			$join		= apply_filters_ref_array( 'posts_join_request',		array( $join, &$this ) );
  2231 			$orderby = apply_filters('posts_orderby_request', $orderby);
  2588 			$orderby	= apply_filters_ref_array( 'posts_orderby_request',		array( $orderby, &$this ) );
  2232 			$distinct = apply_filters('posts_distinct_request', $distinct);
  2589 			$distinct	= apply_filters_ref_array( 'posts_distinct_request',	array( $distinct, &$this ) );
  2233 			$fields = apply_filters('posts_fields_request', $fields);
  2590 			$fields		= apply_filters_ref_array( 'posts_fields_request',		array( $fields, &$this ) );
  2234 			$limits = apply_filters( 'post_limits_request', $limits );
  2591 			$limits		= apply_filters_ref_array( 'post_limits_request',		array( $limits, &$this ) );
       
  2592 
       
  2593 			// Filter all clauses at once, for convenience
       
  2594 			$clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
       
  2595 			foreach ( $pieces as $piece )
       
  2596 				$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2235 		}
  2597 		}
  2236 
  2598 
  2237 		if ( ! empty($groupby) )
  2599 		if ( ! empty($groupby) )
  2238 			$groupby = 'GROUP BY ' . $groupby;
  2600 			$groupby = 'GROUP BY ' . $groupby;
  2239 		if ( !empty( $orderby ) )
  2601 		if ( !empty( $orderby ) )
  2240 			$orderby = 'ORDER BY ' . $orderby;
  2602 			$orderby = 'ORDER BY ' . $orderby;
       
  2603 
  2241 		$found_rows = '';
  2604 		$found_rows = '';
  2242 		if ( !empty($limits) )
  2605 		if ( !$q['no_found_rows'] && !empty($limits) )
  2243 			$found_rows = 'SQL_CALC_FOUND_ROWS';
  2606 			$found_rows = 'SQL_CALC_FOUND_ROWS';
  2244 
  2607 
  2245 		$this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2608 		$this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
       
  2609 
       
  2610 		if ( !$q['suppress_filters'] ) {
       
  2611 			$this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
       
  2612 		}
       
  2613 
       
  2614 		if ( 'ids' == $q['fields'] ) {
       
  2615 			$this->posts = $wpdb->get_col($this->request);
       
  2616 
       
  2617 			return $this->posts;
       
  2618 		}
       
  2619 
       
  2620 		if ( 'id=>parent' == $q['fields'] ) {
       
  2621 			$this->posts = $wpdb->get_results($this->request);
       
  2622 
       
  2623 			$r = array();
       
  2624 			foreach ( $this->posts as $post )
       
  2625 				$r[ $post->ID ] = $post->post_parent;
       
  2626 
       
  2627 			return $r;
       
  2628 		}
       
  2629 
       
  2630 		$split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
       
  2631 		$split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
       
  2632 
       
  2633 		if ( $split_the_query ) {
       
  2634 			// First get the IDs and then fill in the objects
       
  2635 
       
  2636 			$this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
       
  2637 
       
  2638 			$this->request = apply_filters( 'posts_request_ids', $this->request, $this );
       
  2639 
       
  2640 			$ids = $wpdb->get_col( $this->request );
       
  2641 
       
  2642 			if ( $ids ) {
       
  2643 				$this->set_found_posts( $q, $limits );
       
  2644 
       
  2645 				_prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
       
  2646 
       
  2647 				$this->posts = array_map( 'get_post', $ids );
       
  2648 			} else {
       
  2649 				$this->found_posts = $this->max_num_pages = 0;
       
  2650 				$this->posts = array();
       
  2651 			}
       
  2652 		} else {
       
  2653 			$this->posts = $wpdb->get_results( $this->request );
       
  2654 			$this->set_found_posts( $q, $limits );
       
  2655 		}
       
  2656 
       
  2657 		// Raw results filter. Prior to status checks.
  2246 		if ( !$q['suppress_filters'] )
  2658 		if ( !$q['suppress_filters'] )
  2247 			$this->request = apply_filters('posts_request', $this->request);
  2659 			$this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  2248 
       
  2249 		$this->posts = $wpdb->get_results($this->request);
       
  2250 		// Raw results filter.  Prior to status checks.
       
  2251 		if ( !$q['suppress_filters'] )
       
  2252 			$this->posts = apply_filters('posts_results', $this->posts);
       
  2253 
  2660 
  2254 		if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2661 		if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2255 			$cjoin = apply_filters('comment_feed_join', '');
  2662 			$cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
  2256 			$cwhere = apply_filters('comment_feed_where', "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'");
  2663 			$cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2257 			$cgroupby = apply_filters('comment_feed_groupby', '');
  2664 			$cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
  2258 			$cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2665 			$cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2259 			$corderby = apply_filters('comment_feed_orderby', 'comment_date_gmt DESC');
  2666 			$corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2260 			$corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2667 			$corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2261 			$climits = apply_filters('comment_feed_limits', 'LIMIT ' . get_option('posts_per_rss'));
  2668 			$climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2262 			$comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2669 			$comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2263 			$this->comments = $wpdb->get_results($comments_request);
  2670 			$this->comments = $wpdb->get_results($comments_request);
  2264 			$this->comment_count = count($this->comments);
  2671 			$this->comment_count = count($this->comments);
  2265 		}
  2672 		}
  2266 
  2673 
  2267 		if ( !empty($limits) ) {
       
  2268 			$found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
       
  2269 			$this->found_posts = $wpdb->get_var( $found_posts_query );
       
  2270 			$this->found_posts = apply_filters( 'found_posts', $this->found_posts );
       
  2271 			$this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
       
  2272 		}
       
  2273 
       
  2274 		// Check post status to determine if post should be displayed.
  2674 		// Check post status to determine if post should be displayed.
  2275 		if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2675 		if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2276 			$status = get_post_status($this->posts[0]);
  2676 			$status = get_post_status($this->posts[0]);
       
  2677 			$post_status_obj = get_post_status_object($status);
  2277 			//$type = get_post_type($this->posts[0]);
  2678 			//$type = get_post_type($this->posts[0]);
  2278 			if ( ('publish' != $status) ) {
  2679 			if ( !$post_status_obj->public ) {
  2279 				if ( ! is_user_logged_in() ) {
  2680 				if ( ! is_user_logged_in() ) {
  2280 					// User must be logged in to view unpublished posts.
  2681 					// User must be logged in to view unpublished posts.
  2281 					$this->posts = array();
  2682 					$this->posts = array();
  2282 				} else {
  2683 				} else {
  2283 					if  (in_array($status, array('draft', 'pending')) ) {
  2684 					if  ( $post_status_obj->protected ) {
  2284 						// User must have edit permissions on the draft to preview.
  2685 						// User must have edit permissions on the draft to preview.
  2285 						if (! current_user_can("edit_$post_type_cap", $this->posts[0]->ID)) {
  2686 						if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2286 							$this->posts = array();
  2687 							$this->posts = array();
  2287 						} else {
  2688 						} else {
  2288 							$this->is_preview = true;
  2689 							$this->is_preview = true;
  2289 							$this->posts[0]->post_date = current_time('mysql');
  2690 							if ( 'future' != $status )
       
  2691 								$this->posts[0]->post_date = current_time('mysql');
  2290 						}
  2692 						}
  2291 					}  else if ('future' == $status) {
  2693 					} elseif ( $post_status_obj->private ) {
  2292 						$this->is_preview = true;
  2694 						if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2293 						if (!current_user_can("edit_$post_type_cap", $this->posts[0]->ID)) {
  2695 							$this->posts = array();
  2294 							$this->posts = array ( );
       
  2295 						}
       
  2296 					} else {
  2696 					} else {
  2297 						if (! current_user_can("read_$post_type_cap", $this->posts[0]->ID))
  2697 						$this->posts = array();
  2298 							$this->posts = array();
       
  2299 					}
  2698 					}
  2300 				}
  2699 				}
  2301 			}
  2700 			}
  2302 
  2701 
  2303 			if ( $this->is_preview && current_user_can( "edit_{$post_type_cap}", $this->posts[0]->ID ) )
  2702 			if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
  2304 				$this->posts[0] = apply_filters('the_preview', $this->posts[0]);
  2703 				$this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
  2305 		}
  2704 		}
  2306 
  2705 
  2307 		// Put sticky posts at the top of the posts array
  2706 		// Put sticky posts at the top of the posts array
  2308 		$sticky_posts = get_option('sticky_posts');
  2707 		$sticky_posts = get_option('sticky_posts');
  2309 		if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['caller_get_posts'] ) {
  2708 		if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  2310 			$num_posts = count($this->posts);
  2709 			$num_posts = count($this->posts);
  2311 			$sticky_offset = 0;
  2710 			$sticky_offset = 0;
  2312 			// Loop over posts and relocate stickies to the front.
  2711 			// Loop over posts and relocate stickies to the front.
  2313 			for ( $i = 0; $i < $num_posts; $i++ ) {
  2712 			for ( $i = 0; $i < $num_posts; $i++ ) {
  2314 				if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2713 				if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2315 					$sticky_post = $this->posts[$i];
  2714 					$sticky_post = $this->posts[$i];
  2316 					// Remove sticky from current position
  2715 					// Remove sticky from current position
  2317 					array_splice($this->posts, $i, 1);
  2716 					array_splice($this->posts, $i, 1);
  2318 					// Move to front, after other stickies
  2717 					// Move to front, after other stickies
  2319 					array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2718 					array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2320 					// Increment the sticky offset.  The next sticky will be placed at this offset.
  2719 					// Increment the sticky offset. The next sticky will be placed at this offset.
  2321 					$sticky_offset++;
  2720 					$sticky_offset++;
  2322 					// Remove post from sticky posts array
  2721 					// Remove post from sticky posts array
  2323 					$offset = array_search($sticky_post->ID, $sticky_posts);
  2722 					$offset = array_search($sticky_post->ID, $sticky_posts);
  2324 					array_splice($sticky_posts, $offset, 1);
  2723 					unset( $sticky_posts[$offset] );
  2325 				}
  2724 				}
  2326 			}
  2725 			}
       
  2726 
       
  2727 			// If any posts have been excluded specifically, Ignore those that are sticky.
       
  2728 			if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
       
  2729 				$sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2327 
  2730 
  2328 			// Fetch sticky posts that weren't in the query results
  2731 			// Fetch sticky posts that weren't in the query results
  2329 			if ( !empty($sticky_posts) ) {
  2732 			if ( !empty($sticky_posts) ) {
  2330 				$stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
  2733 				$stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
  2331 				// honor post type(s) if not set to any
  2734 				// honor post type(s) if not set to any
  2336 					} else {
  2739 					} else {
  2337 						$post_types = $post_type;
  2740 						$post_types = $post_type;
  2338 					}
  2741 					}
  2339 					$stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
  2742 					$stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
  2340 				}
  2743 				}
       
  2744 
  2341 				$stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
  2745 				$stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
  2342 				/** @todo Make sure post is published or viewable by the current user */
       
  2343 				foreach ( $stickies as $sticky_post ) {
  2746 				foreach ( $stickies as $sticky_post ) {
       
  2747 					// Ignore sticky posts the current user cannot read or are not published.
  2344 					if ( 'publish' != $sticky_post->post_status )
  2748 					if ( 'publish' != $sticky_post->post_status )
  2345 						continue;
  2749 						continue;
  2346 						array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2750 					array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2347 						$sticky_offset++;
  2751 					$sticky_offset++;
  2348 				}
  2752 				}
  2349 			}
  2753 			}
  2350 		}
  2754 		}
  2351 
  2755 
  2352 		if ( !$q['suppress_filters'] )
  2756 		if ( !$q['suppress_filters'] )
  2353 			$this->posts = apply_filters('the_posts', $this->posts);
  2757 			$this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
  2354 
  2758 
  2355 		$this->post_count = count($this->posts);
  2759 		$this->post_count = count($this->posts);
  2356 
  2760 
  2357 		// Sanitize before caching so it'll only get done once
  2761 		// Always sanitize
  2358 		for ($i = 0; $i < $this->post_count; $i++) {
  2762 		foreach ( $this->posts as $i => $post ) {
  2359 			$this->posts[$i] = sanitize_post($this->posts[$i], 'raw');
  2763 			$this->posts[$i] = sanitize_post( $post, 'raw' );
  2360 		}
  2764 		}
  2361 
  2765 
  2362 		update_post_caches($this->posts);
  2766 		if ( $q['cache_results'] )
  2363 
  2767 			update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2364 		if ($this->post_count > 0) {
  2768 
       
  2769 		if ( $this->post_count > 0 ) {
  2365 			$this->post = $this->posts[0];
  2770 			$this->post = $this->posts[0];
  2366 		}
  2771 		}
  2367 
  2772 
  2368 		return $this->posts;
  2773 		return $this->posts;
  2369 	}
  2774 	}
  2370 
  2775 
  2371 	/**
  2776 	function set_found_posts( $q, $limits ) {
  2372 	 * Setup the next post and iterate current post index.
  2777 		global $wpdb;
       
  2778 
       
  2779 		if ( $q['no_found_rows'] || empty( $limits ) )
       
  2780 			return;
       
  2781 
       
  2782 		$this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
       
  2783 		$this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
       
  2784 
       
  2785 		$this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
       
  2786 	}
       
  2787 
       
  2788 	/**
       
  2789 	 * Set up the next post and iterate current post index.
  2373 	 *
  2790 	 *
  2374 	 * @since 1.5.0
  2791 	 * @since 1.5.0
  2375 	 * @access public
  2792 	 * @access public
  2376 	 *
  2793 	 *
  2377 	 * @return object Next post.
  2794 	 * @return object Next post.
  2416 	 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  2833 	 * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  2417 	 *
  2834 	 *
  2418 	 * @return bool True if posts are available, false if end of loop.
  2835 	 * @return bool True if posts are available, false if end of loop.
  2419 	 */
  2836 	 */
  2420 	function have_posts() {
  2837 	function have_posts() {
  2421 		if ($this->current_post + 1 < $this->post_count) {
  2838 		if ( $this->current_post + 1 < $this->post_count ) {
  2422 			return true;
  2839 			return true;
  2423 		} elseif ($this->current_post + 1 == $this->post_count && $this->post_count > 0) {
  2840 		} elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  2424 			do_action_ref_array('loop_end', array(&$this));
  2841 			do_action_ref_array('loop_end', array(&$this));
  2425 			// Do some cleaning up after the loop
  2842 			// Do some cleaning up after the loop
  2426 			$this->rewind_posts();
  2843 			$this->rewind_posts();
  2427 		}
  2844 		}
  2428 
  2845 
  2436 	 * @since 1.5.0
  2853 	 * @since 1.5.0
  2437 	 * @access public
  2854 	 * @access public
  2438 	 */
  2855 	 */
  2439 	function rewind_posts() {
  2856 	function rewind_posts() {
  2440 		$this->current_post = -1;
  2857 		$this->current_post = -1;
  2441 		if ($this->post_count > 0) {
  2858 		if ( $this->post_count > 0 ) {
  2442 			$this->post = $this->posts[0];
  2859 			$this->post = $this->posts[0];
  2443 		}
  2860 		}
  2444 	}
  2861 	}
  2445 
  2862 
  2446 	/**
  2863 	/**
  2469 	function the_comment() {
  2886 	function the_comment() {
  2470 		global $comment;
  2887 		global $comment;
  2471 
  2888 
  2472 		$comment = $this->next_comment();
  2889 		$comment = $this->next_comment();
  2473 
  2890 
  2474 		if ($this->current_comment == 0) {
  2891 		if ( $this->current_comment == 0 ) {
  2475 			do_action('comment_loop_start');
  2892 			do_action('comment_loop_start');
  2476 		}
  2893 		}
  2477 	}
  2894 	}
  2478 
  2895 
  2479 	/**
  2896 	/**
  2485 	 * @access public
  2902 	 * @access public
  2486 	 *
  2903 	 *
  2487 	 * @return bool True, if more comments. False, if no more posts.
  2904 	 * @return bool True, if more comments. False, if no more posts.
  2488 	 */
  2905 	 */
  2489 	function have_comments() {
  2906 	function have_comments() {
  2490 		if ($this->current_comment + 1 < $this->comment_count) {
  2907 		if ( $this->current_comment + 1 < $this->comment_count ) {
  2491 			return true;
  2908 			return true;
  2492 		} elseif ($this->current_comment + 1 == $this->comment_count) {
  2909 		} elseif ( $this->current_comment + 1 == $this->comment_count ) {
  2493 			$this->rewind_comments();
  2910 			$this->rewind_comments();
  2494 		}
  2911 		}
  2495 
  2912 
  2496 		return false;
  2913 		return false;
  2497 	}
  2914 	}
  2502 	 * @since 2.2.0
  2919 	 * @since 2.2.0
  2503 	 * @access public
  2920 	 * @access public
  2504 	 */
  2921 	 */
  2505 	function rewind_comments() {
  2922 	function rewind_comments() {
  2506 		$this->current_comment = -1;
  2923 		$this->current_comment = -1;
  2507 		if ($this->comment_count > 0) {
  2924 		if ( $this->comment_count > 0 ) {
  2508 			$this->comment = $this->comments[0];
  2925 			$this->comment = $this->comments[0];
  2509 		}
  2926 		}
  2510 	}
  2927 	}
  2511 
  2928 
  2512 	/**
  2929 	/**
  2516 	 * @access public
  2933 	 * @access public
  2517 	 *
  2934 	 *
  2518 	 * @param string $query URL query string.
  2935 	 * @param string $query URL query string.
  2519 	 * @return array List of posts.
  2936 	 * @return array List of posts.
  2520 	 */
  2937 	 */
  2521 	function &query($query) {
  2938 	function &query( $query ) {
  2522 		$this->parse_query($query);
  2939 		$this->init();
       
  2940 		$this->query = $this->query_vars = wp_parse_args( $query );
  2523 		return $this->get_posts();
  2941 		return $this->get_posts();
  2524 	}
  2942 	}
  2525 
  2943 
  2526 	/**
  2944 	/**
  2527 	 * Retrieve queried object.
  2945 	 * Retrieve queried object.
  2534 	 * @access public
  2952 	 * @access public
  2535 	 *
  2953 	 *
  2536 	 * @return object
  2954 	 * @return object
  2537 	 */
  2955 	 */
  2538 	function get_queried_object() {
  2956 	function get_queried_object() {
  2539 		if (isset($this->queried_object)) {
  2957 		if ( isset($this->queried_object) )
  2540 			return $this->queried_object;
  2958 			return $this->queried_object;
  2541 		}
  2959 
  2542 
  2960 		$this->queried_object = null;
  2543 		$this->queried_object = NULL;
       
  2544 		$this->queried_object_id = 0;
  2961 		$this->queried_object_id = 0;
  2545 
  2962 
  2546 		if ($this->is_category) {
  2963 		if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  2547 			$cat = $this->get('cat');
  2964 			$tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  2548 			$category = &get_category($cat);
  2965 
  2549 			if ( is_wp_error( $category ) )
  2966 			$query = reset( $tax_query_in_and );
  2550 				return NULL;
  2967 
  2551 			$this->queried_object = &$category;
  2968 			if ( 'term_id' == $query['field'] )
  2552 			$this->queried_object_id = (int) $cat;
  2969 				$term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
  2553 		} else if ($this->is_tag) {
  2970 			else
  2554 			$tag_id = $this->get('tag_id');
  2971 				$term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
  2555 			$tag = &get_term($tag_id, 'post_tag');
  2972 
  2556 			if ( is_wp_error( $tag ) )
  2973 			if ( $term && ! is_wp_error($term) )  {
  2557 				return NULL;
  2974 				$this->queried_object = $term;
  2558 			$this->queried_object = &$tag;
  2975 				$this->queried_object_id = (int) $term->term_id;
  2559 			$this->queried_object_id = (int) $tag_id;
  2976 
  2560 		} else if ($this->is_tax) {
  2977 				if ( $this->is_category )
  2561 			$tax = $this->get('taxonomy');
  2978 					_make_cat_compat( $this->queried_object );
  2562 			$slug = $this->get('term');
  2979 			}
  2563 			$term = &get_terms($tax, array('slug'=>$slug));
  2980 		} elseif ( $this->is_post_type_archive ) {
  2564 			if ( is_wp_error($term) || empty($term) )
  2981 			$this->queried_object = get_post_type_object( $this->get('post_type') );
  2565 				return NULL;
  2982 		} elseif ( $this->is_posts_page ) {
  2566 			$term = $term[0];
  2983 			$page_for_posts = get_option('page_for_posts');
  2567 			$this->queried_object = $term;
  2984 			$this->queried_object = get_page( $page_for_posts );
  2568 			$this->queried_object_id = $term->term_id;
       
  2569 		} else if ($this->is_posts_page) {
       
  2570 			$this->queried_object = & get_page(get_option('page_for_posts'));
       
  2571 			$this->queried_object_id = (int) $this->queried_object->ID;
  2985 			$this->queried_object_id = (int) $this->queried_object->ID;
  2572 		} else if ($this->is_single) {
  2986 		} elseif ( $this->is_singular && !is_null($this->post) ) {
  2573 			$this->queried_object = $this->post;
  2987 			$this->queried_object = $this->post;
  2574 			$this->queried_object_id = (int) $this->post->ID;
  2988 			$this->queried_object_id = (int) $this->post->ID;
  2575 		} else if ($this->is_page) {
  2989 		} elseif ( $this->is_author ) {
  2576 			$this->queried_object = $this->post;
  2990 			$this->queried_object_id = (int) $this->get('author');
  2577 			$this->queried_object_id = (int) $this->post->ID;
  2991 			$this->queried_object = get_userdata( $this->queried_object_id );
  2578 		} else if ($this->is_author) {
       
  2579 			$author_id = (int) $this->get('author');
       
  2580 			$author = get_userdata($author_id);
       
  2581 			$this->queried_object = $author;
       
  2582 			$this->queried_object_id = $author_id;
       
  2583 		}
  2992 		}
  2584 
  2993 
  2585 		return $this->queried_object;
  2994 		return $this->queried_object;
  2586 	}
  2995 	}
  2587 
  2996 
  2594 	 * @return int
  3003 	 * @return int
  2595 	 */
  3004 	 */
  2596 	function get_queried_object_id() {
  3005 	function get_queried_object_id() {
  2597 		$this->get_queried_object();
  3006 		$this->get_queried_object();
  2598 
  3007 
  2599 		if (isset($this->queried_object_id)) {
  3008 		if ( isset($this->queried_object_id) ) {
  2600 			return $this->queried_object_id;
  3009 			return $this->queried_object_id;
  2601 		}
  3010 		}
  2602 
  3011 
  2603 		return 0;
  3012 		return 0;
  2604 	}
  3013 	}
  2605 
  3014 
  2606 	/**
  3015 	/**
  2607 	 * PHP4 type constructor.
  3016 	 * Constructor.
  2608 	 *
  3017 	 *
  2609 	 * Sets up the WordPress query, if parameter is not empty.
  3018 	 * Sets up the WordPress query, if parameter is not empty.
  2610 	 *
  3019 	 *
  2611 	 * @since 1.5.0
  3020 	 * @since 1.5.0
  2612 	 * @access public
  3021 	 * @access public
  2613 	 *
  3022 	 *
  2614 	 * @param string $query URL query string.
  3023 	 * @param string $query URL query string.
  2615 	 * @return WP_Query
  3024 	 * @return WP_Query
  2616 	 */
  3025 	 */
  2617 	function WP_Query ($query = '') {
  3026 	function __construct($query = '') {
  2618 		if (! empty($query)) {
  3027 		if ( ! empty($query) ) {
  2619 			$this->query($query);
  3028 			$this->query($query);
  2620 		}
  3029 		}
       
  3030 	}
       
  3031 
       
  3032 	/**
       
  3033  	 * Is the query for an archive page?
       
  3034  	 *
       
  3035  	 * Month, Year, Category, Author, Post Type archive...
       
  3036 	 *
       
  3037  	 * @since 3.1.0
       
  3038  	 *
       
  3039  	 * @return bool
       
  3040  	 */
       
  3041 	function is_archive() {
       
  3042 		return (bool) $this->is_archive;
       
  3043 	}
       
  3044 
       
  3045 	/**
       
  3046 	 * Is the query for a post type archive page?
       
  3047 	 *
       
  3048 	 * @since 3.1.0
       
  3049 	 *
       
  3050 	 * @param mixed $post_types Optional. Post type or array of posts types to check against.
       
  3051 	 * @return bool
       
  3052 	 */
       
  3053 	function is_post_type_archive( $post_types = '' ) {
       
  3054 		if ( empty( $post_types ) || !$this->is_post_type_archive )
       
  3055 			return (bool) $this->is_post_type_archive;
       
  3056 
       
  3057 		$post_type_object = $this->get_queried_object();
       
  3058 
       
  3059 		return in_array( $post_type_object->name, (array) $post_types );
       
  3060 	}
       
  3061 
       
  3062 	/**
       
  3063 	 * Is the query for an attachment page?
       
  3064 	 *
       
  3065 	 * @since 3.1.0
       
  3066 	 *
       
  3067 	 * @return bool
       
  3068 	 */
       
  3069 	function is_attachment() {
       
  3070 		return (bool) $this->is_attachment;
       
  3071 	}
       
  3072 
       
  3073 	/**
       
  3074 	 * Is the query for an author archive page?
       
  3075 	 *
       
  3076 	 * If the $author parameter is specified, this function will additionally
       
  3077 	 * check if the query is for one of the authors specified.
       
  3078 	 *
       
  3079 	 * @since 3.1.0
       
  3080 	 *
       
  3081 	 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
       
  3082 	 * @return bool
       
  3083 	 */
       
  3084 	function is_author( $author = '' ) {
       
  3085 		if ( !$this->is_author )
       
  3086 			return false;
       
  3087 
       
  3088 		if ( empty($author) )
       
  3089 			return true;
       
  3090 
       
  3091 		$author_obj = $this->get_queried_object();
       
  3092 
       
  3093 		$author = (array) $author;
       
  3094 
       
  3095 		if ( in_array( $author_obj->ID, $author ) )
       
  3096 			return true;
       
  3097 		elseif ( in_array( $author_obj->nickname, $author ) )
       
  3098 			return true;
       
  3099 		elseif ( in_array( $author_obj->user_nicename, $author ) )
       
  3100 			return true;
       
  3101 
       
  3102 		return false;
       
  3103 	}
       
  3104 
       
  3105 	/**
       
  3106 	 * Is the query for a category archive page?
       
  3107 	 *
       
  3108 	 * If the $category parameter is specified, this function will additionally
       
  3109 	 * check if the query is for one of the categories specified.
       
  3110 	 *
       
  3111 	 * @since 3.1.0
       
  3112 	 *
       
  3113 	 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
       
  3114 	 * @return bool
       
  3115 	 */
       
  3116 	function is_category( $category = '' ) {
       
  3117 		if ( !$this->is_category )
       
  3118 			return false;
       
  3119 
       
  3120 		if ( empty($category) )
       
  3121 			return true;
       
  3122 
       
  3123 		$cat_obj = $this->get_queried_object();
       
  3124 
       
  3125 		$category = (array) $category;
       
  3126 
       
  3127 		if ( in_array( $cat_obj->term_id, $category ) )
       
  3128 			return true;
       
  3129 		elseif ( in_array( $cat_obj->name, $category ) )
       
  3130 			return true;
       
  3131 		elseif ( in_array( $cat_obj->slug, $category ) )
       
  3132 			return true;
       
  3133 
       
  3134 		return false;
       
  3135 	}
       
  3136 
       
  3137 	/**
       
  3138 	 * Is the query for a tag archive page?
       
  3139 	 *
       
  3140 	 * If the $tag parameter is specified, this function will additionally
       
  3141 	 * check if the query is for one of the tags specified.
       
  3142 	 *
       
  3143 	 * @since 3.1.0
       
  3144 	 *
       
  3145 	 * @param mixed $slug Optional. Tag slug or array of slugs.
       
  3146 	 * @return bool
       
  3147 	 */
       
  3148 	function is_tag( $slug = '' ) {
       
  3149 		if ( !$this->is_tag )
       
  3150 			return false;
       
  3151 
       
  3152 		if ( empty( $slug ) )
       
  3153 			return true;
       
  3154 
       
  3155 		$tag_obj = $this->get_queried_object();
       
  3156 
       
  3157 		$slug = (array) $slug;
       
  3158 
       
  3159 		if ( in_array( $tag_obj->slug, $slug ) )
       
  3160 			return true;
       
  3161 
       
  3162 		return false;
       
  3163 	}
       
  3164 
       
  3165 	/**
       
  3166 	 * Is the query for a taxonomy archive page?
       
  3167 	 *
       
  3168 	 * If the $taxonomy parameter is specified, this function will additionally
       
  3169 	 * check if the query is for that specific $taxonomy.
       
  3170 	 *
       
  3171 	 * If the $term parameter is specified in addition to the $taxonomy parameter,
       
  3172 	 * this function will additionally check if the query is for one of the terms
       
  3173 	 * specified.
       
  3174 	 *
       
  3175 	 * @since 3.1.0
       
  3176 	 *
       
  3177 	 * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
       
  3178 	 * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
       
  3179 	 * @return bool
       
  3180 	 */
       
  3181 	function is_tax( $taxonomy = '', $term = '' ) {
       
  3182 		global $wp_taxonomies;
       
  3183 
       
  3184 		if ( !$this->is_tax )
       
  3185 			return false;
       
  3186 
       
  3187 		if ( empty( $taxonomy ) )
       
  3188 			return true;
       
  3189 
       
  3190 		$queried_object = $this->get_queried_object();
       
  3191 		$tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
       
  3192 		$term_array = (array) $term;
       
  3193 
       
  3194 		if ( empty( $term ) ) // Only a Taxonomy provided
       
  3195 			return isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array );
       
  3196 
       
  3197 		return isset( $queried_object->term_id ) &&
       
  3198 			count( array_intersect(
       
  3199 				array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
       
  3200 				$term_array
       
  3201 			) );
       
  3202 	}
       
  3203 
       
  3204 	/**
       
  3205 	 * Whether the current URL is within the comments popup window.
       
  3206 	 *
       
  3207 	 * @since 3.1.0
       
  3208 	 *
       
  3209 	 * @return bool
       
  3210 	 */
       
  3211 	function is_comments_popup() {
       
  3212 		return (bool) $this->is_comments_popup;
       
  3213 	}
       
  3214 
       
  3215 	/**
       
  3216 	 * Is the query for a date archive?
       
  3217 	 *
       
  3218 	 * @since 3.1.0
       
  3219 	 *
       
  3220 	 * @return bool
       
  3221 	 */
       
  3222 	function is_date() {
       
  3223 		return (bool) $this->is_date;
       
  3224 	}
       
  3225 
       
  3226 	/**
       
  3227 	 * Is the query for a day archive?
       
  3228 	 *
       
  3229 	 * @since 3.1.0
       
  3230 	 *
       
  3231 	 * @return bool
       
  3232 	 */
       
  3233 	function is_day() {
       
  3234 		return (bool) $this->is_day;
       
  3235 	}
       
  3236 
       
  3237 	/**
       
  3238 	 * Is the query for a feed?
       
  3239 	 *
       
  3240 	 * @since 3.1.0
       
  3241 	 *
       
  3242 	 * @param string|array $feeds Optional feed types to check.
       
  3243 	 * @return bool
       
  3244 	 */
       
  3245 	function is_feed( $feeds = '' ) {
       
  3246 		if ( empty( $feeds ) || ! $this->is_feed )
       
  3247 			return (bool) $this->is_feed;
       
  3248 		$qv = $this->get( 'feed' );
       
  3249 		if ( 'feed' == $qv )
       
  3250 			$qv = get_default_feed();
       
  3251 		return in_array( $qv, (array) $feeds );
       
  3252 	}
       
  3253 
       
  3254 	/**
       
  3255 	 * Is the query for a comments feed?
       
  3256 	 *
       
  3257 	 * @since 3.1.0
       
  3258 	 *
       
  3259 	 * @return bool
       
  3260 	 */
       
  3261 	function is_comment_feed() {
       
  3262 		return (bool) $this->is_comment_feed;
       
  3263 	}
       
  3264 
       
  3265 	/**
       
  3266 	 * Is the query for the front page of the site?
       
  3267 	 *
       
  3268 	 * This is for what is displayed at your site's main URL.
       
  3269 	 *
       
  3270 	 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
       
  3271 	 *
       
  3272 	 * If you set a static page for the front page of your site, this function will return
       
  3273 	 * true when viewing that page.
       
  3274 	 *
       
  3275 	 * Otherwise the same as @see WP_Query::is_home()
       
  3276 	 *
       
  3277 	 * @since 3.1.0
       
  3278 	 * @uses is_home()
       
  3279 	 * @uses get_option()
       
  3280 	 *
       
  3281 	 * @return bool True, if front of site.
       
  3282 	 */
       
  3283 	function is_front_page() {
       
  3284 		// most likely case
       
  3285 		if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
       
  3286 			return true;
       
  3287 		elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
       
  3288 			return true;
       
  3289 		else
       
  3290 			return false;
       
  3291 	}
       
  3292 
       
  3293 	/**
       
  3294 	 * Is the query for the blog homepage?
       
  3295 	 *
       
  3296 	 * This is the page which shows the time based blog content of your site.
       
  3297 	 *
       
  3298 	 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
       
  3299 	 *
       
  3300 	 * If you set a static page for the front page of your site, this function will return
       
  3301 	 * true only on the page you set as the "Posts page".
       
  3302 	 *
       
  3303 	 * @see WP_Query::is_front_page()
       
  3304 	 *
       
  3305 	 * @since 3.1.0
       
  3306 	 *
       
  3307 	 * @return bool True if blog view homepage.
       
  3308 	 */
       
  3309 	function is_home() {
       
  3310 		return (bool) $this->is_home;
       
  3311 	}
       
  3312 
       
  3313 	/**
       
  3314 	 * Is the query for a month archive?
       
  3315 	 *
       
  3316 	 * @since 3.1.0
       
  3317 	 *
       
  3318 	 * @return bool
       
  3319 	 */
       
  3320 	function is_month() {
       
  3321 		return (bool) $this->is_month;
       
  3322 	}
       
  3323 
       
  3324 	/**
       
  3325 	 * Is the query for a single page?
       
  3326 	 *
       
  3327 	 * If the $page parameter is specified, this function will additionally
       
  3328 	 * check if the query is for one of the pages specified.
       
  3329 	 *
       
  3330 	 * @see WP_Query::is_single()
       
  3331 	 * @see WP_Query::is_singular()
       
  3332 	 *
       
  3333 	 * @since 3.1.0
       
  3334 	 *
       
  3335 	 * @param mixed $page Page ID, title, slug, or array of such.
       
  3336 	 * @return bool
       
  3337 	 */
       
  3338 	function is_page( $page = '' ) {
       
  3339 		if ( !$this->is_page )
       
  3340 			return false;
       
  3341 
       
  3342 		if ( empty( $page ) )
       
  3343 			return true;
       
  3344 
       
  3345 		$page_obj = $this->get_queried_object();
       
  3346 
       
  3347 		$page = (array) $page;
       
  3348 
       
  3349 		if ( in_array( $page_obj->ID, $page ) )
       
  3350 			return true;
       
  3351 		elseif ( in_array( $page_obj->post_title, $page ) )
       
  3352 			return true;
       
  3353 		else if ( in_array( $page_obj->post_name, $page ) )
       
  3354 			return true;
       
  3355 
       
  3356 		return false;
       
  3357 	}
       
  3358 
       
  3359 	/**
       
  3360 	 * Is the query for paged result and not for the first page?
       
  3361 	 *
       
  3362 	 * @since 3.1.0
       
  3363 	 *
       
  3364 	 * @return bool
       
  3365 	 */
       
  3366 	function is_paged() {
       
  3367 		return (bool) $this->is_paged;
       
  3368 	}
       
  3369 
       
  3370 	/**
       
  3371 	 * Is the query for a post or page preview?
       
  3372 	 *
       
  3373 	 * @since 3.1.0
       
  3374 	 *
       
  3375 	 * @return bool
       
  3376 	 */
       
  3377 	function is_preview() {
       
  3378 		return (bool) $this->is_preview;
       
  3379 	}
       
  3380 
       
  3381 	/**
       
  3382 	 * Is the query for the robots file?
       
  3383 	 *
       
  3384 	 * @since 3.1.0
       
  3385 	 *
       
  3386 	 * @return bool
       
  3387 	 */
       
  3388 	function is_robots() {
       
  3389 		return (bool) $this->is_robots;
       
  3390 	}
       
  3391 
       
  3392 	/**
       
  3393 	 * Is the query for a search?
       
  3394 	 *
       
  3395 	 * @since 3.1.0
       
  3396 	 *
       
  3397 	 * @return bool
       
  3398 	 */
       
  3399 	function is_search() {
       
  3400 		return (bool) $this->is_search;
       
  3401 	}
       
  3402 
       
  3403 	/**
       
  3404 	 * Is the query for a single post?
       
  3405 	 *
       
  3406 	 * Works for any post type, except attachments and pages
       
  3407 	 *
       
  3408 	 * If the $post parameter is specified, this function will additionally
       
  3409 	 * check if the query is for one of the Posts specified.
       
  3410 	 *
       
  3411 	 * @see WP_Query::is_page()
       
  3412 	 * @see WP_Query::is_singular()
       
  3413 	 *
       
  3414 	 * @since 3.1.0
       
  3415 	 *
       
  3416 	 * @param mixed $post Post ID, title, slug, or array of such.
       
  3417 	 * @return bool
       
  3418 	 */
       
  3419 	function is_single( $post = '' ) {
       
  3420 		if ( !$this->is_single )
       
  3421 			return false;
       
  3422 
       
  3423 		if ( empty($post) )
       
  3424 			return true;
       
  3425 
       
  3426 		$post_obj = $this->get_queried_object();
       
  3427 
       
  3428 		$post = (array) $post;
       
  3429 
       
  3430 		if ( in_array( $post_obj->ID, $post ) )
       
  3431 			return true;
       
  3432 		elseif ( in_array( $post_obj->post_title, $post ) )
       
  3433 			return true;
       
  3434 		elseif ( in_array( $post_obj->post_name, $post ) )
       
  3435 			return true;
       
  3436 
       
  3437 		return false;
       
  3438 	}
       
  3439 
       
  3440 	/**
       
  3441 	 * Is the query for a single post of any post type (post, attachment, page, ... )?
       
  3442 	 *
       
  3443 	 * If the $post_types parameter is specified, this function will additionally
       
  3444 	 * check if the query is for one of the Posts Types specified.
       
  3445 	 *
       
  3446 	 * @see WP_Query::is_page()
       
  3447 	 * @see WP_Query::is_single()
       
  3448 	 *
       
  3449 	 * @since 3.1.0
       
  3450 	 *
       
  3451 	 * @param mixed $post_types Optional. Post Type or array of Post Types
       
  3452 	 * @return bool
       
  3453 	 */
       
  3454 	function is_singular( $post_types = '' ) {
       
  3455 		if ( empty( $post_types ) || !$this->is_singular )
       
  3456 			return (bool) $this->is_singular;
       
  3457 
       
  3458 		$post_obj = $this->get_queried_object();
       
  3459 
       
  3460 		return in_array( $post_obj->post_type, (array) $post_types );
       
  3461 	}
       
  3462 
       
  3463 	/**
       
  3464 	 * Is the query for a specific time?
       
  3465 	 *
       
  3466 	 * @since 3.1.0
       
  3467 	 *
       
  3468 	 * @return bool
       
  3469 	 */
       
  3470 	function is_time() {
       
  3471 		return (bool) $this->is_time;
       
  3472 	}
       
  3473 
       
  3474 	/**
       
  3475 	 * Is the query for a trackback endpoint call?
       
  3476 	 *
       
  3477 	 * @since 3.1.0
       
  3478 	 *
       
  3479 	 * @return bool
       
  3480 	 */
       
  3481 	function is_trackback() {
       
  3482 		return (bool) $this->is_trackback;
       
  3483 	}
       
  3484 
       
  3485 	/**
       
  3486 	 * Is the query for a specific year?
       
  3487 	 *
       
  3488 	 * @since 3.1.0
       
  3489 	 *
       
  3490 	 * @return bool
       
  3491 	 */
       
  3492 	function is_year() {
       
  3493 		return (bool) $this->is_year;
       
  3494 	}
       
  3495 
       
  3496 	/**
       
  3497 	 * Is the query a 404 (returns no results)?
       
  3498 	 *
       
  3499 	 * @since 3.1.0
       
  3500 	 *
       
  3501 	 * @return bool
       
  3502 	 */
       
  3503 	function is_404() {
       
  3504 		return (bool) $this->is_404;
       
  3505 	}
       
  3506 
       
  3507 	/**
       
  3508 	 * Is the query the main query?
       
  3509 	 *
       
  3510 	 * @since 3.3.0
       
  3511 	 *
       
  3512 	 * @return bool
       
  3513 	 */
       
  3514 	function is_main_query() {
       
  3515 		global $wp_the_query;
       
  3516 		return $wp_the_query === $this;
  2621 	}
  3517 	}
  2622 }
  3518 }
  2623 
  3519 
  2624 /**
  3520 /**
  2625  * Redirect old slugs to the correct permalink.
  3521  * Redirect old slugs to the correct permalink.
  2630  * @uses $wp_query
  3526  * @uses $wp_query
  2631  * @uses $wpdb
  3527  * @uses $wpdb
  2632  *
  3528  *
  2633  * @return null If no link is found, null is returned.
  3529  * @return null If no link is found, null is returned.
  2634  */
  3530  */
  2635 function wp_old_slug_redirect () {
  3531 function wp_old_slug_redirect() {
  2636 	global $wp_query;
  3532 	global $wp_query;
  2637 	if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  3533 	if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  2638 		global $wpdb;
  3534 		global $wpdb;
  2639 
  3535 
  2640 		$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'] . "'";
  3536 		// Guess the current post_type based on the query vars.
       
  3537 		if ( get_query_var('post_type') )
       
  3538 			$post_type = get_query_var('post_type');
       
  3539 		elseif ( !empty($wp_query->query_vars['pagename']) )
       
  3540 			$post_type = 'page';
       
  3541 		else
       
  3542 			$post_type = 'post';
       
  3543 
       
  3544 		if ( is_array( $post_type ) ) {
       
  3545 			if ( count( $post_type ) > 1 )
       
  3546 				return;
       
  3547 			$post_type = array_shift( $post_type );
       
  3548 		}
       
  3549 
       
  3550 		// Do not attempt redirect for hierarchical post types
       
  3551 		if ( is_post_type_hierarchical( $post_type ) )
       
  3552 			return;
       
  3553 
       
  3554 		$query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']);
  2641 
  3555 
  2642 		// if year, monthnum, or day have been specified, make our query more precise
  3556 		// if year, monthnum, or day have been specified, make our query more precise
  2643 		// just in case there are multiple identical _wp_old_slug values
  3557 		// just in case there are multiple identical _wp_old_slug values
  2644 		if ( '' != $wp_query->query_vars['year'] )
  3558 		if ( '' != $wp_query->query_vars['year'] )
  2645 			$query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
  3559 			$query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
  2646 		if ( '' != $wp_query->query_vars['monthnum'] )
  3560 		if ( '' != $wp_query->query_vars['monthnum'] )
  2647 			$query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
  3561 			$query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
  2648 		if ( '' != $wp_query->query_vars['day'] )
  3562 		if ( '' != $wp_query->query_vars['day'] )
  2649 			$query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
  3563 			$query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
  2650 
  3564 
  2651 		$id = (int) $wpdb->get_var($query);
  3565 		$id = (int) $wpdb->get_var($query);
  2652 
  3566 
  2653 		if ( !$id )
  3567 		if ( ! $id )
  2654 			return;
  3568 			return;
  2655 
  3569 
  2656 		$link = get_permalink($id);
  3570 		$link = get_permalink($id);
  2657 
  3571 
  2658 		if ( !$link )
  3572 		if ( !$link )
  2659 			return;
  3573 			return;
  2660 
  3574 
  2661 		wp_redirect($link, '301'); // Permanent redirect
  3575 		wp_redirect( $link, 301 ); // Permanent redirect
  2662 		exit;
  3576 		exit;
  2663 	endif;
  3577 	endif;
  2664 }
  3578 }
  2665 
  3579 
  2666 /**
  3580 /**
  2667  * Setup global post data.
  3581  * Set up global post data.
  2668  *
  3582  *
  2669  * @since 1.5.0
  3583  * @since 1.5.0
  2670  *
  3584  *
  2671  * @param object $post Post data.
  3585  * @param object $post Post data.
  2672  * @uses do_action_ref_array() Calls 'the_post'
  3586  * @uses do_action_ref_array() Calls 'the_post'
  2673  * @return bool True when finished.
  3587  * @return bool True when finished.
  2674  */
  3588  */
  2675 function setup_postdata($post) {
  3589 function setup_postdata($post) {
  2676 	global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  3590 	global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  2677 
  3591 
  2678 	$id = (int) $post->ID;
  3592 	$id = (int) $post->ID;
  2679 
  3593 
  2680 	$authordata = get_userdata($post->post_author);
  3594 	$authordata = get_userdata($post->post_author);
  2681 
  3595 
  2682 	$day = mysql2date('d.m.y', $post->post_date, false);
  3596 	$currentday = mysql2date('d.m.y', $post->post_date, false);
  2683 	$currentmonth = mysql2date('m', $post->post_date, false);
  3597 	$currentmonth = mysql2date('m', $post->post_date, false);
  2684 	$numpages = 1;
  3598 	$numpages = 1;
  2685 	$page = get_query_var('page');
  3599 	$page = get_query_var('page');
  2686 	if ( !$page )
  3600 	if ( !$page )
  2687 		$page = 1;
  3601 		$page = 1;
  2696 		$content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  3610 		$content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  2697 		$content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  3611 		$content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  2698 		$pages = explode('<!--nextpage-->', $content);
  3612 		$pages = explode('<!--nextpage-->', $content);
  2699 		$numpages = count($pages);
  3613 		$numpages = count($pages);
  2700 	} else {
  3614 	} else {
  2701 		$pages[0] = $post->post_content;
  3615 		$pages = array( $post->post_content );
  2702 		$multipage = 0;
  3616 		$multipage = 0;
  2703 	}
  3617 	}
  2704 
  3618 
  2705 	do_action_ref_array('the_post', array(&$post));
  3619 	do_action_ref_array('the_post', array(&$post));
  2706 
  3620 
  2707 	return true;
  3621 	return true;
  2708 }
  3622 }
  2709 
       
  2710 ?>