13 |
13 |
14 /** |
14 /** |
15 * Retrieves the value of a query 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_value` argument was introduced. |
19 * |
19 * |
20 * @global WP_Query $wp_query WordPress Query object. |
20 * @global WP_Query $wp_query WordPress Query object. |
21 * |
21 * |
22 * @param string $var The variable key to retrieve. |
22 * @param string $query_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_value Optional. Value to return if the query variable is not set. |
|
24 * Default empty string. |
24 * @return mixed Contents of the query variable. |
25 * @return mixed Contents of the query variable. |
25 */ |
26 */ |
26 function get_query_var( $var, $default = '' ) { |
27 function get_query_var( $query_var, $default_value = '' ) { |
27 global $wp_query; |
28 global $wp_query; |
28 return $wp_query->get( $var, $default ); |
29 return $wp_query->get( $query_var, $default_value ); |
29 } |
30 } |
30 |
31 |
31 /** |
32 /** |
32 * Retrieves the currently queried object. |
33 * Retrieves the currently queried object. |
33 * |
34 * |
65 * |
66 * |
66 * @since 2.2.0 |
67 * @since 2.2.0 |
67 * |
68 * |
68 * @global WP_Query $wp_query WordPress Query object. |
69 * @global WP_Query $wp_query WordPress Query object. |
69 * |
70 * |
70 * @param string $var Query variable key. |
71 * @param string $query_var Query variable key. |
71 * @param mixed $value Query variable value. |
72 * @param mixed $value Query variable value. |
72 */ |
73 */ |
73 function set_query_var( $var, $value ) { |
74 function set_query_var( $query_var, $value ) { |
74 global $wp_query; |
75 global $wp_query; |
75 $wp_query->set( $var, $value ); |
76 $wp_query->set( $query_var, $value ); |
76 } |
77 } |
77 |
78 |
78 /** |
79 /** |
79 * Sets up The Loop with query parameters. |
80 * Sets up The Loop with query parameters. |
80 * |
81 * |
448 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'. |
449 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'. |
449 * |
450 * |
450 * If you set a static page for the front page of your site, this function will return |
451 * If you set a static page for the front page of your site, this function will return |
451 * true when viewing that page. |
452 * true when viewing that page. |
452 * |
453 * |
453 * Otherwise the same as @see is_home() |
454 * Otherwise the same as {@see is_home()}. |
454 * |
455 * |
455 * For more information on this and similar theme functions, check out |
456 * For more information on this and similar theme functions, check out |
456 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
457 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
457 * Conditional Tags} article in the Theme Developer Handbook. |
458 * Conditional Tags} article in the Theme Developer Handbook. |
458 * |
459 * |
898 * |
899 * |
899 * @return bool Whether the query is the main query. |
900 * @return bool Whether the query is the main query. |
900 */ |
901 */ |
901 function is_main_query() { |
902 function is_main_query() { |
902 global $wp_query; |
903 global $wp_query; |
|
904 |
|
905 if ( ! isset( $wp_query ) ) { |
|
906 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '6.1.0' ); |
|
907 return false; |
|
908 } |
903 |
909 |
904 if ( 'pre_get_posts' === current_filter() ) { |
910 if ( 'pre_get_posts' === current_filter() ) { |
905 _doing_it_wrong( |
911 _doing_it_wrong( |
906 __FUNCTION__, |
912 __FUNCTION__, |
907 sprintf( |
913 sprintf( |
932 * |
938 * |
933 * @return bool True if posts are available, false if end of the loop. |
939 * @return bool True if posts are available, false if end of the loop. |
934 */ |
940 */ |
935 function have_posts() { |
941 function have_posts() { |
936 global $wp_query; |
942 global $wp_query; |
|
943 |
|
944 if ( ! isset( $wp_query ) ) { |
|
945 return false; |
|
946 } |
|
947 |
937 return $wp_query->have_posts(); |
948 return $wp_query->have_posts(); |
938 } |
949 } |
939 |
950 |
940 /** |
951 /** |
941 * Determines whether the caller is in the Loop. |
952 * Determines whether the caller is in the Loop. |
950 * |
961 * |
951 * @return bool True if caller is within loop, false if loop hasn't started or ended. |
962 * @return bool True if caller is within loop, false if loop hasn't started or ended. |
952 */ |
963 */ |
953 function in_the_loop() { |
964 function in_the_loop() { |
954 global $wp_query; |
965 global $wp_query; |
|
966 |
|
967 if ( ! isset( $wp_query ) ) { |
|
968 return false; |
|
969 } |
|
970 |
955 return $wp_query->in_the_loop; |
971 return $wp_query->in_the_loop; |
956 } |
972 } |
957 |
973 |
958 /** |
974 /** |
959 * Rewind the loop posts. |
975 * Rewind the loop posts. |
992 * |
1018 * |
993 * @return bool True if comments are available, false if no more comments. |
1019 * @return bool True if comments are available, false if no more comments. |
994 */ |
1020 */ |
995 function have_comments() { |
1021 function have_comments() { |
996 global $wp_query; |
1022 global $wp_query; |
|
1023 |
|
1024 if ( ! isset( $wp_query ) ) { |
|
1025 return false; |
|
1026 } |
|
1027 |
997 return $wp_query->have_comments(); |
1028 return $wp_query->have_comments(); |
998 } |
1029 } |
999 |
1030 |
1000 /** |
1031 /** |
1001 * Iterate comment index in the comment loop. |
1032 * Iterate comment index in the comment loop. |
1002 * |
1033 * |
1003 * @since 2.2.0 |
1034 * @since 2.2.0 |
1004 * |
1035 * |
1005 * @global WP_Query $wp_query WordPress Query object. |
1036 * @global WP_Query $wp_query WordPress Query object. |
1006 * |
|
1007 * @return null |
|
1008 */ |
1037 */ |
1009 function the_comment() { |
1038 function the_comment() { |
1010 global $wp_query; |
1039 global $wp_query; |
1011 return $wp_query->the_comment(); |
1040 |
|
1041 if ( ! isset( $wp_query ) ) { |
|
1042 return; |
|
1043 } |
|
1044 |
|
1045 $wp_query->the_comment(); |
1012 } |
1046 } |
1013 |
1047 |
1014 /** |
1048 /** |
1015 * Redirect old slugs to the correct permalink. |
1049 * Redirect old slugs to the correct permalink. |
1016 * |
1050 * |
1103 function _find_post_by_old_slug( $post_type ) { |
1137 function _find_post_by_old_slug( $post_type ) { |
1104 global $wpdb; |
1138 global $wpdb; |
1105 |
1139 |
1106 $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' ) ); |
1140 $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' ) ); |
1107 |
1141 |
1108 // If year, monthnum, or day have been specified, make our query more precise |
1142 /* |
1109 // just in case there are multiple identical _wp_old_slug values. |
1143 * If year, monthnum, or day have been specified, make our query more precise |
|
1144 * just in case there are multiple identical _wp_old_slug values. |
|
1145 */ |
1110 if ( get_query_var( 'year' ) ) { |
1146 if ( get_query_var( 'year' ) ) { |
1111 $query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) ); |
1147 $query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) ); |
1112 } |
1148 } |
1113 if ( get_query_var( 'monthnum' ) ) { |
1149 if ( get_query_var( 'monthnum' ) ) { |
1114 $query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) ); |
1150 $query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) ); |
1115 } |
1151 } |
1116 if ( get_query_var( 'day' ) ) { |
1152 if ( get_query_var( 'day' ) ) { |
1117 $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); |
1153 $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); |
1118 } |
1154 } |
1119 |
1155 |
1120 $id = (int) $wpdb->get_var( $query ); |
1156 $key = md5( $query ); |
|
1157 $last_changed = wp_cache_get_last_changed( 'posts' ); |
|
1158 $cache_key = "find_post_by_old_slug:$key:$last_changed"; |
|
1159 $cache = wp_cache_get( $cache_key, 'post-queries' ); |
|
1160 if ( false !== $cache ) { |
|
1161 $id = $cache; |
|
1162 } else { |
|
1163 $id = (int) $wpdb->get_var( $query ); |
|
1164 wp_cache_set( $cache_key, $id, 'post-queries' ); |
|
1165 } |
1121 |
1166 |
1122 return $id; |
1167 return $id; |
1123 } |
1168 } |
1124 |
1169 |
1125 /** |
1170 /** |
1148 $date_query .= $wpdb->prepare( ' AND DAYOFMONTH(pm_date.meta_value) = %d', get_query_var( 'day' ) ); |
1193 $date_query .= $wpdb->prepare( ' AND DAYOFMONTH(pm_date.meta_value) = %d', get_query_var( 'day' ) ); |
1149 } |
1194 } |
1150 |
1195 |
1151 $id = 0; |
1196 $id = 0; |
1152 if ( $date_query ) { |
1197 if ( $date_query ) { |
1153 $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' ) ) ); |
1198 $query = $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' ) ); |
1154 |
1199 $key = md5( $query ); |
1155 if ( ! $id ) { |
1200 $last_changed = wp_cache_get_last_changed( 'posts' ); |
1156 // Check to see if an old slug matches the old date. |
1201 $cache_key = "find_post_by_old_date:$key:$last_changed"; |
1157 $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' ) ) ); |
1202 $cache = wp_cache_get( $cache_key, 'post-queries' ); |
|
1203 if ( false !== $cache ) { |
|
1204 $id = $cache; |
|
1205 } else { |
|
1206 $id = (int) $wpdb->get_var( $query ); |
|
1207 if ( ! $id ) { |
|
1208 // Check to see if an old slug matches the old date. |
|
1209 $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' ) ) ); |
|
1210 } |
|
1211 wp_cache_set( $cache_key, $id, 'post-queries' ); |
1158 } |
1212 } |
1159 } |
1213 } |
1160 |
1214 |
1161 return $id; |
1215 return $id; |
1162 } |
1216 } |