query.php 25 KB

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