PageRenderTime 45ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/query.php

https://github.com/markjaquith/WordPress
PHP | 1203 lines | 388 code | 132 blank | 683 comment | 53 complexity | ea8b117fb3cb0c06d87b346d18f8a0c2 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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://developer.wordpress.org/themes/basics/the-loop/ More information on The Loop.
  9. *
  10. * @package WordPress
  11. * @subpackage Query
  12. */
  13. /**
  14. * Retrieves the value of a query 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 WordPress Query object.
  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. * Retrieves the currently queried object.
  31. *
  32. * Wrapper for WP_Query::get_queried_object().
  33. *
  34. * @since 3.1.0
  35. *
  36. * @global WP_Query $wp_query WordPress Query object.
  37. *
  38. * @return WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object.
  39. */
  40. function get_queried_object() {
  41. global $wp_query;
  42. return $wp_query->get_queried_object();
  43. }
  44. /**
  45. * Retrieves the ID of the currently queried object.
  46. *
  47. * Wrapper for WP_Query::get_queried_object_id().
  48. *
  49. * @since 3.1.0
  50. *
  51. * @global WP_Query $wp_query WordPress Query object.
  52. *
  53. * @return int ID of the queried object.
  54. */
  55. function get_queried_object_id() {
  56. global $wp_query;
  57. return $wp_query->get_queried_object_id();
  58. }
  59. /**
  60. * Sets the value of a query variable in the WP_Query class.
  61. *
  62. * @since 2.2.0
  63. *
  64. * @global WP_Query $wp_query WordPress Query object.
  65. *
  66. * @param string $var Query variable key.
  67. * @param mixed $value Query variable value.
  68. */
  69. function set_query_var( $var, $value ) {
  70. global $wp_query;
  71. $wp_query->set( $var, $value );
  72. }
  73. /**
  74. * Sets up The Loop with query parameters.
  75. *
  76. * Note: This function will completely override the main query and isn't intended for use
  77. * by plugins or themes. Its overly-simplistic approach to modifying the main query can be
  78. * problematic and should be avoided wherever possible. In most cases, there are better,
  79. * more performant options for modifying the main query such as via the {@see 'pre_get_posts'}
  80. * action within WP_Query.
  81. *
  82. * This must not be used within the WordPress Loop.
  83. *
  84. * @since 1.5.0
  85. *
  86. * @global WP_Query $wp_query WordPress Query object.
  87. *
  88. * @param array|string $query Array or string of WP_Query arguments.
  89. * @return WP_Post[]|int[] Array of post objects or post IDs.
  90. */
  91. function query_posts( $query ) {
  92. $GLOBALS['wp_query'] = new WP_Query();
  93. return $GLOBALS['wp_query']->query( $query );
  94. }
  95. /**
  96. * Destroys the previous query and sets up a new query.
  97. *
  98. * This should be used after query_posts() and before another query_posts().
  99. * This will remove obscure bugs that occur when the previous WP_Query object
  100. * is not destroyed properly before another is set up.
  101. *
  102. * @since 2.3.0
  103. *
  104. * @global WP_Query $wp_query WordPress Query object.
  105. * @global WP_Query $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
  106. */
  107. function wp_reset_query() {
  108. $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
  109. wp_reset_postdata();
  110. }
  111. /**
  112. * After looping through a separate query, this function restores
  113. * the $post global to the current post in the main query.
  114. *
  115. * @since 3.0.0
  116. *
  117. * @global WP_Query $wp_query WordPress Query object.
  118. */
  119. function wp_reset_postdata() {
  120. global $wp_query;
  121. if ( isset( $wp_query ) ) {
  122. $wp_query->reset_postdata();
  123. }
  124. }
  125. /*
  126. * Query type checks.
  127. */
  128. /**
  129. * Determines whether the query is for an existing archive page.
  130. *
  131. * Archive pages include category, tag, author, date, custom post type,
  132. * and custom taxonomy based archives.
  133. *
  134. * For more information on this and similar theme functions, check out
  135. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  136. * Conditional Tags} article in the Theme Developer Handbook.
  137. *
  138. * @since 1.5.0
  139. *
  140. * @see is_category()
  141. * @see is_tag()
  142. * @see is_author()
  143. * @see is_date()
  144. * @see is_post_type_archive()
  145. * @see is_tax()
  146. * @global WP_Query $wp_query WordPress Query object.
  147. *
  148. * @return bool Whether the query is for an existing archive page.
  149. */
  150. function is_archive() {
  151. global $wp_query;
  152. if ( ! isset( $wp_query ) ) {
  153. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  154. return false;
  155. }
  156. return $wp_query->is_archive();
  157. }
  158. /**
  159. * Determines whether the query is for an existing post type archive page.
  160. *
  161. * For more information on this and similar theme functions, check out
  162. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  163. * Conditional Tags} article in the Theme Developer Handbook.
  164. *
  165. * @since 3.1.0
  166. *
  167. * @global WP_Query $wp_query WordPress Query object.
  168. *
  169. * @param string|string[] $post_types Optional. Post type or array of posts types
  170. * to check against. Default empty.
  171. * @return bool Whether the query is for an existing post type archive page.
  172. */
  173. function is_post_type_archive( $post_types = '' ) {
  174. global $wp_query;
  175. if ( ! isset( $wp_query ) ) {
  176. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  177. return false;
  178. }
  179. return $wp_query->is_post_type_archive( $post_types );
  180. }
  181. /**
  182. * Determines whether the query is for an existing attachment page.
  183. *
  184. * For more information on this and similar theme functions, check out
  185. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  186. * Conditional Tags} article in the Theme Developer Handbook.
  187. *
  188. * @since 2.0.0
  189. *
  190. * @global WP_Query $wp_query WordPress Query object.
  191. *
  192. * @param int|string|int[]|string[] $attachment Optional. Attachment ID, title, slug, or array of such
  193. * to check against. Default empty.
  194. * @return bool Whether the query is for an existing attachment page.
  195. */
  196. function is_attachment( $attachment = '' ) {
  197. global $wp_query;
  198. if ( ! isset( $wp_query ) ) {
  199. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  200. return false;
  201. }
  202. return $wp_query->is_attachment( $attachment );
  203. }
  204. /**
  205. * Determines whether the query is for an existing author archive page.
  206. *
  207. * If the $author parameter is specified, this function will additionally
  208. * check if the query is for one of the authors specified.
  209. *
  210. * For more information on this and similar theme functions, check out
  211. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  212. * Conditional Tags} article in the Theme Developer Handbook.
  213. *
  214. * @since 1.5.0
  215. *
  216. * @global WP_Query $wp_query WordPress Query object.
  217. *
  218. * @param int|string|int[]|string[] $author Optional. User ID, nickname, nicename, or array of such
  219. * to check against. Default empty.
  220. * @return bool Whether the query is for an existing author archive page.
  221. */
  222. function is_author( $author = '' ) {
  223. global $wp_query;
  224. if ( ! isset( $wp_query ) ) {
  225. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  226. return false;
  227. }
  228. return $wp_query->is_author( $author );
  229. }
  230. /**
  231. * Determines whether the query is for an existing category archive page.
  232. *
  233. * If the $category parameter is specified, this function will additionally
  234. * check if the query is for one of the categories specified.
  235. *
  236. * For more information on this and similar theme functions, check out
  237. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  238. * Conditional Tags} article in the Theme Developer Handbook.
  239. *
  240. * @since 1.5.0
  241. *
  242. * @global WP_Query $wp_query WordPress Query object.
  243. *
  244. * @param int|string|int[]|string[] $category Optional. Category ID, name, slug, or array of such
  245. * to check against. Default empty.
  246. * @return bool Whether the query is for an existing category archive page.
  247. */
  248. function is_category( $category = '' ) {
  249. global $wp_query;
  250. if ( ! isset( $wp_query ) ) {
  251. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  252. return false;
  253. }
  254. return $wp_query->is_category( $category );
  255. }
  256. /**
  257. * Determines whether the query is for an existing tag archive page.
  258. *
  259. * If the $tag parameter is specified, this function will additionally
  260. * check if the query is for one of the tags specified.
  261. *
  262. * For more information on this and similar theme functions, check out
  263. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  264. * Conditional Tags} article in the Theme Developer Handbook.
  265. *
  266. * @since 2.3.0
  267. *
  268. * @global WP_Query $wp_query WordPress Query object.
  269. *
  270. * @param int|string|int[]|string[] $tag Optional. Tag ID, name, slug, or array of such
  271. * to check against. Default empty.
  272. * @return bool Whether the query is for an existing tag archive page.
  273. */
  274. function is_tag( $tag = '' ) {
  275. global $wp_query;
  276. if ( ! isset( $wp_query ) ) {
  277. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  278. return false;
  279. }
  280. return $wp_query->is_tag( $tag );
  281. }
  282. /**
  283. * Determines whether the query is for an existing custom taxonomy archive page.
  284. *
  285. * If the $taxonomy parameter is specified, this function will additionally
  286. * check if the query is for that specific $taxonomy.
  287. *
  288. * If the $term parameter is specified in addition to the $taxonomy parameter,
  289. * this function will additionally check if the query is for one of the terms
  290. * specified.
  291. *
  292. * For more information on this and similar theme functions, check out
  293. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  294. * Conditional Tags} article in the Theme Developer Handbook.
  295. *
  296. * @since 2.5.0
  297. *
  298. * @global WP_Query $wp_query WordPress Query object.
  299. *
  300. * @param string|string[] $taxonomy Optional. Taxonomy slug or slugs to check against.
  301. * Default empty.
  302. * @param int|string|int[]|string[] $term Optional. Term ID, name, slug, or array of such
  303. * to check against. Default empty.
  304. * @return bool Whether the query is for an existing custom taxonomy archive page.
  305. * True for custom taxonomy archive pages, false for built-in taxonomies
  306. * (category and tag archives).
  307. */
  308. function is_tax( $taxonomy = '', $term = '' ) {
  309. global $wp_query;
  310. if ( ! isset( $wp_query ) ) {
  311. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  312. return false;
  313. }
  314. return $wp_query->is_tax( $taxonomy, $term );
  315. }
  316. /**
  317. * Determines whether the query is for an existing date archive.
  318. *
  319. * For more information on this and similar theme functions, check out
  320. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  321. * Conditional Tags} article in the Theme Developer Handbook.
  322. *
  323. * @since 1.5.0
  324. *
  325. * @global WP_Query $wp_query WordPress Query object.
  326. *
  327. * @return bool Whether the query is for an existing date archive.
  328. */
  329. function is_date() {
  330. global $wp_query;
  331. if ( ! isset( $wp_query ) ) {
  332. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  333. return false;
  334. }
  335. return $wp_query->is_date();
  336. }
  337. /**
  338. * Determines whether the query is for an existing day archive.
  339. *
  340. * A conditional check to test whether the page is a date-based archive page displaying posts for the current day.
  341. *
  342. * For more information on this and similar theme functions, check out
  343. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  344. * Conditional Tags} article in the Theme Developer Handbook.
  345. *
  346. * @since 1.5.0
  347. *
  348. * @global WP_Query $wp_query WordPress Query object.
  349. *
  350. * @return bool Whether the query is for an existing day archive.
  351. */
  352. function is_day() {
  353. global $wp_query;
  354. if ( ! isset( $wp_query ) ) {
  355. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  356. return false;
  357. }
  358. return $wp_query->is_day();
  359. }
  360. /**
  361. * Determines whether the query is for a feed.
  362. *
  363. * For more information on this and similar theme functions, check out
  364. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  365. * Conditional Tags} article in the Theme Developer Handbook.
  366. *
  367. * @since 1.5.0
  368. *
  369. * @global WP_Query $wp_query WordPress Query object.
  370. *
  371. * @param string|string[] $feeds Optional. Feed type or array of feed types
  372. * to check against. Default empty.
  373. * @return bool Whether the query is for a feed.
  374. */
  375. function is_feed( $feeds = '' ) {
  376. global $wp_query;
  377. if ( ! isset( $wp_query ) ) {
  378. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  379. return false;
  380. }
  381. return $wp_query->is_feed( $feeds );
  382. }
  383. /**
  384. * Is the query for a comments feed?
  385. *
  386. * @since 3.0.0
  387. *
  388. * @global WP_Query $wp_query WordPress Query object.
  389. *
  390. * @return bool Whether the query is for a comments feed.
  391. */
  392. function is_comment_feed() {
  393. global $wp_query;
  394. if ( ! isset( $wp_query ) ) {
  395. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  396. return false;
  397. }
  398. return $wp_query->is_comment_feed();
  399. }
  400. /**
  401. * Determines whether the query is for the front page of the site.
  402. *
  403. * This is for what is displayed at your site's main URL.
  404. *
  405. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  406. *
  407. * If you set a static page for the front page of your site, this function will return
  408. * true when viewing that page.
  409. *
  410. * Otherwise the same as @see is_home()
  411. *
  412. * For more information on this and similar theme functions, check out
  413. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  414. * Conditional Tags} article in the Theme Developer Handbook.
  415. *
  416. * @since 2.5.0
  417. *
  418. * @global WP_Query $wp_query WordPress Query object.
  419. *
  420. * @return bool Whether the query is for the front page of the site.
  421. */
  422. function is_front_page() {
  423. global $wp_query;
  424. if ( ! isset( $wp_query ) ) {
  425. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  426. return false;
  427. }
  428. return $wp_query->is_front_page();
  429. }
  430. /**
  431. * Determines whether the query is for the blog homepage.
  432. *
  433. * The blog homepage is the page that shows the time-based blog content of the site.
  434. *
  435. * is_home() is dependent on the site's "Front page displays" Reading Settings 'show_on_front'
  436. * and 'page_for_posts'.
  437. *
  438. * If a static page is set for the front page of the site, this function will return true only
  439. * on the page you set as the "Posts page".
  440. *
  441. * For more information on this and similar theme functions, check out
  442. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  443. * Conditional Tags} article in the Theme Developer Handbook.
  444. *
  445. * @since 1.5.0
  446. *
  447. * @see is_front_page()
  448. * @global WP_Query $wp_query WordPress Query object.
  449. *
  450. * @return bool Whether the query is for the blog homepage.
  451. */
  452. function is_home() {
  453. global $wp_query;
  454. if ( ! isset( $wp_query ) ) {
  455. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  456. return false;
  457. }
  458. return $wp_query->is_home();
  459. }
  460. /**
  461. * Determines whether the query is for the Privacy Policy page.
  462. *
  463. * The Privacy Policy page is the page that shows the Privacy Policy content of the site.
  464. *
  465. * is_privacy_policy() is dependent on the site's "Change your Privacy Policy page" Privacy Settings 'wp_page_for_privacy_policy'.
  466. *
  467. * This function will return true only on the page you set as the "Privacy Policy page".
  468. *
  469. * For more information on this and similar theme functions, check out
  470. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  471. * Conditional Tags} article in the Theme Developer Handbook.
  472. *
  473. * @since 5.2.0
  474. *
  475. * @global WP_Query $wp_query WordPress Query object.
  476. *
  477. * @return bool Whether the query is for the Privacy Policy page.
  478. */
  479. function is_privacy_policy() {
  480. global $wp_query;
  481. if ( ! isset( $wp_query ) ) {
  482. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  483. return false;
  484. }
  485. return $wp_query->is_privacy_policy();
  486. }
  487. /**
  488. * Determines whether the query is for an existing month archive.
  489. *
  490. * For more information on this and similar theme functions, check out
  491. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  492. * Conditional Tags} article in the Theme Developer Handbook.
  493. *
  494. * @since 1.5.0
  495. *
  496. * @global WP_Query $wp_query WordPress Query object.
  497. *
  498. * @return bool Whether the query is for an existing month archive.
  499. */
  500. function is_month() {
  501. global $wp_query;
  502. if ( ! isset( $wp_query ) ) {
  503. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  504. return false;
  505. }
  506. return $wp_query->is_month();
  507. }
  508. /**
  509. * Determines whether the query is for an existing single page.
  510. *
  511. * If the $page parameter is specified, this function will additionally
  512. * check if the query is for one of the pages specified.
  513. *
  514. * For more information on this and similar theme functions, check out
  515. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  516. * Conditional Tags} article in the Theme Developer Handbook.
  517. *
  518. * @since 1.5.0
  519. *
  520. * @see is_single()
  521. * @see is_singular()
  522. * @global WP_Query $wp_query WordPress Query object.
  523. *
  524. * @param int|string|int[]|string[] $page Optional. Page ID, title, slug, or array of such
  525. * to check against. Default empty.
  526. * @return bool Whether the query is for an existing single page.
  527. */
  528. function is_page( $page = '' ) {
  529. global $wp_query;
  530. if ( ! isset( $wp_query ) ) {
  531. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  532. return false;
  533. }
  534. return $wp_query->is_page( $page );
  535. }
  536. /**
  537. * Determines whether the query is for a paged result and not for the first page.
  538. *
  539. * For more information on this and similar theme functions, check out
  540. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  541. * Conditional Tags} article in the Theme Developer Handbook.
  542. *
  543. * @since 1.5.0
  544. *
  545. * @global WP_Query $wp_query WordPress Query object.
  546. *
  547. * @return bool Whether the query is for a paged result.
  548. */
  549. function is_paged() {
  550. global $wp_query;
  551. if ( ! isset( $wp_query ) ) {
  552. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  553. return false;
  554. }
  555. return $wp_query->is_paged();
  556. }
  557. /**
  558. * Determines whether the query is for a post or page preview.
  559. *
  560. * For more information on this and similar theme functions, check out
  561. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  562. * Conditional Tags} article in the Theme Developer Handbook.
  563. *
  564. * @since 2.0.0
  565. *
  566. * @global WP_Query $wp_query WordPress Query object.
  567. *
  568. * @return bool Whether the query is for a post or page preview.
  569. */
  570. function is_preview() {
  571. global $wp_query;
  572. if ( ! isset( $wp_query ) ) {
  573. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  574. return false;
  575. }
  576. return $wp_query->is_preview();
  577. }
  578. /**
  579. * Is the query for the robots.txt file?
  580. *
  581. * @since 2.1.0
  582. *
  583. * @global WP_Query $wp_query WordPress Query object.
  584. *
  585. * @return bool Whether the query is for the robots.txt file.
  586. */
  587. function is_robots() {
  588. global $wp_query;
  589. if ( ! isset( $wp_query ) ) {
  590. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  591. return false;
  592. }
  593. return $wp_query->is_robots();
  594. }
  595. /**
  596. * Is the query for the favicon.ico file?
  597. *
  598. * @since 5.4.0
  599. *
  600. * @global WP_Query $wp_query WordPress Query object.
  601. *
  602. * @return bool Whether the query is for the favicon.ico file.
  603. */
  604. function is_favicon() {
  605. global $wp_query;
  606. if ( ! isset( $wp_query ) ) {
  607. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  608. return false;
  609. }
  610. return $wp_query->is_favicon();
  611. }
  612. /**
  613. * Determines whether the query is for a search.
  614. *
  615. * For more information on this and similar theme functions, check out
  616. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  617. * Conditional Tags} article in the Theme Developer Handbook.
  618. *
  619. * @since 1.5.0
  620. *
  621. * @global WP_Query $wp_query WordPress Query object.
  622. *
  623. * @return bool Whether the query is for a search.
  624. */
  625. function is_search() {
  626. global $wp_query;
  627. if ( ! isset( $wp_query ) ) {
  628. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  629. return false;
  630. }
  631. return $wp_query->is_search();
  632. }
  633. /**
  634. * Determines whether the query is for an existing single post.
  635. *
  636. * Works for any post type, except attachments and pages
  637. *
  638. * If the $post parameter is specified, this function will additionally
  639. * check if the query is for one of the Posts specified.
  640. *
  641. * For more information on this and similar theme functions, check out
  642. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  643. * Conditional Tags} article in the Theme Developer Handbook.
  644. *
  645. * @since 1.5.0
  646. *
  647. * @see is_page()
  648. * @see is_singular()
  649. * @global WP_Query $wp_query WordPress Query object.
  650. *
  651. * @param int|string|int[]|string[] $post Optional. Post ID, title, slug, or array of such
  652. * to check against. Default empty.
  653. * @return bool Whether the query is for an existing single post.
  654. */
  655. function is_single( $post = '' ) {
  656. global $wp_query;
  657. if ( ! isset( $wp_query ) ) {
  658. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  659. return false;
  660. }
  661. return $wp_query->is_single( $post );
  662. }
  663. /**
  664. * Determines whether the query is for an existing single post of any post type
  665. * (post, attachment, page, custom post types).
  666. *
  667. * If the $post_types parameter is specified, this function will additionally
  668. * check if the query is for one of the Posts Types specified.
  669. *
  670. * For more information on this and similar theme functions, check out
  671. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  672. * Conditional Tags} article in the Theme Developer Handbook.
  673. *
  674. * @since 1.5.0
  675. *
  676. * @see is_page()
  677. * @see is_single()
  678. * @global WP_Query $wp_query WordPress Query object.
  679. *
  680. * @param string|string[] $post_types Optional. Post type or array of post types
  681. * to check against. Default empty.
  682. * @return bool Whether the query is for an existing single post
  683. * or any of the given post types.
  684. */
  685. function is_singular( $post_types = '' ) {
  686. global $wp_query;
  687. if ( ! isset( $wp_query ) ) {
  688. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  689. return false;
  690. }
  691. return $wp_query->is_singular( $post_types );
  692. }
  693. /**
  694. * Determines whether the query is for a specific time.
  695. *
  696. * For more information on this and similar theme functions, check out
  697. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  698. * Conditional Tags} article in the Theme Developer Handbook.
  699. *
  700. * @since 1.5.0
  701. *
  702. * @global WP_Query $wp_query WordPress Query object.
  703. *
  704. * @return bool Whether the query is for a specific time.
  705. */
  706. function is_time() {
  707. global $wp_query;
  708. if ( ! isset( $wp_query ) ) {
  709. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  710. return false;
  711. }
  712. return $wp_query->is_time();
  713. }
  714. /**
  715. * Determines whether the query is for a trackback endpoint call.
  716. *
  717. * For more information on this and similar theme functions, check out
  718. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  719. * Conditional Tags} article in the Theme Developer Handbook.
  720. *
  721. * @since 1.5.0
  722. *
  723. * @global WP_Query $wp_query WordPress Query object.
  724. *
  725. * @return bool Whether the query is for a trackback endpoint call.
  726. */
  727. function is_trackback() {
  728. global $wp_query;
  729. if ( ! isset( $wp_query ) ) {
  730. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  731. return false;
  732. }
  733. return $wp_query->is_trackback();
  734. }
  735. /**
  736. * Determines whether the query is for an existing year archive.
  737. *
  738. * For more information on this and similar theme functions, check out
  739. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  740. * Conditional Tags} article in the Theme Developer Handbook.
  741. *
  742. * @since 1.5.0
  743. *
  744. * @global WP_Query $wp_query WordPress Query object.
  745. *
  746. * @return bool Whether the query is for an existing year archive.
  747. */
  748. function is_year() {
  749. global $wp_query;
  750. if ( ! isset( $wp_query ) ) {
  751. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  752. return false;
  753. }
  754. return $wp_query->is_year();
  755. }
  756. /**
  757. * Determines whether the query has resulted in a 404 (returns no results).
  758. *
  759. * For more information on this and similar theme functions, check out
  760. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  761. * Conditional Tags} article in the Theme Developer Handbook.
  762. *
  763. * @since 1.5.0
  764. *
  765. * @global WP_Query $wp_query WordPress Query object.
  766. *
  767. * @return bool Whether the query is a 404 error.
  768. */
  769. function is_404() {
  770. global $wp_query;
  771. if ( ! isset( $wp_query ) ) {
  772. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  773. return false;
  774. }
  775. return $wp_query->is_404();
  776. }
  777. /**
  778. * Is the query for an embedded post?
  779. *
  780. * @since 4.4.0
  781. *
  782. * @global WP_Query $wp_query WordPress Query object.
  783. *
  784. * @return bool Whether the query is for an embedded post.
  785. */
  786. function is_embed() {
  787. global $wp_query;
  788. if ( ! isset( $wp_query ) ) {
  789. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  790. return false;
  791. }
  792. return $wp_query->is_embed();
  793. }
  794. /**
  795. * Determines whether the query is the main query.
  796. *
  797. * For more information on this and similar theme functions, check out
  798. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  799. * Conditional Tags} article in the Theme Developer Handbook.
  800. *
  801. * @since 3.3.0
  802. *
  803. * @global WP_Query $wp_query WordPress Query object.
  804. *
  805. * @return bool Whether the query is the main query.
  806. */
  807. function is_main_query() {
  808. global $wp_query;
  809. if ( 'pre_get_posts' === current_filter() ) {
  810. _doing_it_wrong(
  811. __FUNCTION__,
  812. sprintf(
  813. /* translators: 1: pre_get_posts, 2: WP_Query->is_main_query(), 3: is_main_query(), 4: Documentation URL. */
  814. __( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
  815. '<code>pre_get_posts</code>',
  816. '<code>WP_Query->is_main_query()</code>',
  817. '<code>is_main_query()</code>',
  818. __( 'https://developer.wordpress.org/reference/functions/is_main_query/' )
  819. ),
  820. '3.7.0'
  821. );
  822. }
  823. return $wp_query->is_main_query();
  824. }
  825. /*
  826. * The Loop. Post loop control.
  827. */
  828. /**
  829. * Determines whether current WordPress query has posts to loop over.
  830. *
  831. * @since 1.5.0
  832. *
  833. * @global WP_Query $wp_query WordPress Query object.
  834. *
  835. * @return bool True if posts are available, false if end of the loop.
  836. */
  837. function have_posts() {
  838. global $wp_query;
  839. return $wp_query->have_posts();
  840. }
  841. /**
  842. * Determines whether the caller is in the Loop.
  843. *
  844. * For more information on this and similar theme functions, check out
  845. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  846. * Conditional Tags} article in the Theme Developer Handbook.
  847. *
  848. * @since 2.0.0
  849. *
  850. * @global WP_Query $wp_query WordPress Query object.
  851. *
  852. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  853. */
  854. function in_the_loop() {
  855. global $wp_query;
  856. return $wp_query->in_the_loop;
  857. }
  858. /**
  859. * Rewind the loop posts.
  860. *
  861. * @since 1.5.0
  862. *
  863. * @global WP_Query $wp_query WordPress Query object.
  864. */
  865. function rewind_posts() {
  866. global $wp_query;
  867. $wp_query->rewind_posts();
  868. }
  869. /**
  870. * Iterate the post index in the loop.
  871. *
  872. * @since 1.5.0
  873. *
  874. * @global WP_Query $wp_query WordPress Query object.
  875. */
  876. function the_post() {
  877. global $wp_query;
  878. $wp_query->the_post();
  879. }
  880. /*
  881. * Comments loop.
  882. */
  883. /**
  884. * Determines whether current WordPress query has comments to loop over.
  885. *
  886. * @since 2.2.0
  887. *
  888. * @global WP_Query $wp_query WordPress Query object.
  889. *
  890. * @return bool True if comments are available, false if no more comments.
  891. */
  892. function have_comments() {
  893. global $wp_query;
  894. return $wp_query->have_comments();
  895. }
  896. /**
  897. * Iterate comment index in the comment loop.
  898. *
  899. * @since 2.2.0
  900. *
  901. * @global WP_Query $wp_query WordPress Query object.
  902. *
  903. * @return null
  904. */
  905. function the_comment() {
  906. global $wp_query;
  907. return $wp_query->the_comment();
  908. }
  909. /**
  910. * Redirect old slugs to the correct permalink.
  911. *
  912. * Attempts to find the current slug from the past slugs.
  913. *
  914. * @since 2.1.0
  915. */
  916. function wp_old_slug_redirect() {
  917. if ( is_404() && '' !== get_query_var( 'name' ) ) {
  918. // Guess the current post type based on the query vars.
  919. if ( get_query_var( 'post_type' ) ) {
  920. $post_type = get_query_var( 'post_type' );
  921. } elseif ( get_query_var( 'attachment' ) ) {
  922. $post_type = 'attachment';
  923. } elseif ( get_query_var( 'pagename' ) ) {
  924. $post_type = 'page';
  925. } else {
  926. $post_type = 'post';
  927. }
  928. if ( is_array( $post_type ) ) {
  929. if ( count( $post_type ) > 1 ) {
  930. return;
  931. }
  932. $post_type = reset( $post_type );
  933. }
  934. // Do not attempt redirect for hierarchical post types.
  935. if ( is_post_type_hierarchical( $post_type ) ) {
  936. return;
  937. }
  938. $id = _find_post_by_old_slug( $post_type );
  939. if ( ! $id ) {
  940. $id = _find_post_by_old_date( $post_type );
  941. }
  942. /**
  943. * Filters the old slug redirect post ID.
  944. *
  945. * @since 4.9.3
  946. *
  947. * @param int $id The redirect post ID.
  948. */
  949. $id = apply_filters( 'old_slug_redirect_post_id', $id );
  950. if ( ! $id ) {
  951. return;
  952. }
  953. $link = get_permalink( $id );
  954. if ( get_query_var( 'paged' ) > 1 ) {
  955. $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) );
  956. } elseif ( is_embed() ) {
  957. $link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
  958. }
  959. /**
  960. * Filters the old slug redirect URL.
  961. *
  962. * @since 4.4.0
  963. *
  964. * @param string $link The redirect URL.
  965. */
  966. $link = apply_filters( 'old_slug_redirect_url', $link );
  967. if ( ! $link ) {
  968. return;
  969. }
  970. wp_redirect( $link, 301 ); // Permanent redirect.
  971. exit;
  972. }
  973. }
  974. /**
  975. * Find the post ID for redirecting an old slug.
  976. *
  977. * @since 4.9.3
  978. * @access private
  979. *
  980. * @see wp_old_slug_redirect()
  981. * @global wpdb $wpdb WordPress database abstraction object.
  982. *
  983. * @param string $post_type The current post type based on the query vars.
  984. * @return int The Post ID.
  985. */
  986. function _find_post_by_old_slug( $post_type ) {
  987. global $wpdb;
  988. $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' ) );
  989. // If year, monthnum, or day have been specified, make our query more precise
  990. // just in case there are multiple identical _wp_old_slug values.
  991. if ( get_query_var( 'year' ) ) {
  992. $query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
  993. }
  994. if ( get_query_var( 'monthnum' ) ) {
  995. $query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
  996. }
  997. if ( get_query_var( 'day' ) ) {
  998. $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
  999. }
  1000. $id = (int) $wpdb->get_var( $query );
  1001. return $id;
  1002. }
  1003. /**
  1004. * Find the post ID for redirecting an old date.
  1005. *
  1006. * @since 4.9.3
  1007. * @access private
  1008. *
  1009. * @see wp_old_slug_redirect()
  1010. * @global wpdb $wpdb WordPress database abstraction object.
  1011. *
  1012. * @param string $post_type The current post type based on the query vars.
  1013. * @return int The Post ID.
  1014. */
  1015. function _find_post_by_old_date( $post_type ) {
  1016. global $wpdb;
  1017. $date_query = '';
  1018. if ( get_query_var( 'year' ) ) {
  1019. $date_query .= $wpdb->prepare( ' AND YEAR(pm_date.meta_value) = %d', get_query_var( 'year' ) );
  1020. }
  1021. if ( get_query_var( 'monthnum' ) ) {
  1022. $date_query .= $wpdb->prepare( ' AND MONTH(pm_date.meta_value) = %d', get_query_var( 'monthnum' ) );
  1023. }
  1024. if ( get_query_var( 'day' ) ) {
  1025. $date_query .= $wpdb->prepare( ' AND DAYOFMONTH(pm_date.meta_value) = %d', get_query_var( 'day' ) );
  1026. }
  1027. $id = 0;
  1028. if ( $date_query ) {
  1029. $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' ) ) );
  1030. if ( ! $id ) {
  1031. // Check to see if an old slug matches the old date.
  1032. $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' ) ) );
  1033. }
  1034. }
  1035. return $id;
  1036. }
  1037. /**
  1038. * Set up global post data.
  1039. *
  1040. * @since 1.5.0
  1041. * @since 4.4.0 Added the ability to pass a post ID to `$post`.
  1042. *
  1043. * @global WP_Query $wp_query WordPress Query object.
  1044. *
  1045. * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  1046. * @return bool True when finished.
  1047. */
  1048. function setup_postdata( $post ) {
  1049. global $wp_query;
  1050. if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
  1051. return $wp_query->setup_postdata( $post );
  1052. }
  1053. return false;
  1054. }
  1055. /**
  1056. * Generates post data.
  1057. *
  1058. * @since 5.2.0
  1059. *
  1060. * @global WP_Query $wp_query WordPress Query object.
  1061. *
  1062. * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  1063. * @return array|false Elements of post, or false on failure.
  1064. */
  1065. function generate_postdata( $post ) {
  1066. global $wp_query;
  1067. if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
  1068. return $wp_query->generate_postdata( $post );
  1069. }
  1070. return false;
  1071. }