query.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. * custom post types)?
  532. *
  533. * If the $post_types parameter is specified, this function will additionally
  534. * check if the query is for one of the Posts Types specified.
  535. *
  536. * @see is_page()
  537. * @see is_single()
  538. *
  539. * @since 1.5.0
  540. *
  541. * @global WP_Query $wp_query Global WP_Query instance.
  542. *
  543. * @param string|array $post_types Optional. Post type or array of post types. Default empty.
  544. * @return bool Whether the query is for an existing single post of any of the given post types.
  545. */
  546. function is_singular( $post_types = '' ) {
  547. global $wp_query;
  548. if ( ! isset( $wp_query ) ) {
  549. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  550. return false;
  551. }
  552. return $wp_query->is_singular( $post_types );
  553. }
  554. /**
  555. * Is the query for a specific time?
  556. *
  557. * @since 1.5.0
  558. *
  559. * @global WP_Query $wp_query Global WP_Query instance.
  560. *
  561. * @return bool
  562. */
  563. function is_time() {
  564. global $wp_query;
  565. if ( ! isset( $wp_query ) ) {
  566. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  567. return false;
  568. }
  569. return $wp_query->is_time();
  570. }
  571. /**
  572. * Is the query for a trackback endpoint call?
  573. *
  574. * @since 1.5.0
  575. *
  576. * @global WP_Query $wp_query Global WP_Query instance.
  577. *
  578. * @return bool
  579. */
  580. function is_trackback() {
  581. global $wp_query;
  582. if ( ! isset( $wp_query ) ) {
  583. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  584. return false;
  585. }
  586. return $wp_query->is_trackback();
  587. }
  588. /**
  589. * Is the query for an existing year archive?
  590. *
  591. * @since 1.5.0
  592. *
  593. * @global WP_Query $wp_query Global WP_Query instance.
  594. *
  595. * @return bool
  596. */
  597. function is_year() {
  598. global $wp_query;
  599. if ( ! isset( $wp_query ) ) {
  600. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  601. return false;
  602. }
  603. return $wp_query->is_year();
  604. }
  605. /**
  606. * Is the query a 404 (returns no results)?
  607. *
  608. * @since 1.5.0
  609. *
  610. * @global WP_Query $wp_query Global WP_Query instance.
  611. *
  612. * @return bool
  613. */
  614. function is_404() {
  615. global $wp_query;
  616. if ( ! isset( $wp_query ) ) {
  617. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  618. return false;
  619. }
  620. return $wp_query->is_404();
  621. }
  622. /**
  623. * Is the query for an embedded post?
  624. *
  625. * @since 4.4.0
  626. *
  627. * @global WP_Query $wp_query Global WP_Query instance.
  628. *
  629. * @return bool Whether we're in an embedded post or not.
  630. */
  631. function is_embed() {
  632. global $wp_query;
  633. if ( ! isset( $wp_query ) ) {
  634. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  635. return false;
  636. }
  637. return $wp_query->is_embed();
  638. }
  639. /**
  640. * Is the query the main query?
  641. *
  642. * @since 3.3.0
  643. *
  644. * @global WP_Query $wp_query Global WP_Query instance.
  645. *
  646. * @return bool
  647. */
  648. function is_main_query() {
  649. if ( 'pre_get_posts' === current_filter() ) {
  650. $message = sprintf(
  651. /* translators: 1: pre_get_posts 2: WP_Query->is_main_query() 3: is_main_query() 4: link to codex is_main_query() page. */
  652. __( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
  653. '<code>pre_get_posts</code>',
  654. '<code>WP_Query->is_main_query()</code>',
  655. '<code>is_main_query()</code>',
  656. __( 'https://codex.wordpress.org/Function_Reference/is_main_query' )
  657. );
  658. _doing_it_wrong( __FUNCTION__, $message, '3.7.0' );
  659. }
  660. global $wp_query;
  661. return $wp_query->is_main_query();
  662. }
  663. /*
  664. * The Loop. Post loop control.
  665. */
  666. /**
  667. * Whether current WordPress query has results to loop over.
  668. *
  669. * @since 1.5.0
  670. *
  671. * @global WP_Query $wp_query Global WP_Query instance.
  672. *
  673. * @return bool
  674. */
  675. function have_posts() {
  676. global $wp_query;
  677. return $wp_query->have_posts();
  678. }
  679. /**
  680. * Whether the caller is in the Loop.
  681. *
  682. * @since 2.0.0
  683. *
  684. * @global WP_Query $wp_query Global WP_Query instance.
  685. *
  686. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  687. */
  688. function in_the_loop() {
  689. global $wp_query;
  690. return $wp_query->in_the_loop;
  691. }
  692. /**
  693. * Rewind the loop posts.
  694. *
  695. * @since 1.5.0
  696. *
  697. * @global WP_Query $wp_query Global WP_Query instance.
  698. */
  699. function rewind_posts() {
  700. global $wp_query;
  701. $wp_query->rewind_posts();
  702. }
  703. /**
  704. * Iterate the post index in the loop.
  705. *
  706. * @since 1.5.0
  707. *
  708. * @global WP_Query $wp_query Global WP_Query instance.
  709. */
  710. function the_post() {
  711. global $wp_query;
  712. $wp_query->the_post();
  713. }
  714. /*
  715. * Comments loop.
  716. */
  717. /**
  718. * Whether there are comments to loop over.
  719. *
  720. * @since 2.2.0
  721. *
  722. * @global WP_Query $wp_query Global WP_Query instance.
  723. *
  724. * @return bool
  725. */
  726. function have_comments() {
  727. global $wp_query;
  728. return $wp_query->have_comments();
  729. }
  730. /**
  731. * Iterate comment index in the comment loop.
  732. *
  733. * @since 2.2.0
  734. *
  735. * @global WP_Query $wp_query Global WP_Query instance.
  736. *
  737. * @return object
  738. */
  739. function the_comment() {
  740. global $wp_query;
  741. return $wp_query->the_comment();
  742. }
  743. /**
  744. * Redirect old slugs to the correct permalink.
  745. *
  746. * Attempts to find the current slug from the past slugs.
  747. *
  748. * @since 2.1.0
  749. *
  750. * @global wpdb $wpdb WordPress database abstraction object.
  751. */
  752. function wp_old_slug_redirect() {
  753. if ( is_404() && '' !== get_query_var( 'name' ) ) {
  754. global $wpdb;
  755. // Guess the current post_type based on the query vars.
  756. if ( get_query_var( 'post_type' ) ) {
  757. $post_type = get_query_var( 'post_type' );
  758. } elseif ( get_query_var( 'attachment' ) ) {
  759. $post_type = 'attachment';
  760. } elseif ( get_query_var( 'pagename' ) ) {
  761. $post_type = 'page';
  762. } else {
  763. $post_type = 'post';
  764. }
  765. if ( is_array( $post_type ) ) {
  766. if ( count( $post_type ) > 1 ) {
  767. return;
  768. }
  769. $post_type = reset( $post_type );
  770. }
  771. // Do not attempt redirect for hierarchical post types
  772. if ( is_post_type_hierarchical( $post_type ) ) {
  773. return;
  774. }
  775. $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' ) );
  776. // if year, monthnum, or day have been specified, make our query more precise
  777. // just in case there are multiple identical _wp_old_slug values
  778. if ( get_query_var( 'year' ) ) {
  779. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var( 'year' ) );
  780. }
  781. if ( get_query_var( 'monthnum' ) ) {
  782. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
  783. }
  784. if ( get_query_var( 'day' ) ) {
  785. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
  786. }
  787. $id = (int) $wpdb->get_var( $query );
  788. if ( ! $id ) {
  789. return;
  790. }
  791. $link = get_permalink( $id );
  792. if ( get_query_var( 'paged' ) > 1 ) {
  793. $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) );
  794. } elseif( is_embed() ) {
  795. $link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
  796. }
  797. /**
  798. * Filters the old slug redirect URL.
  799. *
  800. * @since 4.4.0
  801. *
  802. * @param string $link The redirect URL.
  803. */
  804. $link = apply_filters( 'old_slug_redirect_url', $link );
  805. if ( ! $link ) {
  806. return;
  807. }
  808. wp_redirect( $link, 301 ); // Permanent redirect
  809. exit;
  810. }
  811. }
  812. /**
  813. * Set up global post data.
  814. *
  815. * @since 1.5.0
  816. * @since 4.4.0 Added the ability to pass a post ID to `$post`.
  817. *
  818. * @global WP_Query $wp_query Global WP_Query instance.
  819. *
  820. * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  821. * @return bool True when finished.
  822. */
  823. function setup_postdata( $post ) {
  824. global $wp_query;
  825. if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
  826. return $wp_query->setup_postdata( $post );
  827. }
  828. return false;
  829. }