diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php
index 75c93d6aa1..b2b6809e29 100644
a
|
b
|
function generate_postdata( $post ) { |
1161 | 1161 | |
1162 | 1162 | return false; |
1163 | 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * Simplifies the WordPress loop. |
| 1167 | * |
| 1168 | * @since 5.x |
| 1169 | * |
| 1170 | * @global WP_Query $wp_query WordPress Query object. |
| 1171 | * |
| 1172 | * @param WP_Query|WP_Post[] $iterable WordPress query object or an array of posts. |
| 1173 | * |
| 1174 | * @return Generator |
| 1175 | */ |
| 1176 | function wp_loop( $iterable = null ) { |
| 1177 | |
| 1178 | if ( null === $iterable ) { |
| 1179 | $iterable = $GLOBALS['wp_query']; |
| 1180 | } |
| 1181 | |
| 1182 | $posts = $iterable; |
| 1183 | |
| 1184 | if ( is_object( $iterable ) && property_exists( $iterable, 'posts' ) ) { |
| 1185 | $posts = $iterable->posts; |
| 1186 | } |
| 1187 | |
| 1188 | if ( ! is_array( $posts ) ) { |
| 1189 | /* translators: Data type */ |
| 1190 | _doing_it_wrong( __FUNCTION__, sprintf( __( 'Expected an array, received %s instead.' ), gettype( $posts ) ), '3.1.0' ); |
| 1191 | |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
| 1195 | global $post; |
| 1196 | |
| 1197 | // Save the global post object so we can restore it later |
| 1198 | $save_post = $post; |
| 1199 | |
| 1200 | try { |
| 1201 | foreach ( $posts as $post ) { |
| 1202 | |
| 1203 | setup_postdata( $post ); |
| 1204 | yield $post; |
| 1205 | |
| 1206 | } |
| 1207 | } finally { |
| 1208 | |
| 1209 | wp_reset_postdata(); |
| 1210 | // Restore the global post object |
| 1211 | $post = $save_post; |
| 1212 | |
| 1213 | } |
| 1214 | } |