/wp-includes/query.php

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