wp/wp-includes/query.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 21 48c4eec2b7e6
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    10  * @package WordPress
    10  * @package WordPress
    11  * @subpackage Query
    11  * @subpackage Query
    12  */
    12  */
    13 
    13 
    14 /**
    14 /**
    15  * Retrieve variable in the WP_Query class.
    15  * Retrieves the value of a query 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 WordPress Query object.
    20  * @global WP_Query $wp_query WordPress Query object.
    27 	global $wp_query;
    27 	global $wp_query;
    28 	return $wp_query->get( $var, $default );
    28 	return $wp_query->get( $var, $default );
    29 }
    29 }
    30 
    30 
    31 /**
    31 /**
    32  * Retrieve the currently-queried object.
    32  * Retrieves the currently queried object.
    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 WordPress Query object.
    38  * @global WP_Query $wp_query WordPress Query object.
    39  *
    39  *
    40  * @return object Queried object.
    40  * @return WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object.
    41  */
    41  */
    42 function get_queried_object() {
    42 function get_queried_object() {
    43 	global $wp_query;
    43 	global $wp_query;
    44 	return $wp_query->get_queried_object();
    44 	return $wp_query->get_queried_object();
    45 }
    45 }
    46 
    46 
    47 /**
    47 /**
    48  * Retrieve ID of the current queried object.
    48  * Retrieves the ID of the currently queried object.
    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  *
    59 	global $wp_query;
    59 	global $wp_query;
    60 	return $wp_query->get_queried_object_id();
    60 	return $wp_query->get_queried_object_id();
    61 }
    61 }
    62 
    62 
    63 /**
    63 /**
    64  * Set query variable.
    64  * Sets the value of a query variable in the WP_Query class.
    65  *
    65  *
    66  * @since 2.2.0
    66  * @since 2.2.0
    67  *
    67  *
    68  * @global WP_Query $wp_query WordPress Query object.
    68  * @global WP_Query $wp_query WordPress Query object.
    69  *
    69  *
   136  */
   136  */
   137 
   137 
   138 /**
   138 /**
   139  * Determines whether the query is for an existing archive page.
   139  * Determines whether the query is for an existing archive page.
   140  *
   140  *
   141  * Month, Year, Category, Author, Post Type archive...
   141  * Archive pages include category, tag, author, date, custom post type,
   142  *
   142  * and custom taxonomy based archives.
   143  * For more information on this and similar theme functions, check out
   143  *
   144  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   144  * For more information on this and similar theme functions, check out
   145  * Conditional Tags} article in the Theme Developer Handbook.
   145  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   146  *
   146  * Conditional Tags} article in the Theme Developer Handbook.
   147  * @since 1.5.0
   147  *
   148  *
   148  * @since 1.5.0
       
   149  *
       
   150  * @see is_category()
       
   151  * @see is_tag()
       
   152  * @see is_author()
       
   153  * @see is_date()
       
   154  * @see is_post_type_archive()
       
   155  * @see is_tax()
   149  * @global WP_Query $wp_query WordPress Query object.
   156  * @global WP_Query $wp_query WordPress Query object.
   150  *
   157  *
   151  * @return bool Whether the query is for an existing archive page.
   158  * @return bool Whether the query is for an existing archive page.
   152  */
   159  */
   153 function is_archive() {
   160 function is_archive() {
   561  *
   568  *
   562  * For more information on this and similar theme functions, check out
   569  * For more information on this and similar theme functions, check out
   563  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   570  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   564  * Conditional Tags} article in the Theme Developer Handbook.
   571  * Conditional Tags} article in the Theme Developer Handbook.
   565  *
   572  *
       
   573  * @since 1.5.0
       
   574  *
   566  * @see is_single()
   575  * @see is_single()
   567  * @see is_singular()
   576  * @see is_singular()
   568  *
       
   569  * @since 1.5.0
       
   570  *
       
   571  * @global WP_Query $wp_query WordPress Query object.
   577  * @global WP_Query $wp_query WordPress Query object.
   572  *
   578  *
   573  * @param int|string|int[]|string[] $page Optional. Page ID, title, slug, or array of such
   579  * @param int|string|int[]|string[] $page Optional. Page ID, title, slug, or array of such
   574  *                                        to check against. Default empty.
   580  *                                        to check against. Default empty.
   575  * @return bool Whether the query is for an existing single page.
   581  * @return bool Whether the query is for an existing single page.
   707  *
   713  *
   708  * For more information on this and similar theme functions, check out
   714  * For more information on this and similar theme functions, check out
   709  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   715  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   710  * Conditional Tags} article in the Theme Developer Handbook.
   716  * Conditional Tags} article in the Theme Developer Handbook.
   711  *
   717  *
       
   718  * @since 1.5.0
       
   719  *
   712  * @see is_page()
   720  * @see is_page()
   713  * @see is_singular()
   721  * @see is_singular()
   714  *
       
   715  * @since 1.5.0
       
   716  *
       
   717  * @global WP_Query $wp_query WordPress Query object.
   722  * @global WP_Query $wp_query WordPress Query object.
   718  *
   723  *
   719  * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such
   724  * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such
   720  *                                        to check against. Default empty.
   725  *                                        to check against. Default empty.
   721  * @return bool Whether the query is for an existing single post.
   726  * @return bool Whether the query is for an existing single post.
   740  *
   745  *
   741  * For more information on this and similar theme functions, check out
   746  * For more information on this and similar theme functions, check out
   742  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   747  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
   743  * Conditional Tags} article in the Theme Developer Handbook.
   748  * Conditional Tags} article in the Theme Developer Handbook.
   744  *
   749  *
       
   750  * @since 1.5.0
       
   751  *
   745  * @see is_page()
   752  * @see is_page()
   746  * @see is_single()
   753  * @see is_single()
   747  *
       
   748  * @since 1.5.0
       
   749  *
       
   750  * @global WP_Query $wp_query WordPress Query object.
   754  * @global WP_Query $wp_query WordPress Query object.
   751  *
   755  *
   752  * @param string|string[] $post_types Optional. Post type or array of post types
   756  * @param string|string[] $post_types Optional. Post type or array of post types
   753  *                                    to check against. Default empty.
   757  *                                    to check against. Default empty.
   754  * @return bool Whether the query is for an existing single post
   758  * @return bool Whether the query is for an existing single post
   893  * @global WP_Query $wp_query WordPress Query object.
   897  * @global WP_Query $wp_query WordPress Query object.
   894  *
   898  *
   895  * @return bool Whether the query is the main query.
   899  * @return bool Whether the query is the main query.
   896  */
   900  */
   897 function is_main_query() {
   901 function is_main_query() {
       
   902 	global $wp_query;
       
   903 
   898 	if ( 'pre_get_posts' === current_filter() ) {
   904 	if ( 'pre_get_posts' === current_filter() ) {
   899 		$message = sprintf(
   905 		_doing_it_wrong(
   900 			/* translators: 1: pre_get_posts, 2: WP_Query->is_main_query(), 3: is_main_query(), 4: Link to codex is_main_query() page. */
   906 			__FUNCTION__,
   901 			__( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
   907 			sprintf(
   902 			'<code>pre_get_posts</code>',
   908 				/* translators: 1: pre_get_posts, 2: WP_Query->is_main_query(), 3: is_main_query(), 4: Documentation URL. */
   903 			'<code>WP_Query->is_main_query()</code>',
   909 				__( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
   904 			'<code>is_main_query()</code>',
   910 				'<code>pre_get_posts</code>',
   905 			__( 'https://codex.wordpress.org/Function_Reference/is_main_query' )
   911 				'<code>WP_Query->is_main_query()</code>',
       
   912 				'<code>is_main_query()</code>',
       
   913 				__( 'https://developer.wordpress.org/reference/functions/is_main_query/' )
       
   914 			),
       
   915 			'3.7.0'
   906 		);
   916 		);
   907 		_doing_it_wrong( __FUNCTION__, $message, '3.7.0' );
   917 	}
   908 	}
   918 
   909 
       
   910 	global $wp_query;
       
   911 	return $wp_query->is_main_query();
   919 	return $wp_query->is_main_query();
   912 }
   920 }
   913 
   921 
   914 /*
   922 /*
   915  * The Loop. Post loop control.
   923  * The Loop. Post loop control.
   994  *
  1002  *
   995  * @since 2.2.0
  1003  * @since 2.2.0
   996  *
  1004  *
   997  * @global WP_Query $wp_query WordPress Query object.
  1005  * @global WP_Query $wp_query WordPress Query object.
   998  *
  1006  *
   999  * @return object
  1007  * @return null
  1000  */
  1008  */
  1001 function the_comment() {
  1009 function the_comment() {
  1002 	global $wp_query;
  1010 	global $wp_query;
  1003 	return $wp_query->the_comment();
  1011 	return $wp_query->the_comment();
  1004 }
  1012 }
  1081 }
  1089 }
  1082 
  1090 
  1083 /**
  1091 /**
  1084  * Find the post ID for redirecting an old slug.
  1092  * Find the post ID for redirecting an old slug.
  1085  *
  1093  *
  1086  * @see wp_old_slug_redirect()
       
  1087  *
       
  1088  * @since 4.9.3
  1094  * @since 4.9.3
  1089  * @access private
  1095  * @access private
  1090  *
  1096  *
       
  1097  * @see wp_old_slug_redirect()
  1091  * @global wpdb $wpdb WordPress database abstraction object.
  1098  * @global wpdb $wpdb WordPress database abstraction object.
  1092  *
  1099  *
  1093  * @param string $post_type The current post type based on the query vars.
  1100  * @param string $post_type The current post type based on the query vars.
  1094  * @return int The Post ID.
  1101  * @return int The Post ID.
  1095  */
  1102  */
  1116 }
  1123 }
  1117 
  1124 
  1118 /**
  1125 /**
  1119  * Find the post ID for redirecting an old date.
  1126  * Find the post ID for redirecting an old date.
  1120  *
  1127  *
  1121  * @see wp_old_slug_redirect()
       
  1122  *
       
  1123  * @since 4.9.3
  1128  * @since 4.9.3
  1124  * @access private
  1129  * @access private
  1125  *
  1130  *
       
  1131  * @see wp_old_slug_redirect()
  1126  * @global wpdb $wpdb WordPress database abstraction object.
  1132  * @global wpdb $wpdb WordPress database abstraction object.
  1127  *
  1133  *
  1128  * @param string $post_type The current post type based on the query vars.
  1134  * @param string $post_type The current post type based on the query vars.
  1129  * @return int The Post ID.
  1135  * @return int The Post ID.
  1130  */
  1136  */
  1182  * @since 5.2.0
  1188  * @since 5.2.0
  1183  *
  1189  *
  1184  * @global WP_Query $wp_query WordPress Query object.
  1190  * @global WP_Query $wp_query WordPress Query object.
  1185  *
  1191  *
  1186  * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  1192  * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  1187  * @return array|bool Elements of post, or false on failure.
  1193  * @return array|false Elements of post, or false on failure.
  1188  */
  1194  */
  1189 function generate_postdata( $post ) {
  1195 function generate_postdata( $post ) {
  1190 	global $wp_query;
  1196 	global $wp_query;
  1191 
  1197 
  1192 	if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
  1198 	if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {