wp/wp-includes/query.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     3  * WordPress Query API
     3  * WordPress Query API
     4  *
     4  *
     5  * The query API attempts to get which part of WordPress the user is on. It
     5  * The query API attempts to get which part of WordPress the user is on. It
     6  * also provides functionality for getting URL query information.
     6  * also provides functionality for getting URL query information.
     7  *
     7  *
     8  * @link https://codex.wordpress.org/The_Loop More information on The Loop.
     8  * @link https://developer.wordpress.org/themes/basics/the-loop/ More information on The Loop.
     9  *
     9  *
    10  * @package WordPress
    10  * @package WordPress
    11  * @subpackage Query
    11  * @subpackage Query
    12  */
    12  */
    13 
    13 
    15  * Retrieve variable in the WP_Query class.
    15  * Retrieve variable in the WP_Query class.
    16  *
    16  *
    17  * @since 1.5.0
    17  * @since 1.5.0
    18  * @since 3.9.0 The `$default` argument was introduced.
    18  * @since 3.9.0 The `$default` argument was introduced.
    19  *
    19  *
    20  * @global WP_Query $wp_query Global WP_Query instance.
    20  * @global WP_Query $wp_query WordPress Query object.
    21  *
    21  *
    22  * @param string $var       The variable key to retrieve.
    22  * @param string $var       The variable key to retrieve.
    23  * @param mixed  $default   Optional. Value to return if the query variable is not set. Default empty.
    23  * @param mixed  $default   Optional. Value to return if the query variable is not set. Default empty.
    24  * @return mixed Contents of the query variable.
    24  * @return mixed Contents of the query variable.
    25  */
    25  */
    33  *
    33  *
    34  * Wrapper for WP_Query::get_queried_object().
    34  * Wrapper for WP_Query::get_queried_object().
    35  *
    35  *
    36  * @since 3.1.0
    36  * @since 3.1.0
    37  *
    37  *
    38  * @global WP_Query $wp_query Global WP_Query instance.
    38  * @global WP_Query $wp_query WordPress Query object.
    39  *
    39  *
    40  * @return object Queried object.
    40  * @return object Queried object.
    41  */
    41  */
    42 function get_queried_object() {
    42 function get_queried_object() {
    43 	global $wp_query;
    43 	global $wp_query;
    49  *
    49  *
    50  * Wrapper for WP_Query::get_queried_object_id().
    50  * Wrapper for WP_Query::get_queried_object_id().
    51  *
    51  *
    52  * @since 3.1.0
    52  * @since 3.1.0
    53  *
    53  *
    54  * @global WP_Query $wp_query Global WP_Query instance.
    54  * @global WP_Query $wp_query WordPress Query object.
    55  *
    55  *
    56  * @return int ID of the queried object.
    56  * @return int ID of the queried object.
    57  */
    57  */
    58 function get_queried_object_id() {
    58 function get_queried_object_id() {
    59 	global $wp_query;
    59 	global $wp_query;
    63 /**
    63 /**
    64  * Set query variable.
    64  * Set query variable.
    65  *
    65  *
    66  * @since 2.2.0
    66  * @since 2.2.0
    67  *
    67  *
    68  * @global WP_Query $wp_query Global WP_Query instance.
    68  * @global WP_Query $wp_query WordPress Query object.
    69  *
    69  *
    70  * @param string $var   Query variable key.
    70  * @param string $var   Query variable key.
    71  * @param mixed  $value Query variable value.
    71  * @param mixed  $value Query variable value.
    72  */
    72  */
    73 function set_query_var( $var, $value ) {
    73 function set_query_var( $var, $value ) {
    86  *
    86  *
    87  * This must not be used within the WordPress Loop.
    87  * This must not be used within the WordPress Loop.
    88  *
    88  *
    89  * @since 1.5.0
    89  * @since 1.5.0
    90  *
    90  *
    91  * @global WP_Query $wp_query Global WP_Query instance.
    91  * @global WP_Query $wp_query WordPress Query object.
    92  *
    92  *
    93  * @param array|string $query Array or string of WP_Query arguments.
    93  * @param array|string $query Array or string of WP_Query arguments.
    94  * @return array List of post objects.
    94  * @return WP_Post[]|int[] Array of post objects or post IDs.
    95  */
    95  */
    96 function query_posts( $query ) {
    96 function query_posts( $query ) {
    97 	$GLOBALS['wp_query'] = new WP_Query();
    97 	$GLOBALS['wp_query'] = new WP_Query();
    98 	return $GLOBALS['wp_query']->query( $query );
    98 	return $GLOBALS['wp_query']->query( $query );
    99 }
    99 }
   105  * This will remove obscure bugs that occur when the previous WP_Query object
   105  * This will remove obscure bugs that occur when the previous WP_Query object
   106  * is not destroyed properly before another is set up.
   106  * is not destroyed properly before another is set up.
   107  *
   107  *
   108  * @since 2.3.0
   108  * @since 2.3.0
   109  *
   109  *
   110  * @global WP_Query $wp_query     Global WP_Query instance.
   110  * @global WP_Query $wp_query     WordPress Query object.
   111  * @global WP_Query $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
   111  * @global WP_Query $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
   112  */
   112  */
   113 function wp_reset_query() {
   113 function wp_reset_query() {
   114 	$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
   114 	$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
   115 	wp_reset_postdata();
   115 	wp_reset_postdata();
   119  * After looping through a separate query, this function restores
   119  * After looping through a separate query, this function restores
   120  * the $post global to the current post in the main query.
   120  * the $post global to the current post in the main query.
   121  *
   121  *
   122  * @since 3.0.0
   122  * @since 3.0.0
   123  *
   123  *
   124  * @global WP_Query $wp_query Global WP_Query instance.
   124  * @global WP_Query $wp_query WordPress Query object.
   125  */
   125  */
   126 function wp_reset_postdata() {
   126 function wp_reset_postdata() {
   127 	global $wp_query;
   127 	global $wp_query;
   128 
   128 
   129 	if ( isset( $wp_query ) ) {
   129 	if ( isset( $wp_query ) ) {
   144  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   144  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   145  * Conditional Tags} article in the Theme Developer Handbook.
   145  * Conditional Tags} article in the Theme Developer Handbook.
   146  *
   146  *
   147  * @since 1.5.0
   147  * @since 1.5.0
   148  *
   148  *
   149  * @global WP_Query $wp_query Global WP_Query instance.
   149  * @global WP_Query $wp_query WordPress Query object.
   150  *
   150  *
   151  * @return bool
   151  * @return bool Whether the query is for an existing archive page.
   152  */
   152  */
   153 function is_archive() {
   153 function is_archive() {
   154 	global $wp_query;
   154 	global $wp_query;
   155 
   155 
   156 	if ( ! isset( $wp_query ) ) {
   156 	if ( ! isset( $wp_query ) ) {
   168  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   168  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   169  * Conditional Tags} article in the Theme Developer Handbook.
   169  * Conditional Tags} article in the Theme Developer Handbook.
   170  *
   170  *
   171  * @since 3.1.0
   171  * @since 3.1.0
   172  *
   172  *
   173  * @global WP_Query $wp_query Global WP_Query instance.
   173  * @global WP_Query $wp_query WordPress Query object.
   174  *
   174  *
   175  * @param string|array $post_types Optional. Post type or array of posts types to check against.
   175  * @param string|string[] $post_types Optional. Post type or array of posts types
   176  * @return bool
   176  *                                    to check against. Default empty.
       
   177  * @return bool Whether the query is for an existing post type archive page.
   177  */
   178  */
   178 function is_post_type_archive( $post_types = '' ) {
   179 function is_post_type_archive( $post_types = '' ) {
   179 	global $wp_query;
   180 	global $wp_query;
   180 
   181 
   181 	if ( ! isset( $wp_query ) ) {
   182 	if ( ! isset( $wp_query ) ) {
   193  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   194  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   194  * Conditional Tags} article in the Theme Developer Handbook.
   195  * Conditional Tags} article in the Theme Developer Handbook.
   195  *
   196  *
   196  * @since 2.0.0
   197  * @since 2.0.0
   197  *
   198  *
   198  * @global WP_Query $wp_query Global WP_Query instance.
   199  * @global WP_Query $wp_query WordPress Query object.
   199  *
   200  *
   200  * @param int|string|array|object $attachment Attachment ID, title, slug, or array of such.
   201  * @param int|string|int[]|string[] $attachment Optional. Attachment ID, title, slug, or array of such
   201  * @return bool
   202  *                                              to check against. Default empty.
       
   203  * @return bool Whether the query is for an existing attachment page.
   202  */
   204  */
   203 function is_attachment( $attachment = '' ) {
   205 function is_attachment( $attachment = '' ) {
   204 	global $wp_query;
   206 	global $wp_query;
   205 
   207 
   206 	if ( ! isset( $wp_query ) ) {
   208 	if ( ! isset( $wp_query ) ) {
   221  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   223  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   222  * Conditional Tags} article in the Theme Developer Handbook.
   224  * Conditional Tags} article in the Theme Developer Handbook.
   223  *
   225  *
   224  * @since 1.5.0
   226  * @since 1.5.0
   225  *
   227  *
   226  * @global WP_Query $wp_query Global WP_Query instance.
   228  * @global WP_Query $wp_query WordPress Query object.
   227  *
   229  *
   228  * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
   230  * @param int|string|int[]|string[] $author Optional. User ID, nickname, nicename, or array of such
   229  * @return bool
   231  *                                          to check against. Default empty.
       
   232  * @return bool Whether the query is for an existing author archive page.
   230  */
   233  */
   231 function is_author( $author = '' ) {
   234 function is_author( $author = '' ) {
   232 	global $wp_query;
   235 	global $wp_query;
   233 
   236 
   234 	if ( ! isset( $wp_query ) ) {
   237 	if ( ! isset( $wp_query ) ) {
   249  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   252  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   250  * Conditional Tags} article in the Theme Developer Handbook.
   253  * Conditional Tags} article in the Theme Developer Handbook.
   251  *
   254  *
   252  * @since 1.5.0
   255  * @since 1.5.0
   253  *
   256  *
   254  * @global WP_Query $wp_query Global WP_Query instance.
   257  * @global WP_Query $wp_query WordPress Query object.
   255  *
   258  *
   256  * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
   259  * @param int|string|int[]|string[] $category Optional. Category ID, name, slug, or array of such
   257  * @return bool
   260  *                                            to check against. Default empty.
       
   261  * @return bool Whether the query is for an existing category archive page.
   258  */
   262  */
   259 function is_category( $category = '' ) {
   263 function is_category( $category = '' ) {
   260 	global $wp_query;
   264 	global $wp_query;
   261 
   265 
   262 	if ( ! isset( $wp_query ) ) {
   266 	if ( ! isset( $wp_query ) ) {
   277  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   281  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   278  * Conditional Tags} article in the Theme Developer Handbook.
   282  * Conditional Tags} article in the Theme Developer Handbook.
   279  *
   283  *
   280  * @since 2.3.0
   284  * @since 2.3.0
   281  *
   285  *
   282  * @global WP_Query $wp_query Global WP_Query instance.
   286  * @global WP_Query $wp_query WordPress Query object.
   283  *
   287  *
   284  * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
   288  * @param int|string|int[]|string[] $tag Optional. Tag ID, name, slug, or array of such
   285  * @return bool
   289  *                                       to check against. Default empty.
       
   290  * @return bool Whether the query is for an existing tag archive page.
   286  */
   291  */
   287 function is_tag( $tag = '' ) {
   292 function is_tag( $tag = '' ) {
   288 	global $wp_query;
   293 	global $wp_query;
   289 
   294 
   290 	if ( ! isset( $wp_query ) ) {
   295 	if ( ! isset( $wp_query ) ) {
   309  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   314  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   310  * Conditional Tags} article in the Theme Developer Handbook.
   315  * Conditional Tags} article in the Theme Developer Handbook.
   311  *
   316  *
   312  * @since 2.5.0
   317  * @since 2.5.0
   313  *
   318  *
   314  * @global WP_Query $wp_query Global WP_Query instance.
   319  * @global WP_Query $wp_query WordPress Query object.
   315  *
   320  *
   316  * @param string|array     $taxonomy Optional. Taxonomy slug or slugs.
   321  * @param string|string[]           $taxonomy Optional. Taxonomy slug or slugs to check against.
   317  * @param int|string|array $term     Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
   322  *                                            Default empty.
   318  * @return bool True for custom taxonomy archive pages, false for built-in taxonomies (category and tag archives).
   323  * @param int|string|int[]|string[] $term     Optional. Term ID, name, slug, or array of such
       
   324  *                                            to check against. Default empty.
       
   325  * @return bool Whether the query is for an existing custom taxonomy archive page.
       
   326  *              True for custom taxonomy archive pages, false for built-in taxonomies
       
   327  *              (category and tag archives).
   319  */
   328  */
   320 function is_tax( $taxonomy = '', $term = '' ) {
   329 function is_tax( $taxonomy = '', $term = '' ) {
   321 	global $wp_query;
   330 	global $wp_query;
   322 
   331 
   323 	if ( ! isset( $wp_query ) ) {
   332 	if ( ! isset( $wp_query ) ) {
   335  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   344  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   336  * Conditional Tags} article in the Theme Developer Handbook.
   345  * Conditional Tags} article in the Theme Developer Handbook.
   337  *
   346  *
   338  * @since 1.5.0
   347  * @since 1.5.0
   339  *
   348  *
   340  * @global WP_Query $wp_query Global WP_Query instance.
   349  * @global WP_Query $wp_query WordPress Query object.
   341  *
   350  *
   342  * @return bool
   351  * @return bool Whether the query is for an existing date archive.
   343  */
   352  */
   344 function is_date() {
   353 function is_date() {
   345 	global $wp_query;
   354 	global $wp_query;
   346 
   355 
   347 	if ( ! isset( $wp_query ) ) {
   356 	if ( ! isset( $wp_query ) ) {
   361  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   370  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   362  * Conditional Tags} article in the Theme Developer Handbook.
   371  * Conditional Tags} article in the Theme Developer Handbook.
   363  *
   372  *
   364  * @since 1.5.0
   373  * @since 1.5.0
   365  *
   374  *
   366  * @global WP_Query $wp_query Global WP_Query instance.
   375  * @global WP_Query $wp_query WordPress Query object.
   367  *
   376  *
   368  * @return bool
   377  * @return bool Whether the query is for an existing day archive.
   369  */
   378  */
   370 function is_day() {
   379 function is_day() {
   371 	global $wp_query;
   380 	global $wp_query;
   372 
   381 
   373 	if ( ! isset( $wp_query ) ) {
   382 	if ( ! isset( $wp_query ) ) {
   385  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   394  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   386  * Conditional Tags} article in the Theme Developer Handbook.
   395  * Conditional Tags} article in the Theme Developer Handbook.
   387  *
   396  *
   388  * @since 1.5.0
   397  * @since 1.5.0
   389  *
   398  *
   390  * @global WP_Query $wp_query Global WP_Query instance.
   399  * @global WP_Query $wp_query WordPress Query object.
   391  *
   400  *
   392  * @param string|array $feeds Optional feed types to check.
   401  * @param string|string[] $feeds Optional. Feed type or array of feed types
   393  * @return bool
   402  *                                         to check against. Default empty.
       
   403  * @return bool Whether the query is for a feed.
   394  */
   404  */
   395 function is_feed( $feeds = '' ) {
   405 function is_feed( $feeds = '' ) {
   396 	global $wp_query;
   406 	global $wp_query;
   397 
   407 
   398 	if ( ! isset( $wp_query ) ) {
   408 	if ( ! isset( $wp_query ) ) {
   406 /**
   416 /**
   407  * Is the query for a comments feed?
   417  * Is the query for a comments feed?
   408  *
   418  *
   409  * @since 3.0.0
   419  * @since 3.0.0
   410  *
   420  *
   411  * @global WP_Query $wp_query Global WP_Query instance.
   421  * @global WP_Query $wp_query WordPress Query object.
   412  *
   422  *
   413  * @return bool
   423  * @return bool Whether the query is for a comments feed.
   414  */
   424  */
   415 function is_comment_feed() {
   425 function is_comment_feed() {
   416 	global $wp_query;
   426 	global $wp_query;
   417 
   427 
   418 	if ( ! isset( $wp_query ) ) {
   428 	if ( ! isset( $wp_query ) ) {
   439  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   449  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   440  * Conditional Tags} article in the Theme Developer Handbook.
   450  * Conditional Tags} article in the Theme Developer Handbook.
   441  *
   451  *
   442  * @since 2.5.0
   452  * @since 2.5.0
   443  *
   453  *
   444  * @global WP_Query $wp_query Global WP_Query instance.
   454  * @global WP_Query $wp_query WordPress Query object.
   445  *
   455  *
   446  * @return bool True, if front of site.
   456  * @return bool Whether the query is for the front page of the site.
   447  */
   457  */
   448 function is_front_page() {
   458 function is_front_page() {
   449 	global $wp_query;
   459 	global $wp_query;
   450 
   460 
   451 	if ( ! isset( $wp_query ) ) {
   461 	if ( ! isset( $wp_query ) ) {
   472  * Conditional Tags} article in the Theme Developer Handbook.
   482  * Conditional Tags} article in the Theme Developer Handbook.
   473  *
   483  *
   474  * @since 1.5.0
   484  * @since 1.5.0
   475  *
   485  *
   476  * @see is_front_page()
   486  * @see is_front_page()
   477  * @global WP_Query $wp_query Global WP_Query instance.
   487  * @global WP_Query $wp_query WordPress Query object.
   478  *
   488  *
   479  * @return bool True if blog view homepage, otherwise false.
   489  * @return bool Whether the query is for the blog homepage.
   480  */
   490  */
   481 function is_home() {
   491 function is_home() {
   482 	global $wp_query;
   492 	global $wp_query;
   483 
   493 
   484 	if ( ! isset( $wp_query ) ) {
   494 	if ( ! isset( $wp_query ) ) {
   502  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   512  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   503  * Conditional Tags} article in the Theme Developer Handbook.
   513  * Conditional Tags} article in the Theme Developer Handbook.
   504  *
   514  *
   505  * @since 5.2.0
   515  * @since 5.2.0
   506  *
   516  *
   507  * @global WP_Query $wp_query Global WP_Query instance.
   517  * @global WP_Query $wp_query WordPress Query object.
   508  *
   518  *
   509  * @return bool
   519  * @return bool Whether the query is for the Privacy Policy page.
   510  */
   520  */
   511 function is_privacy_policy() {
   521 function is_privacy_policy() {
   512 	global $wp_query;
   522 	global $wp_query;
   513 
   523 
   514 	if ( ! isset( $wp_query ) ) {
   524 	if ( ! isset( $wp_query ) ) {
   526  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   536  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   527  * Conditional Tags} article in the Theme Developer Handbook.
   537  * Conditional Tags} article in the Theme Developer Handbook.
   528  *
   538  *
   529  * @since 1.5.0
   539  * @since 1.5.0
   530  *
   540  *
   531  * @global WP_Query $wp_query Global WP_Query instance.
   541  * @global WP_Query $wp_query WordPress Query object.
   532  *
   542  *
   533  * @return bool
   543  * @return bool Whether the query is for an existing month archive.
   534  */
   544  */
   535 function is_month() {
   545 function is_month() {
   536 	global $wp_query;
   546 	global $wp_query;
   537 
   547 
   538 	if ( ! isset( $wp_query ) ) {
   548 	if ( ! isset( $wp_query ) ) {
   556  * @see is_single()
   566  * @see is_single()
   557  * @see is_singular()
   567  * @see is_singular()
   558  *
   568  *
   559  * @since 1.5.0
   569  * @since 1.5.0
   560  *
   570  *
   561  * @global WP_Query $wp_query Global WP_Query instance.
   571  * @global WP_Query $wp_query WordPress Query object.
   562  *
   572  *
   563  * @param int|string|array $page Optional. Page ID, title, slug, or array of such. Default empty.
   573  * @param int|string|int[]|string[] $page Optional. Page ID, title, slug, or array of such
       
   574  *                                        to check against. Default empty.
   564  * @return bool Whether the query is for an existing single page.
   575  * @return bool Whether the query is for an existing single page.
   565  */
   576  */
   566 function is_page( $page = '' ) {
   577 function is_page( $page = '' ) {
   567 	global $wp_query;
   578 	global $wp_query;
   568 
   579 
   573 
   584 
   574 	return $wp_query->is_page( $page );
   585 	return $wp_query->is_page( $page );
   575 }
   586 }
   576 
   587 
   577 /**
   588 /**
   578  * Determines whether the query is for paged results and not for the first page.
   589  * Determines whether the query is for a paged result and not for the first page.
   579  *
   590  *
   580  * For more information on this and similar theme functions, check out
   591  * For more information on this and similar theme functions, check out
   581  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   592  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   582  * Conditional Tags} article in the Theme Developer Handbook.
   593  * Conditional Tags} article in the Theme Developer Handbook.
   583  *
   594  *
   584  * @since 1.5.0
   595  * @since 1.5.0
   585  *
   596  *
   586  * @global WP_Query $wp_query Global WP_Query instance.
   597  * @global WP_Query $wp_query WordPress Query object.
   587  *
   598  *
   588  * @return bool
   599  * @return bool Whether the query is for a paged result.
   589  */
   600  */
   590 function is_paged() {
   601 function is_paged() {
   591 	global $wp_query;
   602 	global $wp_query;
   592 
   603 
   593 	if ( ! isset( $wp_query ) ) {
   604 	if ( ! isset( $wp_query ) ) {
   605  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   616  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   606  * Conditional Tags} article in the Theme Developer Handbook.
   617  * Conditional Tags} article in the Theme Developer Handbook.
   607  *
   618  *
   608  * @since 2.0.0
   619  * @since 2.0.0
   609  *
   620  *
   610  * @global WP_Query $wp_query Global WP_Query instance.
   621  * @global WP_Query $wp_query WordPress Query object.
   611  *
   622  *
   612  * @return bool
   623  * @return bool Whether the query is for a post or page preview.
   613  */
   624  */
   614 function is_preview() {
   625 function is_preview() {
   615 	global $wp_query;
   626 	global $wp_query;
   616 
   627 
   617 	if ( ! isset( $wp_query ) ) {
   628 	if ( ! isset( $wp_query ) ) {
   621 
   632 
   622 	return $wp_query->is_preview();
   633 	return $wp_query->is_preview();
   623 }
   634 }
   624 
   635 
   625 /**
   636 /**
   626  * Is the query for the robots file?
   637  * Is the query for the robots.txt file?
   627  *
   638  *
   628  * @since 2.1.0
   639  * @since 2.1.0
   629  *
   640  *
   630  * @global WP_Query $wp_query Global WP_Query instance.
   641  * @global WP_Query $wp_query WordPress Query object.
   631  *
   642  *
   632  * @return bool
   643  * @return bool Whether the query is for the robots.txt file.
   633  */
   644  */
   634 function is_robots() {
   645 function is_robots() {
   635 	global $wp_query;
   646 	global $wp_query;
   636 
   647 
   637 	if ( ! isset( $wp_query ) ) {
   648 	if ( ! isset( $wp_query ) ) {
   641 
   652 
   642 	return $wp_query->is_robots();
   653 	return $wp_query->is_robots();
   643 }
   654 }
   644 
   655 
   645 /**
   656 /**
       
   657  * Is the query for the favicon.ico file?
       
   658  *
       
   659  * @since 5.4.0
       
   660  *
       
   661  * @global WP_Query $wp_query WordPress Query object.
       
   662  *
       
   663  * @return bool Whether the query is for the favicon.ico file.
       
   664  */
       
   665 function is_favicon() {
       
   666 	global $wp_query;
       
   667 
       
   668 	if ( ! isset( $wp_query ) ) {
       
   669 		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
       
   670 		return false;
       
   671 	}
       
   672 
       
   673 	return $wp_query->is_favicon();
       
   674 }
       
   675 
       
   676 /**
   646  * Determines whether the query is for a search.
   677  * Determines whether the query is for a search.
   647  *
   678  *
   648  * For more information on this and similar theme functions, check out
   679  * For more information on this and similar theme functions, check out
   649  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   680  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   650  * Conditional Tags} article in the Theme Developer Handbook.
   681  * Conditional Tags} article in the Theme Developer Handbook.
   651  *
   682  *
   652  * @since 1.5.0
   683  * @since 1.5.0
   653  *
   684  *
   654  * @global WP_Query $wp_query Global WP_Query instance.
   685  * @global WP_Query $wp_query WordPress Query object.
   655  *
   686  *
   656  * @return bool
   687  * @return bool Whether the query is for a search.
   657  */
   688  */
   658 function is_search() {
   689 function is_search() {
   659 	global $wp_query;
   690 	global $wp_query;
   660 
   691 
   661 	if ( ! isset( $wp_query ) ) {
   692 	if ( ! isset( $wp_query ) ) {
   681  * @see is_page()
   712  * @see is_page()
   682  * @see is_singular()
   713  * @see is_singular()
   683  *
   714  *
   684  * @since 1.5.0
   715  * @since 1.5.0
   685  *
   716  *
   686  * @global WP_Query $wp_query Global WP_Query instance.
   717  * @global WP_Query $wp_query WordPress Query object.
   687  *
   718  *
   688  * @param int|string|array $post Optional. Post ID, title, slug, or array of such. Default empty.
   719  * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such
       
   720  *                                        to check against. Default empty.
   689  * @return bool Whether the query is for an existing single post.
   721  * @return bool Whether the query is for an existing single post.
   690  */
   722  */
   691 function is_single( $post = '' ) {
   723 function is_single( $post = '' ) {
   692 	global $wp_query;
   724 	global $wp_query;
   693 
   725 
   713  * @see is_page()
   745  * @see is_page()
   714  * @see is_single()
   746  * @see is_single()
   715  *
   747  *
   716  * @since 1.5.0
   748  * @since 1.5.0
   717  *
   749  *
   718  * @global WP_Query $wp_query Global WP_Query instance.
   750  * @global WP_Query $wp_query WordPress Query object.
   719  *
   751  *
   720  * @param string|array $post_types Optional. Post type or array of post types. Default empty.
   752  * @param string|string[] $post_types Optional. Post type or array of post types
   721  * @return bool Whether the query is for an existing single post of any of the given post types.
   753  *                                    to check against. Default empty.
       
   754  * @return bool Whether the query is for an existing single post
       
   755  *              or any of the given post types.
   722  */
   756  */
   723 function is_singular( $post_types = '' ) {
   757 function is_singular( $post_types = '' ) {
   724 	global $wp_query;
   758 	global $wp_query;
   725 
   759 
   726 	if ( ! isset( $wp_query ) ) {
   760 	if ( ! isset( $wp_query ) ) {
   738  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   772  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   739  * Conditional Tags} article in the Theme Developer Handbook.
   773  * Conditional Tags} article in the Theme Developer Handbook.
   740  *
   774  *
   741  * @since 1.5.0
   775  * @since 1.5.0
   742  *
   776  *
   743  * @global WP_Query $wp_query Global WP_Query instance.
   777  * @global WP_Query $wp_query WordPress Query object.
   744  *
   778  *
   745  * @return bool
   779  * @return bool Whether the query is for a specific time.
   746  */
   780  */
   747 function is_time() {
   781 function is_time() {
   748 	global $wp_query;
   782 	global $wp_query;
   749 
   783 
   750 	if ( ! isset( $wp_query ) ) {
   784 	if ( ! isset( $wp_query ) ) {
   762  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   796  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   763  * Conditional Tags} article in the Theme Developer Handbook.
   797  * Conditional Tags} article in the Theme Developer Handbook.
   764  *
   798  *
   765  * @since 1.5.0
   799  * @since 1.5.0
   766  *
   800  *
   767  * @global WP_Query $wp_query Global WP_Query instance.
   801  * @global WP_Query $wp_query WordPress Query object.
   768  *
   802  *
   769  * @return bool
   803  * @return bool Whether the query is for a trackback endpoint call.
   770  */
   804  */
   771 function is_trackback() {
   805 function is_trackback() {
   772 	global $wp_query;
   806 	global $wp_query;
   773 
   807 
   774 	if ( ! isset( $wp_query ) ) {
   808 	if ( ! isset( $wp_query ) ) {
   786  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   820  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   787  * Conditional Tags} article in the Theme Developer Handbook.
   821  * Conditional Tags} article in the Theme Developer Handbook.
   788  *
   822  *
   789  * @since 1.5.0
   823  * @since 1.5.0
   790  *
   824  *
   791  * @global WP_Query $wp_query Global WP_Query instance.
   825  * @global WP_Query $wp_query WordPress Query object.
   792  *
   826  *
   793  * @return bool
   827  * @return bool Whether the query is for an existing year archive.
   794  */
   828  */
   795 function is_year() {
   829 function is_year() {
   796 	global $wp_query;
   830 	global $wp_query;
   797 
   831 
   798 	if ( ! isset( $wp_query ) ) {
   832 	if ( ! isset( $wp_query ) ) {
   810  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   844  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   811  * Conditional Tags} article in the Theme Developer Handbook.
   845  * Conditional Tags} article in the Theme Developer Handbook.
   812  *
   846  *
   813  * @since 1.5.0
   847  * @since 1.5.0
   814  *
   848  *
   815  * @global WP_Query $wp_query Global WP_Query instance.
   849  * @global WP_Query $wp_query WordPress Query object.
   816  *
   850  *
   817  * @return bool
   851  * @return bool Whether the query is a 404 error.
   818  */
   852  */
   819 function is_404() {
   853 function is_404() {
   820 	global $wp_query;
   854 	global $wp_query;
   821 
   855 
   822 	if ( ! isset( $wp_query ) ) {
   856 	if ( ! isset( $wp_query ) ) {
   830 /**
   864 /**
   831  * Is the query for an embedded post?
   865  * Is the query for an embedded post?
   832  *
   866  *
   833  * @since 4.4.0
   867  * @since 4.4.0
   834  *
   868  *
   835  * @global WP_Query $wp_query Global WP_Query instance.
   869  * @global WP_Query $wp_query WordPress Query object.
   836  *
   870  *
   837  * @return bool Whether we're in an embedded post or not.
   871  * @return bool Whether the query is for an embedded post.
   838  */
   872  */
   839 function is_embed() {
   873 function is_embed() {
   840 	global $wp_query;
   874 	global $wp_query;
   841 
   875 
   842 	if ( ! isset( $wp_query ) ) {
   876 	if ( ! isset( $wp_query ) ) {
   854  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   888  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   855  * Conditional Tags} article in the Theme Developer Handbook.
   889  * Conditional Tags} article in the Theme Developer Handbook.
   856  *
   890  *
   857  * @since 3.3.0
   891  * @since 3.3.0
   858  *
   892  *
   859  * @global WP_Query $wp_query Global WP_Query instance.
   893  * @global WP_Query $wp_query WordPress Query object.
   860  *
   894  *
   861  * @return bool
   895  * @return bool Whether the query is the main query.
   862  */
   896  */
   863 function is_main_query() {
   897 function is_main_query() {
   864 	if ( 'pre_get_posts' === current_filter() ) {
   898 	if ( 'pre_get_posts' === current_filter() ) {
   865 		$message = sprintf(
   899 		$message = sprintf(
   866 			/* translators: 1: pre_get_posts 2: WP_Query->is_main_query() 3: is_main_query() 4: link to codex is_main_query() page. */
   900 			/* translators: 1: pre_get_posts, 2: WP_Query->is_main_query(), 3: is_main_query(), 4: Link to codex is_main_query() page. */
   867 			__( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
   901 			__( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
   868 			'<code>pre_get_posts</code>',
   902 			'<code>pre_get_posts</code>',
   869 			'<code>WP_Query->is_main_query()</code>',
   903 			'<code>WP_Query->is_main_query()</code>',
   870 			'<code>is_main_query()</code>',
   904 			'<code>is_main_query()</code>',
   871 			__( 'https://codex.wordpress.org/Function_Reference/is_main_query' )
   905 			__( 'https://codex.wordpress.org/Function_Reference/is_main_query' )
   880 /*
   914 /*
   881  * The Loop. Post loop control.
   915  * The Loop. Post loop control.
   882  */
   916  */
   883 
   917 
   884 /**
   918 /**
   885  * Whether current WordPress query has results to loop over.
   919  * Determines whether current WordPress query has posts to loop over.
   886  *
   920  *
   887  * @since 1.5.0
   921  * @since 1.5.0
   888  *
   922  *
   889  * @global WP_Query $wp_query Global WP_Query instance.
   923  * @global WP_Query $wp_query WordPress Query object.
   890  *
   924  *
   891  * @return bool
   925  * @return bool True if posts are available, false if end of the loop.
   892  */
   926  */
   893 function have_posts() {
   927 function have_posts() {
   894 	global $wp_query;
   928 	global $wp_query;
   895 	return $wp_query->have_posts();
   929 	return $wp_query->have_posts();
   896 }
   930 }
   902  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   936  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   903  * Conditional Tags} article in the Theme Developer Handbook.
   937  * Conditional Tags} article in the Theme Developer Handbook.
   904  *
   938  *
   905  * @since 2.0.0
   939  * @since 2.0.0
   906  *
   940  *
   907  * @global WP_Query $wp_query Global WP_Query instance.
   941  * @global WP_Query $wp_query WordPress Query object.
   908  *
   942  *
   909  * @return bool True if caller is within loop, false if loop hasn't started or ended.
   943  * @return bool True if caller is within loop, false if loop hasn't started or ended.
   910  */
   944  */
   911 function in_the_loop() {
   945 function in_the_loop() {
   912 	global $wp_query;
   946 	global $wp_query;
   916 /**
   950 /**
   917  * Rewind the loop posts.
   951  * Rewind the loop posts.
   918  *
   952  *
   919  * @since 1.5.0
   953  * @since 1.5.0
   920  *
   954  *
   921  * @global WP_Query $wp_query Global WP_Query instance.
   955  * @global WP_Query $wp_query WordPress Query object.
   922  */
   956  */
   923 function rewind_posts() {
   957 function rewind_posts() {
   924 	global $wp_query;
   958 	global $wp_query;
   925 	$wp_query->rewind_posts();
   959 	$wp_query->rewind_posts();
   926 }
   960 }
   928 /**
   962 /**
   929  * Iterate the post index in the loop.
   963  * Iterate the post index in the loop.
   930  *
   964  *
   931  * @since 1.5.0
   965  * @since 1.5.0
   932  *
   966  *
   933  * @global WP_Query $wp_query Global WP_Query instance.
   967  * @global WP_Query $wp_query WordPress Query object.
   934  */
   968  */
   935 function the_post() {
   969 function the_post() {
   936 	global $wp_query;
   970 	global $wp_query;
   937 	$wp_query->the_post();
   971 	$wp_query->the_post();
   938 }
   972 }
   940 /*
   974 /*
   941  * Comments loop.
   975  * Comments loop.
   942  */
   976  */
   943 
   977 
   944 /**
   978 /**
   945  * Whether there are comments to loop over.
   979  * Determines whether current WordPress query has comments to loop over.
   946  *
   980  *
   947  * @since 2.2.0
   981  * @since 2.2.0
   948  *
   982  *
   949  * @global WP_Query $wp_query Global WP_Query instance.
   983  * @global WP_Query $wp_query WordPress Query object.
   950  *
   984  *
   951  * @return bool
   985  * @return bool True if comments are available, false if no more comments.
   952  */
   986  */
   953 function have_comments() {
   987 function have_comments() {
   954 	global $wp_query;
   988 	global $wp_query;
   955 	return $wp_query->have_comments();
   989 	return $wp_query->have_comments();
   956 }
   990 }
   958 /**
   992 /**
   959  * Iterate comment index in the comment loop.
   993  * Iterate comment index in the comment loop.
   960  *
   994  *
   961  * @since 2.2.0
   995  * @since 2.2.0
   962  *
   996  *
   963  * @global WP_Query $wp_query Global WP_Query instance.
   997  * @global WP_Query $wp_query WordPress Query object.
   964  *
   998  *
   965  * @return object
   999  * @return object
   966  */
  1000  */
   967 function the_comment() {
  1001 function the_comment() {
   968 	global $wp_query;
  1002 	global $wp_query;
   976  *
  1010  *
   977  * @since 2.1.0
  1011  * @since 2.1.0
   978  */
  1012  */
   979 function wp_old_slug_redirect() {
  1013 function wp_old_slug_redirect() {
   980 	if ( is_404() && '' !== get_query_var( 'name' ) ) {
  1014 	if ( is_404() && '' !== get_query_var( 'name' ) ) {
   981 		// Guess the current post_type based on the query vars.
  1015 		// Guess the current post type based on the query vars.
   982 		if ( get_query_var( 'post_type' ) ) {
  1016 		if ( get_query_var( 'post_type' ) ) {
   983 			$post_type = get_query_var( 'post_type' );
  1017 			$post_type = get_query_var( 'post_type' );
   984 		} elseif ( get_query_var( 'attachment' ) ) {
  1018 		} elseif ( get_query_var( 'attachment' ) ) {
   985 			$post_type = 'attachment';
  1019 			$post_type = 'attachment';
   986 		} elseif ( get_query_var( 'pagename' ) ) {
  1020 		} elseif ( get_query_var( 'pagename' ) ) {
   994 				return;
  1028 				return;
   995 			}
  1029 			}
   996 			$post_type = reset( $post_type );
  1030 			$post_type = reset( $post_type );
   997 		}
  1031 		}
   998 
  1032 
   999 		// Do not attempt redirect for hierarchical post types
  1033 		// Do not attempt redirect for hierarchical post types.
  1000 		if ( is_post_type_hierarchical( $post_type ) ) {
  1034 		if ( is_post_type_hierarchical( $post_type ) ) {
  1001 			return;
  1035 			return;
  1002 		}
  1036 		}
  1003 
  1037 
  1004 		$id = _find_post_by_old_slug( $post_type );
  1038 		$id = _find_post_by_old_slug( $post_type );
  1039 
  1073 
  1040 		if ( ! $link ) {
  1074 		if ( ! $link ) {
  1041 			return;
  1075 			return;
  1042 		}
  1076 		}
  1043 
  1077 
  1044 		wp_redirect( $link, 301 ); // Permanent redirect
  1078 		wp_redirect( $link, 301 ); // Permanent redirect.
  1045 		exit;
  1079 		exit;
  1046 	}
  1080 	}
  1047 }
  1081 }
  1048 
  1082 
  1049 /**
  1083 /**
  1055  * @access private
  1089  * @access private
  1056  *
  1090  *
  1057  * @global wpdb $wpdb WordPress database abstraction object.
  1091  * @global wpdb $wpdb WordPress database abstraction object.
  1058  *
  1092  *
  1059  * @param string $post_type The current post type based on the query vars.
  1093  * @param string $post_type The current post type based on the query vars.
  1060  * @return int $id The Post ID.
  1094  * @return int The Post ID.
  1061  */
  1095  */
  1062 function _find_post_by_old_slug( $post_type ) {
  1096 function _find_post_by_old_slug( $post_type ) {
  1063 	global $wpdb;
  1097 	global $wpdb;
  1064 
  1098 
  1065 	$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, get_query_var( 'name' ) );
  1099 	$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, get_query_var( 'name' ) );
  1066 
  1100 
  1067 	// if year, monthnum, or day have been specified, make our query more precise
  1101 	// If year, monthnum, or day have been specified, make our query more precise
  1068 	// just in case there are multiple identical _wp_old_slug values
  1102 	// just in case there are multiple identical _wp_old_slug values.
  1069 	if ( get_query_var( 'year' ) ) {
  1103 	if ( get_query_var( 'year' ) ) {
  1070 		$query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
  1104 		$query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
  1071 	}
  1105 	}
  1072 	if ( get_query_var( 'monthnum' ) ) {
  1106 	if ( get_query_var( 'monthnum' ) ) {
  1073 		$query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
  1107 		$query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
  1090  * @access private
  1124  * @access private
  1091  *
  1125  *
  1092  * @global wpdb $wpdb WordPress database abstraction object.
  1126  * @global wpdb $wpdb WordPress database abstraction object.
  1093  *
  1127  *
  1094  * @param string $post_type The current post type based on the query vars.
  1128  * @param string $post_type The current post type based on the query vars.
  1095  * @return int $id The Post ID.
  1129  * @return int The Post ID.
  1096  */
  1130  */
  1097 function _find_post_by_old_date( $post_type ) {
  1131 function _find_post_by_old_date( $post_type ) {
  1098 	global $wpdb;
  1132 	global $wpdb;
  1099 
  1133 
  1100 	$date_query = '';
  1134 	$date_query = '';
  1111 	$id = 0;
  1145 	$id = 0;
  1112 	if ( $date_query ) {
  1146 	if ( $date_query ) {
  1113 		$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) );
  1147 		$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) );
  1114 
  1148 
  1115 		if ( ! $id ) {
  1149 		if ( ! $id ) {
  1116 			// Check to see if an old slug matches the old date
  1150 			// Check to see if an old slug matches the old date.
  1117 			$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
  1151 			$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
  1118 		}
  1152 		}
  1119 	}
  1153 	}
  1120 
  1154 
  1121 	return $id;
  1155 	return $id;
  1125  * Set up global post data.
  1159  * Set up global post data.
  1126  *
  1160  *
  1127  * @since 1.5.0
  1161  * @since 1.5.0
  1128  * @since 4.4.0 Added the ability to pass a post ID to `$post`.
  1162  * @since 4.4.0 Added the ability to pass a post ID to `$post`.
  1129  *
  1163  *
  1130  * @global WP_Query $wp_query Global WP_Query instance.
  1164  * @global WP_Query $wp_query WordPress Query object.
  1131  *
  1165  *
  1132  * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  1166  * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  1133  * @return bool True when finished.
  1167  * @return bool True when finished.
  1134  */
  1168  */
  1135 function setup_postdata( $post ) {
  1169 function setup_postdata( $post ) {
  1145 /**
  1179 /**
  1146  * Generates post data.
  1180  * Generates post data.
  1147  *
  1181  *
  1148  * @since 5.2.0
  1182  * @since 5.2.0
  1149  *
  1183  *
  1150  * @global WP_Query $wp_query Global WP_Query instance.
  1184  * @global WP_Query $wp_query WordPress Query object.
  1151  *
  1185  *
  1152  * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  1186  * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  1153  * @return array|bool Elements of post, or false on failure.
  1187  * @return array|bool Elements of post, or false on failure.
  1154  */
  1188  */
  1155 function generate_postdata( $post ) {
  1189 function generate_postdata( $post ) {