query.php 36 KB

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