You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

942 lines
23 KiB

  1. <?php
  2. /**
  3. * WordPress Query API
  4. *
  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.
  7. *
  8. * @link https://codex.wordpress.org/The_Loop More information on The Loop.
  9. *
  10. * @package WordPress
  11. * @subpackage Query
  12. */
  13. /**
  14. * Retrieve variable in the WP_Query class.
  15. *
  16. * @since 1.5.0
  17. * @since 3.9.0 The `$default` argument was introduced.
  18. *
  19. * @global WP_Query $wp_query Global WP_Query instance.
  20. *
  21. * @param string $var The variable key to retrieve.
  22. * @param mixed $default Optional. Value to return if the query variable is not set. Default empty.
  23. * @return mixed Contents of the query variable.
  24. */
  25. function get_query_var( $var, $default = '' ) {
  26. global $wp_query;
  27. return $wp_query->get( $var, $default );
  28. }
  29. /**
  30. * Retrieve the currently-queried object.
  31. *
  32. * Wrapper for WP_Query::get_queried_object().
  33. *
  34. * @since 3.1.0
  35. * @access public
  36. *
  37. * @global WP_Query $wp_query Global WP_Query instance.
  38. *
  39. * @return object Queried object.
  40. */
  41. function get_queried_object() {
  42. global $wp_query;
  43. return $wp_query->get_queried_object();
  44. }
  45. /**
  46. * Retrieve ID of the current queried object.
  47. *
  48. * Wrapper for WP_Query::get_queried_object_id().
  49. *
  50. * @since 3.1.0
  51. *
  52. * @global WP_Query $wp_query Global WP_Query instance.
  53. *
  54. * @return int ID of the queried object.
  55. */
  56. function get_queried_object_id() {
  57. global $wp_query;
  58. return $wp_query->get_queried_object_id();
  59. }
  60. /**
  61. * Set query variable.
  62. *
  63. * @since 2.2.0
  64. *
  65. * @global WP_Query $wp_query Global WP_Query instance.
  66. *
  67. * @param string $var Query variable key.
  68. * @param mixed $value Query variable value.
  69. */
  70. function set_query_var( $var, $value ) {
  71. global $wp_query;
  72. $wp_query->set( $var, $value );
  73. }
  74. /**
  75. * Sets up The Loop with query parameters.
  76. *
  77. * Note: This function will completely override the main query and isn't intended for use
  78. * by plugins or themes. Its overly-simplistic approach to modifying the main query can be
  79. * problematic and should be avoided wherever possible. In most cases, there are better,
  80. * more performant options for modifying the main query such as via the {@see 'pre_get_posts'}
  81. * action within WP_Query.
  82. *
  83. * This must not be used within the WordPress Loop.
  84. *
  85. * @since 1.5.0
  86. *
  87. * @global WP_Query $wp_query Global WP_Query instance.
  88. *
  89. * @param array|string $query Array or string of WP_Query arguments.
  90. * @return array List of post objects.
  91. */
  92. function query_posts($query) {
  93. $GLOBALS['wp_query'] = new WP_Query();
  94. return $GLOBALS['wp_query']->query($query);
  95. }
  96. /**
  97. * Destroys the previous query and sets up a new query.
  98. *
  99. * This should be used after query_posts() and before another query_posts().
  100. * This will remove obscure bugs that occur when the previous WP_Query object
  101. * is not destroyed properly before another is set up.
  102. *
  103. * @since 2.3.0
  104. *
  105. * @global WP_Query $wp_query Global WP_Query instance.
  106. * @global WP_Query $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
  107. */
  108. function wp_reset_query() {
  109. $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
  110. wp_reset_postdata();
  111. }
  112. /**
  113. * After looping through a separate query, this function restores
  114. * the $post global to the current post in the main query.
  115. *
  116. * @since 3.0.0
  117. *
  118. * @global WP_Query $wp_query Global WP_Query instance.
  119. */
  120. function wp_reset_postdata() {
  121. global $wp_query;
  122. if ( isset( $wp_query ) ) {
  123. $wp_query->reset_postdata();
  124. }
  125. }
  126. /*
  127. * Query type checks.
  128. */
  129. /**
  130. * Is the query for an existing archive page?
  131. *
  132. * Month, Year, Category, Author, Post Type archive...
  133. *
  134. * @since 1.5.0
  135. *
  136. * @global WP_Query $wp_query Global WP_Query instance.
  137. *
  138. * @return bool
  139. */
  140. function is_archive() {
  141. global $wp_query;
  142. if ( ! isset( $wp_query ) ) {
  143. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  144. return false;
  145. }
  146. return $wp_query->is_archive();
  147. }
  148. /**
  149. * Is the query for an existing post type archive page?
  150. *
  151. * @since 3.1.0
  152. *
  153. * @global WP_Query $wp_query Global WP_Query instance.
  154. *
  155. * @param string|array $post_types Optional. Post type or array of posts types to check against.
  156. * @return bool
  157. */
  158. function is_post_type_archive( $post_types = '' ) {
  159. global $wp_query;
  160. if ( ! isset( $wp_query ) ) {
  161. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  162. return false;
  163. }
  164. return $wp_query->is_post_type_archive( $post_types );
  165. }
  166. /**
  167. * Is the query for an existing attachment page?
  168. *
  169. * @since 2.0.0
  170. *
  171. * @global WP_Query $wp_query Global WP_Query instance.
  172. *
  173. * @param int|string|array|object $attachment Attachment ID, title, slug, or array of such.
  174. * @return bool
  175. */
  176. function is_attachment( $attachment = '' ) {
  177. global $wp_query;
  178. if ( ! isset( $wp_query ) ) {
  179. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  180. return false;
  181. }
  182. return $wp_query->is_attachment( $attachment );
  183. }
  184. /**
  185. * Is the query for an existing author archive page?
  186. *
  187. * If the $author parameter is specified, this function will additionally
  188. * check if the query is for one of the authors specified.
  189. *
  190. * @since 1.5.0
  191. *
  192. * @global WP_Query $wp_query Global WP_Query instance.
  193. *
  194. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  195. * @return bool
  196. */
  197. function is_author( $author = '' ) {
  198. global $wp_query;
  199. if ( ! isset( $wp_query ) ) {
  200. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  201. return false;
  202. }
  203. return $wp_query->is_author( $author );
  204. }
  205. /**
  206. * Is the query for an existing category archive page?
  207. *
  208. * If the $category parameter is specified, this function will additionally
  209. * check if the query is for one of the categories specified.
  210. *
  211. * @since 1.5.0
  212. *
  213. * @global WP_Query $wp_query Global WP_Query instance.
  214. *
  215. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  216. * @return bool
  217. */
  218. function is_category( $category = '' ) {
  219. global $wp_query;
  220. if ( ! isset( $wp_query ) ) {
  221. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  222. return false;
  223. }
  224. return $wp_query->is_category( $category );
  225. }
  226. /**
  227. * Is the query for an existing tag archive page?
  228. *
  229. * If the $tag parameter is specified, this function will additionally
  230. * check if the query is for one of the tags specified.
  231. *
  232. * @since 2.3.0
  233. *
  234. * @global WP_Query $wp_query Global WP_Query instance.
  235. *
  236. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  237. * @return bool
  238. */
  239. function is_tag( $tag = '' ) {
  240. global $wp_query;
  241. if ( ! isset( $wp_query ) ) {
  242. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  243. return false;
  244. }
  245. return $wp_query->is_tag( $tag );
  246. }
  247. /**
  248. * Is the query for an existing custom taxonomy archive page?
  249. *
  250. * If the $taxonomy parameter is specified, this function will additionally
  251. * check if the query is for that specific $taxonomy.
  252. *
  253. * If the $term parameter is specified in addition to the $taxonomy parameter,
  254. * this function will additionally check if the query is for one of the terms
  255. * specified.
  256. *
  257. * @since 2.5.0
  258. *
  259. * @global WP_Query $wp_query Global WP_Query instance.
  260. *
  261. * @param string|array $taxonomy Optional. Taxonomy slug or slugs.
  262. * @param int|string|array $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  263. * @return bool True for custom taxonomy archive pages, false for built-in taxonomies (category and tag archives).
  264. */
  265. function is_tax( $taxonomy = '', $term = '' ) {
  266. global $wp_query;
  267. if ( ! isset( $wp_query ) ) {
  268. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  269. return false;
  270. }
  271. return $wp_query->is_tax( $taxonomy, $term );
  272. }
  273. /**
  274. * Is the query for an existing date archive?
  275. *
  276. * @since 1.5.0
  277. *
  278. * @global WP_Query $wp_query Global WP_Query instance.
  279. *
  280. * @return bool
  281. */
  282. function is_date() {
  283. global $wp_query;
  284. if ( ! isset( $wp_query ) ) {
  285. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  286. return false;
  287. }
  288. return $wp_query->is_date();
  289. }
  290. /**
  291. * Is the query for an existing day archive?
  292. *
  293. * @since 1.5.0
  294. *
  295. * @global WP_Query $wp_query Global WP_Query instance.
  296. *
  297. * @return bool
  298. */
  299. function is_day() {
  300. global $wp_query;
  301. if ( ! isset( $wp_query ) ) {
  302. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  303. return false;
  304. }
  305. return $wp_query->is_day();
  306. }
  307. /**
  308. * Is the query for a feed?
  309. *
  310. * @since 1.5.0
  311. *
  312. * @global WP_Query $wp_query Global WP_Query instance.
  313. *
  314. * @param string|array $feeds Optional feed types to check.
  315. * @return bool
  316. */
  317. function is_feed( $feeds = '' ) {
  318. global $wp_query;
  319. if ( ! isset( $wp_query ) ) {
  320. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  321. return false;
  322. }
  323. return $wp_query->is_feed( $feeds );
  324. }
  325. /**
  326. * Is the query for a comments feed?
  327. *
  328. * @since 3.0.0
  329. *
  330. * @global WP_Query $wp_query Global WP_Query instance.
  331. *
  332. * @return bool
  333. */
  334. function is_comment_feed() {
  335. global $wp_query;
  336. if ( ! isset( $wp_query ) ) {
  337. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  338. return false;
  339. }
  340. return $wp_query->is_comment_feed();
  341. }
  342. /**
  343. * Is the query for the front page of the site?
  344. *
  345. * This is for what is displayed at your site's main URL.
  346. *
  347. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  348. *
  349. * If you set a static page for the front page of your site, this function will return
  350. * true when viewing that page.
  351. *
  352. * Otherwise the same as @see is_home()
  353. *
  354. * @since 2.5.0
  355. *
  356. * @global WP_Query $wp_query Global WP_Query instance.
  357. *
  358. * @return bool True, if front of site.
  359. */
  360. function is_front_page() {
  361. global $wp_query;
  362. if ( ! isset( $wp_query ) ) {
  363. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  364. return false;
  365. }
  366. return $wp_query->is_front_page();
  367. }
  368. /**
  369. * Determines if the query is for the blog homepage.
  370. *
  371. * The blog homepage is the page that shows the time-based blog content of the site.
  372. *
  373. * is_home() is dependent on the site's "Front page displays" Reading Settings 'show_on_front'
  374. * and 'page_for_posts'.
  375. *
  376. * If a static page is set for the front page of the site, this function will return true only
  377. * on the page you set as the "Posts page".
  378. *
  379. * @since 1.5.0
  380. *
  381. * @see is_front_page()
  382. * @global WP_Query $wp_query Global WP_Query instance.
  383. *
  384. * @return bool True if blog view homepage, otherwise false.
  385. */
  386. function is_home() {
  387. global $wp_query;
  388. if ( ! isset( $wp_query ) ) {
  389. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  390. return false;
  391. }
  392. return $wp_query->is_home();
  393. }
  394. /**
  395. * Is the query for an existing month archive?
  396. *
  397. * @since 1.5.0
  398. *
  399. * @global WP_Query $wp_query Global WP_Query instance.
  400. *
  401. * @return bool
  402. */
  403. function is_month() {
  404. global $wp_query;
  405. if ( ! isset( $wp_query ) ) {
  406. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  407. return false;
  408. }
  409. return $wp_query->is_month();
  410. }
  411. /**
  412. * Is the query for an existing single page?
  413. *
  414. * If the $page parameter is specified, this function will additionally
  415. * check if the query is for one of the pages specified.
  416. *
  417. * @see is_single()
  418. * @see is_singular()
  419. *
  420. * @since 1.5.0
  421. *
  422. * @global WP_Query $wp_query Global WP_Query instance.
  423. *
  424. * @param int|string|array $page Optional. Page ID, title, slug, or array of such. Default empty.
  425. * @return bool Whether the query is for an existing single page.
  426. */
  427. function is_page( $page = '' ) {
  428. global $wp_query;
  429. if ( ! isset( $wp_query ) ) {
  430. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  431. return false;
  432. }
  433. return $wp_query->is_page( $page );
  434. }
  435. /**
  436. * Is the query for paged result and not for the first page?
  437. *
  438. * @since 1.5.0
  439. *
  440. * @global WP_Query $wp_query Global WP_Query instance.
  441. *
  442. * @return bool
  443. */
  444. function is_paged() {
  445. global $wp_query;
  446. if ( ! isset( $wp_query ) ) {
  447. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  448. return false;
  449. }
  450. return $wp_query->is_paged();
  451. }
  452. /**
  453. * Is the query for a post or page preview?
  454. *
  455. * @since 2.0.0
  456. *
  457. * @global WP_Query $wp_query Global WP_Query instance.
  458. *
  459. * @return bool
  460. */
  461. function is_preview() {
  462. global $wp_query;
  463. if ( ! isset( $wp_query ) ) {
  464. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  465. return false;
  466. }
  467. return $wp_query->is_preview();
  468. }
  469. /**
  470. * Is the query for the robots file?
  471. *
  472. * @since 2.1.0
  473. *
  474. * @global WP_Query $wp_query Global WP_Query instance.
  475. *
  476. * @return bool
  477. */
  478. function is_robots() {
  479. global $wp_query;
  480. if ( ! isset( $wp_query ) ) {
  481. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  482. return false;
  483. }
  484. return $wp_query->is_robots();
  485. }
  486. /**
  487. * Is the query for a search?
  488. *
  489. * @since 1.5.0
  490. *
  491. * @global WP_Query $wp_query Global WP_Query instance.
  492. *
  493. * @return bool
  494. */
  495. function is_search() {
  496. global $wp_query;
  497. if ( ! isset( $wp_query ) ) {
  498. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  499. return false;
  500. }
  501. return $wp_query->is_search();
  502. }
  503. /**
  504. * Is the query for an existing single post?
  505. *
  506. * Works for any post type, except attachments and pages
  507. *
  508. * If the $post parameter is specified, this function will additionally
  509. * check if the query is for one of the Posts specified.
  510. *
  511. * @see is_page()
  512. * @see is_singular()
  513. *
  514. * @since 1.5.0
  515. *
  516. * @global WP_Query $wp_query Global WP_Query instance.
  517. *
  518. * @param int|string|array $post Optional. Post ID, title, slug, or array of such. Default empty.
  519. * @return bool Whether the query is for an existing single post.
  520. */
  521. function is_single( $post = '' ) {
  522. global $wp_query;
  523. if ( ! isset( $wp_query ) ) {
  524. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  525. return false;
  526. }
  527. return $wp_query->is_single( $post );
  528. }
  529. /**
  530. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  531. *
  532. * If the $post_types parameter is specified, this function will additionally
  533. * check if the query is for one of the Posts Types specified.
  534. *
  535. * @see is_page()
  536. * @see is_single()
  537. *
  538. * @since 1.5.0
  539. *
  540. * @global WP_Query $wp_query Global WP_Query instance.
  541. *
  542. * @param string|array $post_types Optional. Post type or array of post types. Default empty.
  543. * @return bool Whether the query is for an existing single post of any of the given post types.
  544. */
  545. function is_singular( $post_types = '' ) {
  546. global $wp_query;
  547. if ( ! isset( $wp_query ) ) {
  548. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  549. return false;
  550. }
  551. return $wp_query->is_singular( $post_types );
  552. }
  553. /**
  554. * Is the query for a specific time?
  555. *
  556. * @since 1.5.0
  557. *
  558. * @global WP_Query $wp_query Global WP_Query instance.
  559. *
  560. * @return bool
  561. */
  562. function is_time() {
  563. global $wp_query;
  564. if ( ! isset( $wp_query ) ) {
  565. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  566. return false;
  567. }
  568. return $wp_query->is_time();
  569. }
  570. /**
  571. * Is the query for a trackback endpoint call?
  572. *
  573. * @since 1.5.0
  574. *
  575. * @global WP_Query $wp_query Global WP_Query instance.
  576. *
  577. * @return bool
  578. */
  579. function is_trackback() {
  580. global $wp_query;
  581. if ( ! isset( $wp_query ) ) {
  582. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  583. return false;
  584. }
  585. return $wp_query->is_trackback();
  586. }
  587. /**
  588. * Is the query for an existing year archive?
  589. *
  590. * @since 1.5.0
  591. *
  592. * @global WP_Query $wp_query Global WP_Query instance.
  593. *
  594. * @return bool
  595. */
  596. function is_year() {
  597. global $wp_query;
  598. if ( ! isset( $wp_query ) ) {
  599. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  600. return false;
  601. }
  602. return $wp_query->is_year();
  603. }
  604. /**
  605. * Is the query a 404 (returns no results)?
  606. *
  607. * @since 1.5.0
  608. *
  609. * @global WP_Query $wp_query Global WP_Query instance.
  610. *
  611. * @return bool
  612. */
  613. function is_404() {
  614. global $wp_query;
  615. if ( ! isset( $wp_query ) ) {
  616. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  617. return false;
  618. }
  619. return $wp_query->is_404();
  620. }
  621. /**
  622. * Is the query for an embedded post?
  623. *
  624. * @since 4.4.0
  625. *
  626. * @global WP_Query $wp_query Global WP_Query instance.
  627. *
  628. * @return bool Whether we're in an embedded post or not.
  629. */
  630. function is_embed() {
  631. global $wp_query;
  632. if ( ! isset( $wp_query ) ) {
  633. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  634. return false;
  635. }
  636. return $wp_query->is_embed();
  637. }
  638. /**
  639. * Is the query the main query?
  640. *
  641. * @since 3.3.0
  642. *
  643. * @global WP_Query $wp_query Global WP_Query instance.
  644. *
  645. * @return bool
  646. */
  647. function is_main_query() {
  648. if ( 'pre_get_posts' === current_filter() ) {
  649. $message = sprintf(
  650. /* translators: 1: pre_get_posts 2: WP_Query->is_main_query() 3: is_main_query() 4: link to codex is_main_query() page. */
  651. __( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
  652. '<code>pre_get_posts</code>',
  653. '<code>WP_Query->is_main_query()</code>',
  654. '<code>is_main_query()</code>',
  655. __( 'https://codex.wordpress.org/Function_Reference/is_main_query' )
  656. );
  657. _doing_it_wrong( __FUNCTION__, $message, '3.7.0' );
  658. }
  659. global $wp_query;
  660. return $wp_query->is_main_query();
  661. }
  662. /*
  663. * The Loop. Post loop control.
  664. */
  665. /**
  666. * Whether current WordPress query has results to loop over.
  667. *
  668. * @since 1.5.0
  669. *
  670. * @global WP_Query $wp_query Global WP_Query instance.
  671. *
  672. * @return bool
  673. */
  674. function have_posts() {
  675. global $wp_query;
  676. return $wp_query->have_posts();
  677. }
  678. /**
  679. * Whether the caller is in the Loop.
  680. *
  681. * @since 2.0.0
  682. *
  683. * @global WP_Query $wp_query Global WP_Query instance.
  684. *
  685. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  686. */
  687. function in_the_loop() {
  688. global $wp_query;
  689. return $wp_query->in_the_loop;
  690. }
  691. /**
  692. * Rewind the loop posts.
  693. *
  694. * @since 1.5.0
  695. *
  696. * @global WP_Query $wp_query Global WP_Query instance.
  697. */
  698. function rewind_posts() {
  699. global $wp_query;
  700. $wp_query->rewind_posts();
  701. }
  702. /**
  703. * Iterate the post index in the loop.
  704. *
  705. * @since 1.5.0
  706. *
  707. * @global WP_Query $wp_query Global WP_Query instance.
  708. */
  709. function the_post() {
  710. global $wp_query;
  711. $wp_query->the_post();
  712. }
  713. /*
  714. * Comments loop.
  715. */
  716. /**
  717. * Whether there are comments to loop over.
  718. *
  719. * @since 2.2.0
  720. *
  721. * @global WP_Query $wp_query Global WP_Query instance.
  722. *
  723. * @return bool
  724. */
  725. function have_comments() {
  726. global $wp_query;
  727. return $wp_query->have_comments();
  728. }
  729. /**
  730. * Iterate comment index in the comment loop.
  731. *
  732. * @since 2.2.0
  733. *
  734. * @global WP_Query $wp_query Global WP_Query instance.
  735. *
  736. * @return object
  737. */
  738. function the_comment() {
  739. global $wp_query;
  740. return $wp_query->the_comment();
  741. }
  742. /**
  743. * Redirect old slugs to the correct permalink.
  744. *
  745. * Attempts to find the current slug from the past slugs.
  746. *
  747. * @since 2.1.0
  748. *
  749. * @global wpdb $wpdb WordPress database abstraction object.
  750. */
  751. function wp_old_slug_redirect() {
  752. if ( is_404() && '' !== get_query_var( 'name' ) ) {
  753. global $wpdb;
  754. // Guess the current post_type based on the query vars.
  755. if ( get_query_var( 'post_type' ) ) {
  756. $post_type = get_query_var( 'post_type' );
  757. } elseif ( get_query_var( 'attachment' ) ) {
  758. $post_type = 'attachment';
  759. } elseif ( get_query_var( 'pagename' ) ) {
  760. $post_type = 'page';
  761. } else {
  762. $post_type = 'post';
  763. }
  764. if ( is_array( $post_type ) ) {
  765. if ( count( $post_type ) > 1 ) {
  766. return;
  767. }
  768. $post_type = reset( $post_type );
  769. }
  770. // Do not attempt redirect for hierarchical post types
  771. if ( is_post_type_hierarchical( $post_type ) ) {
  772. return;
  773. }
  774. $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' ) );
  775. // if year, monthnum, or day have been specified, make our query more precise
  776. // just in case there are multiple identical _wp_old_slug values
  777. if ( get_query_var( 'year' ) ) {
  778. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var( 'year' ) );
  779. }
  780. if ( get_query_var( 'monthnum' ) ) {
  781. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
  782. }
  783. if ( get_query_var( 'day' ) ) {
  784. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
  785. }
  786. $id = (int) $wpdb->get_var( $query );
  787. if ( ! $id ) {
  788. return;
  789. }
  790. $link = get_permalink( $id );
  791. if ( get_query_var( 'paged' ) > 1 ) {
  792. $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) );
  793. } elseif( is_embed() ) {
  794. $link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
  795. }
  796. /**
  797. * Filters the old slug redirect URL.
  798. *
  799. * @since 4.4.0
  800. *
  801. * @param string $link The redirect URL.
  802. */
  803. $link = apply_filters( 'old_slug_redirect_url', $link );
  804. if ( ! $link ) {
  805. return;
  806. }
  807. wp_redirect( $link, 301 ); // Permanent redirect
  808. exit;
  809. }
  810. }
  811. /**
  812. * Set up global post data.
  813. *
  814. * @since 1.5.0
  815. * @since 4.4.0 Added the ability to pass a post ID to `$post`.
  816. *
  817. * @global WP_Query $wp_query Global WP_Query instance.
  818. *
  819. * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  820. * @return bool True when finished.
  821. */
  822. function setup_postdata( $post ) {
  823. global $wp_query;
  824. if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
  825. return $wp_query->setup_postdata( $post );
  826. }
  827. return false;
  828. }