/wp-includes/query.php

https://gitlab.com/webkod3r/tripolis · PHP · 1868 lines · 664 code · 210 blank · 994 comment · 171 complexity · 0047b8afd80793d3bac0df5c46ec5c51 MD5 · raw file

  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. * Set up The Loop with query parameters.
  76. *
  77. * This will override the current WordPress Loop and shouldn't be used more than
  78. * once. This must not be used within the WordPress Loop.
  79. *
  80. * @since 1.5.0
  81. *
  82. * @global WP_Query $wp_query Global WP_Query instance.
  83. *
  84. * @param string $query
  85. * @return array List of posts
  86. */
  87. function query_posts($query) {
  88. $GLOBALS['wp_query'] = new WP_Query();
  89. return $GLOBALS['wp_query']->query($query);
  90. }
  91. /**
  92. * Destroy the previous query and set up a new query.
  93. *
  94. * This should be used after {@link query_posts()} and before another {@link
  95. * query_posts()}. This will remove obscure bugs that occur when the previous
  96. * wp_query object is not destroyed properly before another is set up.
  97. *
  98. * @since 2.3.0
  99. *
  100. * @global WP_Query $wp_query Global WP_Query instance.
  101. * @global WP_Query $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
  102. */
  103. function wp_reset_query() {
  104. $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
  105. wp_reset_postdata();
  106. }
  107. /**
  108. * After looping through a separate query, this function restores
  109. * the $post global to the current post in the main query.
  110. *
  111. * @since 3.0.0
  112. *
  113. * @global WP_Query $wp_query Global WP_Query instance.
  114. */
  115. function wp_reset_postdata() {
  116. global $wp_query;
  117. if ( isset( $wp_query ) ) {
  118. $wp_query->reset_postdata();
  119. }
  120. }
  121. /*
  122. * Query type checks.
  123. */
  124. /**
  125. * Is the query for an existing archive page?
  126. *
  127. * Month, Year, Category, Author, Post Type archive...
  128. *
  129. * @since 1.5.0
  130. *
  131. * @global WP_Query $wp_query Global WP_Query instance.
  132. *
  133. * @return bool
  134. */
  135. function is_archive() {
  136. global $wp_query;
  137. if ( ! isset( $wp_query ) ) {
  138. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  139. return false;
  140. }
  141. return $wp_query->is_archive();
  142. }
  143. /**
  144. * Is the query for an existing post type archive page?
  145. *
  146. * @since 3.1.0
  147. *
  148. * @global WP_Query $wp_query Global WP_Query instance.
  149. *
  150. * @param string|array $post_types Optional. Post type or array of posts types to check against.
  151. * @return bool
  152. */
  153. function is_post_type_archive( $post_types = '' ) {
  154. global $wp_query;
  155. if ( ! isset( $wp_query ) ) {
  156. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  157. return false;
  158. }
  159. return $wp_query->is_post_type_archive( $post_types );
  160. }
  161. /**
  162. * Is the query for an existing attachment page?
  163. *
  164. * @since 2.0.0
  165. *
  166. * @global WP_Query $wp_query Global WP_Query instance.
  167. *
  168. * @param int|string|array|object $attachment Attachment ID, title, slug, or array of such.
  169. * @return bool
  170. */
  171. function is_attachment( $attachment = '' ) {
  172. global $wp_query;
  173. if ( ! isset( $wp_query ) ) {
  174. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  175. return false;
  176. }
  177. return $wp_query->is_attachment( $attachment );
  178. }
  179. /**
  180. * Is the query for an existing author archive page?
  181. *
  182. * If the $author parameter is specified, this function will additionally
  183. * check if the query is for one of the authors specified.
  184. *
  185. * @since 1.5.0
  186. *
  187. * @global WP_Query $wp_query Global WP_Query instance.
  188. *
  189. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  190. * @return bool
  191. */
  192. function is_author( $author = '' ) {
  193. global $wp_query;
  194. if ( ! isset( $wp_query ) ) {
  195. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  196. return false;
  197. }
  198. return $wp_query->is_author( $author );
  199. }
  200. /**
  201. * Is the query for an existing category archive page?
  202. *
  203. * If the $category parameter is specified, this function will additionally
  204. * check if the query is for one of the categories specified.
  205. *
  206. * @since 1.5.0
  207. *
  208. * @global WP_Query $wp_query Global WP_Query instance.
  209. *
  210. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  211. * @return bool
  212. */
  213. function is_category( $category = '' ) {
  214. global $wp_query;
  215. if ( ! isset( $wp_query ) ) {
  216. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  217. return false;
  218. }
  219. return $wp_query->is_category( $category );
  220. }
  221. /**
  222. * Is the query for an existing tag archive page?
  223. *
  224. * If the $tag parameter is specified, this function will additionally
  225. * check if the query is for one of the tags specified.
  226. *
  227. * @since 2.3.0
  228. *
  229. * @global WP_Query $wp_query Global WP_Query instance.
  230. *
  231. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  232. * @return bool
  233. */
  234. function is_tag( $tag = '' ) {
  235. global $wp_query;
  236. if ( ! isset( $wp_query ) ) {
  237. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  238. return false;
  239. }
  240. return $wp_query->is_tag( $tag );
  241. }
  242. /**
  243. * Is the query for an existing taxonomy archive page?
  244. *
  245. * If the $taxonomy parameter is specified, this function will additionally
  246. * check if the query is for that specific $taxonomy.
  247. *
  248. * If the $term parameter is specified in addition to the $taxonomy parameter,
  249. * this function will additionally check if the query is for one of the terms
  250. * specified.
  251. *
  252. * @since 2.5.0
  253. *
  254. * @global WP_Query $wp_query Global WP_Query instance.
  255. *
  256. * @param string|array $taxonomy Optional. Taxonomy slug or slugs.
  257. * @param int|string|array $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  258. * @return bool
  259. */
  260. function is_tax( $taxonomy = '', $term = '' ) {
  261. global $wp_query;
  262. if ( ! isset( $wp_query ) ) {
  263. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  264. return false;
  265. }
  266. return $wp_query->is_tax( $taxonomy, $term );
  267. }
  268. /**
  269. * Is the query for an existing date archive?
  270. *
  271. * @since 1.5.0
  272. *
  273. * @global WP_Query $wp_query Global WP_Query instance.
  274. *
  275. * @return bool
  276. */
  277. function is_date() {
  278. global $wp_query;
  279. if ( ! isset( $wp_query ) ) {
  280. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  281. return false;
  282. }
  283. return $wp_query->is_date();
  284. }
  285. /**
  286. * Is the query for an existing day archive?
  287. *
  288. * @since 1.5.0
  289. *
  290. * @global WP_Query $wp_query Global WP_Query instance.
  291. *
  292. * @return bool
  293. */
  294. function is_day() {
  295. global $wp_query;
  296. if ( ! isset( $wp_query ) ) {
  297. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  298. return false;
  299. }
  300. return $wp_query->is_day();
  301. }
  302. /**
  303. * Is the query for a feed?
  304. *
  305. * @since 1.5.0
  306. *
  307. * @global WP_Query $wp_query Global WP_Query instance.
  308. *
  309. * @param string|array $feeds Optional feed types to check.
  310. * @return bool
  311. */
  312. function is_feed( $feeds = '' ) {
  313. global $wp_query;
  314. if ( ! isset( $wp_query ) ) {
  315. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  316. return false;
  317. }
  318. return $wp_query->is_feed( $feeds );
  319. }
  320. /**
  321. * Is the query for a comments feed?
  322. *
  323. * @since 3.0.0
  324. *
  325. * @global WP_Query $wp_query Global WP_Query instance.
  326. *
  327. * @return bool
  328. */
  329. function is_comment_feed() {
  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' );
  333. return false;
  334. }
  335. return $wp_query->is_comment_feed();
  336. }
  337. /**
  338. * Is the query for the front page of the site?
  339. *
  340. * This is for what is displayed at your site's main URL.
  341. *
  342. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  343. *
  344. * If you set a static page for the front page of your site, this function will return
  345. * true when viewing that page.
  346. *
  347. * Otherwise the same as @see is_home()
  348. *
  349. * @since 2.5.0
  350. *
  351. * @global WP_Query $wp_query Global WP_Query instance.
  352. *
  353. * @return bool True, if front of site.
  354. */
  355. function is_front_page() {
  356. global $wp_query;
  357. if ( ! isset( $wp_query ) ) {
  358. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  359. return false;
  360. }
  361. return $wp_query->is_front_page();
  362. }
  363. /**
  364. * Is the query for the blog homepage?
  365. *
  366. * This is the page which shows the time based blog content of your site.
  367. *
  368. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  369. *
  370. * If you set a static page for the front page of your site, this function will return
  371. * true only on the page you set as the "Posts page".
  372. *
  373. * @see is_front_page()
  374. *
  375. * @since 1.5.0
  376. *
  377. * @global WP_Query $wp_query Global WP_Query instance.
  378. *
  379. * @return bool True if blog view homepage.
  380. */
  381. function is_home() {
  382. global $wp_query;
  383. if ( ! isset( $wp_query ) ) {
  384. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  385. return false;
  386. }
  387. return $wp_query->is_home();
  388. }
  389. /**
  390. * Is the query for an existing month archive?
  391. *
  392. * @since 1.5.0
  393. *
  394. * @global WP_Query $wp_query Global WP_Query instance.
  395. *
  396. * @return bool
  397. */
  398. function is_month() {
  399. global $wp_query;
  400. if ( ! isset( $wp_query ) ) {
  401. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  402. return false;
  403. }
  404. return $wp_query->is_month();
  405. }
  406. /**
  407. * Is the query for an existing single page?
  408. *
  409. * If the $page parameter is specified, this function will additionally
  410. * check if the query is for one of the pages specified.
  411. *
  412. * @see is_single()
  413. * @see is_singular()
  414. *
  415. * @since 1.5.0
  416. *
  417. * @global WP_Query $wp_query Global WP_Query instance.
  418. *
  419. * @param int|string|array $page Optional. Page ID, title, slug, or array of such. Default empty.
  420. * @return bool Whether the query is for an existing single page.
  421. */
  422. function is_page( $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' );
  426. return false;
  427. }
  428. return $wp_query->is_page( $page );
  429. }
  430. /**
  431. * Is the query for paged result and not for the first page?
  432. *
  433. * @since 1.5.0
  434. *
  435. * @global WP_Query $wp_query Global WP_Query instance.
  436. *
  437. * @return bool
  438. */
  439. function is_paged() {
  440. global $wp_query;
  441. if ( ! isset( $wp_query ) ) {
  442. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  443. return false;
  444. }
  445. return $wp_query->is_paged();
  446. }
  447. /**
  448. * Is the query for a post or page preview?
  449. *
  450. * @since 2.0.0
  451. *
  452. * @global WP_Query $wp_query Global WP_Query instance.
  453. *
  454. * @return bool
  455. */
  456. function is_preview() {
  457. global $wp_query;
  458. if ( ! isset( $wp_query ) ) {
  459. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  460. return false;
  461. }
  462. return $wp_query->is_preview();
  463. }
  464. /**
  465. * Is the query for the robots file?
  466. *
  467. * @since 2.1.0
  468. *
  469. * @global WP_Query $wp_query Global WP_Query instance.
  470. *
  471. * @return bool
  472. */
  473. function is_robots() {
  474. global $wp_query;
  475. if ( ! isset( $wp_query ) ) {
  476. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  477. return false;
  478. }
  479. return $wp_query->is_robots();
  480. }
  481. /**
  482. * Is the query for a search?
  483. *
  484. * @since 1.5.0
  485. *
  486. * @global WP_Query $wp_query Global WP_Query instance.
  487. *
  488. * @return bool
  489. */
  490. function is_search() {
  491. global $wp_query;
  492. if ( ! isset( $wp_query ) ) {
  493. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  494. return false;
  495. }
  496. return $wp_query->is_search();
  497. }
  498. /**
  499. * Is the query for an existing single post?
  500. *
  501. * Works for any post type, except attachments and pages
  502. *
  503. * If the $post parameter is specified, this function will additionally
  504. * check if the query is for one of the Posts specified.
  505. *
  506. * @see is_page()
  507. * @see is_singular()
  508. *
  509. * @since 1.5.0
  510. *
  511. * @global WP_Query $wp_query Global WP_Query instance.
  512. *
  513. * @param int|string|array $post Optional. Post ID, title, slug, or array of such. Default empty.
  514. * @return bool Whether the query is for an existing single post.
  515. */
  516. function is_single( $post = '' ) {
  517. global $wp_query;
  518. if ( ! isset( $wp_query ) ) {
  519. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  520. return false;
  521. }
  522. return $wp_query->is_single( $post );
  523. }
  524. /**
  525. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  526. *
  527. * If the $post_types parameter is specified, this function will additionally
  528. * check if the query is for one of the Posts Types specified.
  529. *
  530. * @see is_page()
  531. * @see is_single()
  532. *
  533. * @since 1.5.0
  534. *
  535. * @global WP_Query $wp_query Global WP_Query instance.
  536. *
  537. * @param string|array $post_types Optional. Post type or array of post types. Default empty.
  538. * @return bool Whether the query is for an existing single post of any of the given post types.
  539. */
  540. function is_singular( $post_types = '' ) {
  541. global $wp_query;
  542. if ( ! isset( $wp_query ) ) {
  543. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  544. return false;
  545. }
  546. return $wp_query->is_singular( $post_types );
  547. }
  548. /**
  549. * Is the query for a specific time?
  550. *
  551. * @since 1.5.0
  552. *
  553. * @global WP_Query $wp_query Global WP_Query instance.
  554. *
  555. * @return bool
  556. */
  557. function is_time() {
  558. global $wp_query;
  559. if ( ! isset( $wp_query ) ) {
  560. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  561. return false;
  562. }
  563. return $wp_query->is_time();
  564. }
  565. /**
  566. * Is the query for a trackback endpoint call?
  567. *
  568. * @since 1.5.0
  569. *
  570. * @global WP_Query $wp_query Global WP_Query instance.
  571. *
  572. * @return bool
  573. */
  574. function is_trackback() {
  575. global $wp_query;
  576. if ( ! isset( $wp_query ) ) {
  577. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  578. return false;
  579. }
  580. return $wp_query->is_trackback();
  581. }
  582. /**
  583. * Is the query for an existing year archive?
  584. *
  585. * @since 1.5.0
  586. *
  587. * @global WP_Query $wp_query Global WP_Query instance.
  588. *
  589. * @return bool
  590. */
  591. function is_year() {
  592. global $wp_query;
  593. if ( ! isset( $wp_query ) ) {
  594. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  595. return false;
  596. }
  597. return $wp_query->is_year();
  598. }
  599. /**
  600. * Is the query a 404 (returns no results)?
  601. *
  602. * @since 1.5.0
  603. *
  604. * @global WP_Query $wp_query Global WP_Query instance.
  605. *
  606. * @return bool
  607. */
  608. function is_404() {
  609. global $wp_query;
  610. if ( ! isset( $wp_query ) ) {
  611. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  612. return false;
  613. }
  614. return $wp_query->is_404();
  615. }
  616. /**
  617. * Is the query for an embedded post?
  618. *
  619. * @since 4.4.0
  620. *
  621. * @global WP_Query $wp_query Global WP_Query instance.
  622. *
  623. * @return bool Whether we're in an embedded post or not.
  624. */
  625. function is_embed() {
  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' );
  629. return false;
  630. }
  631. return $wp_query->is_embed();
  632. }
  633. /**
  634. * Is the query the main query?
  635. *
  636. * @since 3.3.0
  637. *
  638. * @global WP_Query $wp_query Global WP_Query instance.
  639. *
  640. * @return bool
  641. */
  642. function is_main_query() {
  643. if ( 'pre_get_posts' === current_filter() ) {
  644. $message = sprintf(
  645. /* translators: 1: pre_get_posts 2: WP_Query->is_main_query() 3: is_main_query() 4: link to codex is_main_query() page. */
  646. __( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
  647. '<code>pre_get_posts</code>',
  648. '<code>WP_Query->is_main_query()</code>',
  649. '<code>is_main_query()</code>',
  650. __( 'https://codex.wordpress.org/Function_Reference/is_main_query' )
  651. );
  652. _doing_it_wrong( __FUNCTION__, $message, '3.7' );
  653. }
  654. global $wp_query;
  655. return $wp_query->is_main_query();
  656. }
  657. /*
  658. * The Loop. Post loop control.
  659. */
  660. /**
  661. * Whether current WordPress query has results to loop over.
  662. *
  663. * @since 1.5.0
  664. *
  665. * @global WP_Query $wp_query Global WP_Query instance.
  666. *
  667. * @return bool
  668. */
  669. function have_posts() {
  670. global $wp_query;
  671. return $wp_query->have_posts();
  672. }
  673. /**
  674. * Whether the caller is in the Loop.
  675. *
  676. * @since 2.0.0
  677. *
  678. * @global WP_Query $wp_query Global WP_Query instance.
  679. *
  680. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  681. */
  682. function in_the_loop() {
  683. global $wp_query;
  684. return $wp_query->in_the_loop;
  685. }
  686. /**
  687. * Rewind the loop posts.
  688. *
  689. * @since 1.5.0
  690. *
  691. * @global WP_Query $wp_query Global WP_Query instance.
  692. */
  693. function rewind_posts() {
  694. global $wp_query;
  695. $wp_query->rewind_posts();
  696. }
  697. /**
  698. * Iterate the post index in the loop.
  699. *
  700. * @since 1.5.0
  701. *
  702. * @global WP_Query $wp_query Global WP_Query instance.
  703. */
  704. function the_post() {
  705. global $wp_query;
  706. $wp_query->the_post();
  707. }
  708. /*
  709. * Comments loop.
  710. */
  711. /**
  712. * Whether there are comments to loop over.
  713. *
  714. * @since 2.2.0
  715. *
  716. * @global WP_Query $wp_query Global WP_Query instance.
  717. *
  718. * @return bool
  719. */
  720. function have_comments() {
  721. global $wp_query;
  722. return $wp_query->have_comments();
  723. }
  724. /**
  725. * Iterate comment index in the comment loop.
  726. *
  727. * @since 2.2.0
  728. *
  729. * @global WP_Query $wp_query Global WP_Query instance.
  730. *
  731. * @return object
  732. */
  733. function the_comment() {
  734. global $wp_query;
  735. return $wp_query->the_comment();
  736. }
  737. /*
  738. * WP_Query
  739. */
  740. /**
  741. * The WordPress Query class.
  742. *
  743. * @link https://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  744. *
  745. * @since 1.5.0
  746. * @since 4.5.0 Removed the `$comments_popup` property.
  747. */
  748. class WP_Query {
  749. /**
  750. * Query vars set by the user
  751. *
  752. * @since 1.5.0
  753. * @access public
  754. * @var array
  755. */
  756. public $query;
  757. /**
  758. * Query vars, after parsing
  759. *
  760. * @since 1.5.0
  761. * @access public
  762. * @var array
  763. */
  764. public $query_vars = array();
  765. /**
  766. * Taxonomy query, as passed to get_tax_sql()
  767. *
  768. * @since 3.1.0
  769. * @access public
  770. * @var object WP_Tax_Query
  771. */
  772. public $tax_query;
  773. /**
  774. * Metadata query container
  775. *
  776. * @since 3.2.0
  777. * @access public
  778. * @var object WP_Meta_Query
  779. */
  780. public $meta_query = false;
  781. /**
  782. * Date query container
  783. *
  784. * @since 3.7.0
  785. * @access public
  786. * @var object WP_Date_Query
  787. */
  788. public $date_query = false;
  789. /**
  790. * Holds the data for a single object that is queried.
  791. *
  792. * Holds the contents of a post, page, category, attachment.
  793. *
  794. * @since 1.5.0
  795. * @access public
  796. * @var object|array
  797. */
  798. public $queried_object;
  799. /**
  800. * The ID of the queried object.
  801. *
  802. * @since 1.5.0
  803. * @access public
  804. * @var int
  805. */
  806. public $queried_object_id;
  807. /**
  808. * Get post database query.
  809. *
  810. * @since 2.0.1
  811. * @access public
  812. * @var string
  813. */
  814. public $request;
  815. /**
  816. * List of posts.
  817. *
  818. * @since 1.5.0
  819. * @access public
  820. * @var array
  821. */
  822. public $posts;
  823. /**
  824. * The amount of posts for the current query.
  825. *
  826. * @since 1.5.0
  827. * @access public
  828. * @var int
  829. */
  830. public $post_count = 0;
  831. /**
  832. * Index of the current item in the loop.
  833. *
  834. * @since 1.5.0
  835. * @access public
  836. * @var int
  837. */
  838. public $current_post = -1;
  839. /**
  840. * Whether the loop has started and the caller is in the loop.
  841. *
  842. * @since 2.0.0
  843. * @access public
  844. * @var bool
  845. */
  846. public $in_the_loop = false;
  847. /**
  848. * The current post.
  849. *
  850. * @since 1.5.0
  851. * @access public
  852. * @var WP_Post
  853. */
  854. public $post;
  855. /**
  856. * The list of comments for current post.
  857. *
  858. * @since 2.2.0
  859. * @access public
  860. * @var array
  861. */
  862. public $comments;
  863. /**
  864. * The amount of comments for the posts.
  865. *
  866. * @since 2.2.0
  867. * @access public
  868. * @var int
  869. */
  870. public $comment_count = 0;
  871. /**
  872. * The index of the comment in the comment loop.
  873. *
  874. * @since 2.2.0
  875. * @access public
  876. * @var int
  877. */
  878. public $current_comment = -1;
  879. /**
  880. * Current comment ID.
  881. *
  882. * @since 2.2.0
  883. * @access public
  884. * @var int
  885. */
  886. public $comment;
  887. /**
  888. * The amount of found posts for the current query.
  889. *
  890. * If limit clause was not used, equals $post_count.
  891. *
  892. * @since 2.1.0
  893. * @access public
  894. * @var int
  895. */
  896. public $found_posts = 0;
  897. /**
  898. * The amount of pages.
  899. *
  900. * @since 2.1.0
  901. * @access public
  902. * @var int
  903. */
  904. public $max_num_pages = 0;
  905. /**
  906. * The amount of comment pages.
  907. *
  908. * @since 2.7.0
  909. * @access public
  910. * @var int
  911. */
  912. public $max_num_comment_pages = 0;
  913. /**
  914. * Set if query is single post.
  915. *
  916. * @since 1.5.0
  917. * @access public
  918. * @var bool
  919. */
  920. public $is_single = false;
  921. /**
  922. * Set if query is preview of blog.
  923. *
  924. * @since 2.0.0
  925. * @access public
  926. * @var bool
  927. */
  928. public $is_preview = false;
  929. /**
  930. * Set if query returns a page.
  931. *
  932. * @since 1.5.0
  933. * @access public
  934. * @var bool
  935. */
  936. public $is_page = false;
  937. /**
  938. * Set if query is an archive list.
  939. *
  940. * @since 1.5.0
  941. * @access public
  942. * @var bool
  943. */
  944. public $is_archive = false;
  945. /**
  946. * Set if query is part of a date.
  947. *
  948. * @since 1.5.0
  949. * @access public
  950. * @var bool
  951. */
  952. public $is_date = false;
  953. /**
  954. * Set if query contains a year.
  955. *
  956. * @since 1.5.0
  957. * @access public
  958. * @var bool
  959. */
  960. public $is_year = false;
  961. /**
  962. * Set if query contains a month.
  963. *
  964. * @since 1.5.0
  965. * @access public
  966. * @var bool
  967. */
  968. public $is_month = false;
  969. /**
  970. * Set if query contains a day.
  971. *
  972. * @since 1.5.0
  973. * @access public
  974. * @var bool
  975. */
  976. public $is_day = false;
  977. /**
  978. * Set if query contains time.
  979. *
  980. * @since 1.5.0
  981. * @access public
  982. * @var bool
  983. */
  984. public $is_time = false;
  985. /**
  986. * Set if query contains an author.
  987. *
  988. * @since 1.5.0
  989. * @access public
  990. * @var bool
  991. */
  992. public $is_author = false;
  993. /**
  994. * Set if query contains category.
  995. *
  996. * @since 1.5.0
  997. * @access public
  998. * @var bool
  999. */
  1000. public $is_category = false;
  1001. /**
  1002. * Set if query contains tag.
  1003. *
  1004. * @since 2.3.0
  1005. * @access public
  1006. * @var bool
  1007. */
  1008. public $is_tag = false;
  1009. /**
  1010. * Set if query contains taxonomy.
  1011. *
  1012. * @since 2.5.0
  1013. * @access public
  1014. * @var bool
  1015. */
  1016. public $is_tax = false;
  1017. /**
  1018. * Set if query was part of a search result.
  1019. *
  1020. * @since 1.5.0
  1021. * @access public
  1022. * @var bool
  1023. */
  1024. public $is_search = false;
  1025. /**
  1026. * Set if query is feed display.
  1027. *
  1028. * @since 1.5.0
  1029. * @access public
  1030. * @var bool
  1031. */
  1032. public $is_feed = false;
  1033. /**
  1034. * Set if query is comment feed display.
  1035. *
  1036. * @since 2.2.0
  1037. * @access public
  1038. * @var bool
  1039. */
  1040. public $is_comment_feed = false;
  1041. /**
  1042. * Set if query is trackback.
  1043. *
  1044. * @since 1.5.0
  1045. * @access public
  1046. * @var bool
  1047. */
  1048. public $is_trackback = false;
  1049. /**
  1050. * Set if query is blog homepage.
  1051. *
  1052. * @since 1.5.0
  1053. * @access public
  1054. * @var bool
  1055. */
  1056. public $is_home = false;
  1057. /**
  1058. * Set if query couldn't found anything.
  1059. *
  1060. * @since 1.5.0
  1061. * @access public
  1062. * @var bool
  1063. */
  1064. public $is_404 = false;
  1065. /**
  1066. * Set if query is embed.
  1067. *
  1068. * @since 4.4.0
  1069. * @access public
  1070. * @var bool
  1071. */
  1072. public $is_embed = false;
  1073. /**
  1074. * Set if query is paged
  1075. *
  1076. * @since 1.5.0
  1077. * @access public
  1078. * @var bool
  1079. */
  1080. public $is_paged = false;
  1081. /**
  1082. * Set if query is part of administration page.
  1083. *
  1084. * @since 1.5.0
  1085. * @access public
  1086. * @var bool
  1087. */
  1088. public $is_admin = false;
  1089. /**
  1090. * Set if query is an attachment.
  1091. *
  1092. * @since 2.0.0
  1093. * @access public
  1094. * @var bool
  1095. */
  1096. public $is_attachment = false;
  1097. /**
  1098. * Set if is single, is a page, or is an attachment.
  1099. *
  1100. * @since 2.1.0
  1101. * @access public
  1102. * @var bool
  1103. */
  1104. public $is_singular = false;
  1105. /**
  1106. * Set if query is for robots.
  1107. *
  1108. * @since 2.1.0
  1109. * @access public
  1110. * @var bool
  1111. */
  1112. public $is_robots = false;
  1113. /**
  1114. * Set if query contains posts.
  1115. *
  1116. * Basically, the homepage if the option isn't set for the static homepage.
  1117. *
  1118. * @since 2.1.0
  1119. * @access public
  1120. * @var bool
  1121. */
  1122. public $is_posts_page = false;
  1123. /**
  1124. * Set if query is for a post type archive.
  1125. *
  1126. * @since 3.1.0
  1127. * @access public
  1128. * @var bool
  1129. */
  1130. public $is_post_type_archive = false;
  1131. /**
  1132. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1133. * whether we have to re-parse because something has changed
  1134. *
  1135. * @since 3.1.0
  1136. * @access private
  1137. * @var bool|string
  1138. */
  1139. private $query_vars_hash = false;
  1140. /**
  1141. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1142. * via pre_get_posts hooks.
  1143. *
  1144. * @since 3.1.1
  1145. * @access private
  1146. */
  1147. private $query_vars_changed = true;
  1148. /**
  1149. * Set if post thumbnails are cached
  1150. *
  1151. * @since 3.2.0
  1152. * @access public
  1153. * @var bool
  1154. */
  1155. public $thumbnails_cached = false;
  1156. /**
  1157. * Cached list of search stopwords.
  1158. *
  1159. * @since 3.7.0
  1160. * @var array
  1161. */
  1162. private $stopwords;
  1163. private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' );
  1164. private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
  1165. /**
  1166. * Resets query flags to false.
  1167. *
  1168. * The query flags are what page info WordPress was able to figure out.
  1169. *
  1170. * @since 2.0.0
  1171. * @access private
  1172. */
  1173. private function init_query_flags() {
  1174. $this->is_single = false;
  1175. $this->is_preview = false;
  1176. $this->is_page = false;
  1177. $this->is_archive = false;
  1178. $this->is_date = false;
  1179. $this->is_year = false;
  1180. $this->is_month = false;
  1181. $this->is_day = false;
  1182. $this->is_time = false;
  1183. $this->is_author = false;
  1184. $this->is_category = false;
  1185. $this->is_tag = false;
  1186. $this->is_tax = false;
  1187. $this->is_search = false;
  1188. $this->is_feed = false;
  1189. $this->is_comment_feed = false;
  1190. $this->is_trackback = false;
  1191. $this->is_home = false;
  1192. $this->is_404 = false;
  1193. $this->is_paged = false;
  1194. $this->is_admin = false;
  1195. $this->is_attachment = false;
  1196. $this->is_singular = false;
  1197. $this->is_robots = false;
  1198. $this->is_posts_page = false;
  1199. $this->is_post_type_archive = false;
  1200. }
  1201. /**
  1202. * Initiates object properties and sets default values.
  1203. *
  1204. * @since 1.5.0
  1205. * @access public
  1206. */
  1207. public function init() {
  1208. unset($this->posts);
  1209. unset($this->query);
  1210. $this->query_vars = array();
  1211. unset($this->queried_object);
  1212. unset($this->queried_object_id);
  1213. $this->post_count = 0;
  1214. $this->current_post = -1;
  1215. $this->in_the_loop = false;
  1216. unset( $this->request );
  1217. unset( $this->post );
  1218. unset( $this->comments );
  1219. unset( $this->comment );
  1220. $this->comment_count = 0;
  1221. $this->current_comment = -1;
  1222. $this->found_posts = 0;
  1223. $this->max_num_pages = 0;
  1224. $this->max_num_comment_pages = 0;
  1225. $this->init_query_flags();
  1226. }
  1227. /**
  1228. * Reparse the query vars.
  1229. *
  1230. * @since 1.5.0
  1231. * @access public
  1232. */
  1233. public function parse_query_vars() {
  1234. $this->parse_query();
  1235. }
  1236. /**
  1237. * Fills in the query variables, which do not exist within the parameter.
  1238. *
  1239. * @since 2.1.0
  1240. * @since 4.4.0 Removed the `comments_popup` public query variable.
  1241. * @access public
  1242. *
  1243. * @param array $array Defined query variables.
  1244. * @return array Complete query variables with undefined ones filled in empty.
  1245. */
  1246. public function fill_query_vars($array) {
  1247. $keys = array(
  1248. 'error'
  1249. , 'm'
  1250. , 'p'
  1251. , 'post_parent'
  1252. , 'subpost'
  1253. , 'subpost_id'
  1254. , 'attachment'
  1255. , 'attachment_id'
  1256. , 'name'
  1257. , 'static'
  1258. , 'pagename'
  1259. , 'page_id'
  1260. , 'second'
  1261. , 'minute'
  1262. , 'hour'
  1263. , 'day'
  1264. , 'monthnum'
  1265. , 'year'
  1266. , 'w'
  1267. , 'category_name'
  1268. , 'tag'
  1269. , 'cat'
  1270. , 'tag_id'
  1271. , 'author'
  1272. , 'author_name'
  1273. , 'feed'
  1274. , 'tb'
  1275. , 'paged'
  1276. , 'meta_key'
  1277. , 'meta_value'
  1278. , 'preview'
  1279. , 's'
  1280. , 'sentence'
  1281. , 'title'
  1282. , 'fields'
  1283. , 'menu_order'
  1284. , 'embed'
  1285. );
  1286. foreach ( $keys as $key ) {
  1287. if ( !isset($array[$key]) )
  1288. $array[$key] = '';
  1289. }
  1290. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 'post_name__in',
  1291. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
  1292. 'author__in', 'author__not_in' );
  1293. foreach ( $array_keys as $key ) {
  1294. if ( !isset($array[$key]) )
  1295. $array[$key] = array();
  1296. }
  1297. return $array;
  1298. }
  1299. /**
  1300. * Parse a query string and set query type booleans.
  1301. *
  1302. * @since 1.5.0
  1303. * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
  1304. * array key to `$orderby`.
  1305. * @since 4.4.0 Introduced `$post_name__in` and `$title` parameters. `$s` was updated to support excluded
  1306. * search terms, by prepending a hyphen.
  1307. * @since 4.5.0 Removed the `$comments_popup` parameter.
  1308. * Introduced the `$comment_status` and `$ping_status` parameters.
  1309. * Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
  1310. * @access public
  1311. *
  1312. * @param string|array $query {
  1313. * Optional. Array or string of Query parameters.
  1314. *
  1315. * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type.
  1316. * @type int|string $author Author ID, or comma-separated list of IDs.
  1317. * @type string $author_name User 'user_nicename'.
  1318. * @type array $author__in An array of author IDs to query from.
  1319. * @type array $author__not_in An array of author IDs not to query from.
  1320. * @type bool $cache_results Whether to cache post information. Default true.
  1321. * @type int|string $cat Category ID or comma-separated list of IDs (this or any children).
  1322. * @type array $category__and An array of category IDs (AND in).
  1323. * @type array $category__in An array of category IDs (OR in, no children).
  1324. * @type array $category__not_in An array of category IDs (NOT in).
  1325. * @type string $category_name Use category slug (not name, this or any children).
  1326. * @type string $comment_status Comment status.
  1327. * @type int $comments_per_page The number of comments to return per page.
  1328. * Default 'comments_per_page' option.
  1329. * @type array $date_query An associative array of WP_Date_Query arguments.
  1330. * {@see WP_Date_Query::__construct()}
  1331. * @type int $day Day of the month. Default empty. Accepts numbers 1-31.
  1332. * @type bool $exact Whether to search by exact keyword. Default false.
  1333. * @type string|array $fields Which fields to return. Single field or all fields (string),
  1334. * or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
  1335. * Default all fields. Accepts 'ids', 'id=>parent'.
  1336. * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23.
  1337. * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
  1338. * excludes stickies from 'post__in'. Accepts 1|true, 0|false.
  1339. * Default 0|false.
  1340. * @type int $m Combination YearMonth. Accepts any four-digit year and month
  1341. * numbers 1-12. Default empty.
  1342. * @type string $meta_compare Comparison operator to test the 'meta_value'.
  1343. * @type string $meta_key Custom field key.
  1344. * @type array $meta_query An associative array of WP_Meta_Query arguments.
  1345. * {@see WP_Meta_Query}
  1346. * @type string $meta_value Custom field value.
  1347. * @type int $meta_value_num Custom field value number.
  1348. * @type int $menu_order The menu order of the posts.
  1349. * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12.
  1350. * @type string $name Post slug.
  1351. * @type bool $nopaging Show all posts (true) or paginate (false). Default false.
  1352. * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve
  1353. * performance. Default false.
  1354. * @type int $offset The number of posts to offset before retrieval.
  1355. * @type string $order Designates ascending or descending order of posts. Default 'DESC'.
  1356. * Accepts 'ASC', 'DESC'.
  1357. * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be
  1358. * passed. To use 'meta_value', or 'meta_value_num',
  1359. * 'meta_key=keyname' must be also be defined. To sort by a
  1360. * specific `$meta_query` clause, use that clause's array key.
  1361. * Default 'date'. Accepts 'none', 'name', 'author', 'date',
  1362. * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',
  1363. * 'RAND(x)' (where 'x' is an integer seed value),
  1364. * 'comment_count', 'meta_value', 'meta_value_num', 'post__in',
  1365. * and the array keys of `$meta_query`.
  1366. * @type int $p Post ID.
  1367. * @type int $page Show the number of posts that would show up on page X of a
  1368. * static front page.
  1369. * @type int $paged The number of the current page.
  1370. * @type int $page_id Page ID.
  1371. * @type string $pagename Page slug.
  1372. * @type string $perm Show posts if user has the appropriate capability.
  1373. * @type string $ping_status Ping status.
  1374. * @type array $post__in An array of post IDs to retrieve, sticky posts will be included
  1375. * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type.
  1376. * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma-
  1377. * separated IDs will NOT work.
  1378. * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve
  1379. * top-level pages.
  1380. * @type array $post_parent__in An array containing parent page IDs to query child pages from.
  1381. * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from.
  1382. * @type string|array $post_type A post type slug (string) or array of post type slugs.
  1383. * Default 'any' if using 'tax_query'.
  1384. * @type string|array $post_status A post status (string) or array of post statuses.
  1385. * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts.
  1386. * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
  1387. * 'posts_per_page' when is_archive(), or is_search() are true.
  1388. * @type array $post_name__in An array of post slugs that results must match.
  1389. * @type string $s Search keyword(s). Prepending a term with a hyphen will
  1390. * exclude posts matching that term. Eg, 'pillow -sofa' will
  1391. * return posts containing 'pillow' but not 'sofa'.
  1392. * @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
  1393. * @type bool $sentence Whether to search by phrase. Default false.
  1394. * @type bool $suppress_filters Whether to suppress filters. Default false.
  1395. * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all).
  1396. * @type array $tag__and An array of tag ids (AND in).
  1397. * @type array $tag__in An array of tag ids (OR in).
  1398. * @type array $tag__not_in An array of tag ids (NOT in).
  1399. * @type int $tag_id Tag id or comma-separated list of IDs.
  1400. * @type array $tag_slug__and An array of tag slugs (AND in).
  1401. * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is
  1402. * true. Note: a string of comma-separated IDs will NOT work.
  1403. * @type array $tax_query An associative array of WP_Tax_Query arguments.
  1404. * {@see WP_Tax_Query->queries}
  1405. * @type string $title Post title.
  1406. * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
  1407. * @type bool $update_post_term_cache Whether to update the post term cache. Default true.
  1408. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
  1409. * @type int $year The four-digit year. Default empty. Accepts any four-digit year.
  1410. * }
  1411. */
  1412. public function parse_query( $query = '' ) {
  1413. if ( ! empty( $query ) ) {
  1414. $this->init();
  1415. $this->query = $this->query_vars = wp_parse_args( $query );
  1416. } elseif ( ! isset( $this->query ) ) {
  1417. $this->query = $this->query_vars;
  1418. }
  1419. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1420. $qv = &$this->query_vars;
  1421. $this->query_vars_changed = true;
  1422. if ( ! empty($qv['robots']) )
  1423. $this->is_robots = true;
  1424. $qv['p'] = absint($qv['p']);
  1425. $qv['page_id'] = absint($qv['page_id']);
  1426. $qv['year'] = absint($qv['year']);
  1427. $qv['monthnum'] = absint($qv['monthnum']);
  1428. $qv['day'] = absint($qv['day']);
  1429. $qv['w'] = absint($qv['w']);
  1430. $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
  1431. $qv['paged'] = absint($qv['paged']);
  1432. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1433. $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
  1434. $qv['pagename'] = trim( $qv['pagename'] );
  1435. $qv['name'] = trim( $qv['name'] );
  1436. $qv['title'] = trim( $qv['title'] );
  1437. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1438. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1439. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1440. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  1441. // Fairly insane upper bound for search string lengths.
  1442. if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
  1443. $qv['s'] = '';
  1444. }
  1445. // Compat. Map subpost to attachment.
  1446. if ( '' != $qv['subpost'] )
  1447. $qv['attachment'] = $qv['subpost'];
  1448. if ( '' != $qv['subpost_id'] )
  1449. $qv['attachment_id'] = $qv['subpost_id'];
  1450. $qv['attachment_id'] = absint($qv['attachment_id']);
  1451. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1452. $this->is_single = true;
  1453. $this->is_attachment = true;
  1454. } elseif ( '' != $qv['name'] ) {
  1455. $this->is_single = true;
  1456. } elseif ( $qv['p'] ) {
  1457. $this->is_single = true;
  1458. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1459. // If year, month, day, hour, minute, and second are set, a single
  1460. // post is being queried.
  1461. $this->is_single = true;
  1462. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1463. $this->is_page = true;
  1464. $this->is_single = false;
  1465. } else {
  1466. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1467. if ( isset( $this->query['s'] ) ) {
  1468. $this->is_search = true;
  1469. }
  1470. if ( '' !== $qv['second'] ) {
  1471. $this->is_time = true;
  1472. $this->is_date = true;
  1473. }
  1474. if ( '' !== $qv['minute'] ) {
  1475. $this->is_time = true;
  1476. $this->is_date = true;
  1477. }
  1478. if ( '' !== $qv['hour'] ) {
  1479. $this->is_time = true;
  1480. $this->is_date = true;
  1481. }
  1482. if ( $qv['day'] ) {
  1483. if ( ! $this->is_date ) {
  1484. $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
  1485. if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
  1486. $qv['error'] = '404';
  1487. } else {
  1488. $this->is_day = true;
  1489. $this->is_date = true;
  1490. }
  1491. }
  1492. }
  1493. if ( $qv['monthnum'] ) {
  1494. if ( ! $this->is_date ) {
  1495. if ( 12 < $qv['monthnum'] ) {
  1496. $qv['error'] = '404';
  1497. } else {
  1498. $this->is_month = true;
  1499. $this->is_date = true;
  1500. }
  1501. }
  1502. }
  1503. if ( $qv['year'] ) {
  1504. if ( ! $this->is_date ) {
  1505. $this->is_year = true;
  1506. $this->is_date = true;
  1507. }
  1508. }
  1509. if ( $qv['m'] ) {
  1510. $this->is_date = true;
  1511. if ( strlen($qv['m']) > 9 ) {
  1512. $this->is_time = true;
  1513. } elseif ( strlen( $qv['m'] ) > 7 ) {
  1514. $this->is_day = true;
  1515. } elseif ( strlen( $qv['m'] ) > 5 ) {
  1516. $this->is_month = true;
  1517. } else {
  1518. $this->is_year = true;
  1519. }
  1520. }
  1521. if ( '' != $qv['w'] ) {
  1522. $this->is_date = true;
  1523. }
  1524. $this->query_vars_hash = false;
  1525. $this->parse_tax_query( $qv );
  1526. foreach ( $this->tax_query->queries as $tax_query ) {
  1527. if ( ! is_array( $tax_query ) ) {
  1528. continue;
  1529. }
  1530. if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
  1531. switch ( $tax_query['taxonomy'] ) {
  1532. case 'category':
  1533. $this->is_category = true;
  1534. break;
  1535. case 'post_tag':
  1536. $this->is_tag = true;
  1537. break;
  1538. default:
  1539. $this->is_tax = true;
  1540. }
  1541. }
  1542. }
  1543. unset( $tax_query );
  1544. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1545. $this->is_author = false;
  1546. } else {
  1547. $this->is_author = true;
  1548. }
  1549. if ( '' != $qv['author_name'] )
  1550. $this->is_author = true;
  1551. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1552. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1553. if ( ! empty( $post_type_obj->has_archive ) )
  1554. $this->is_post_type_archive = true;
  1555. }
  1556. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1557. $this->is_archive = true;
  1558. }
  1559. if ( '' != $qv['feed'] )
  1560. $this->is_feed = true;
  1561. if ( '' != $qv['embed'] ) {
  1562. $this->is_embed = true;
  1563. }
  1564. if ( '' != $qv['tb'] )
  1565. $this->is_trackback = true;
  1566. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1567. $this->is_paged = true;
  1568. // if we're previewing inside the write screen
  1569. if ( '' != $qv['preview'] )
  1570. $this->is_preview = true;
  1571. if ( is_admin() )
  1572. $this->is_admin = true;
  1573. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1574. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1575. $qv['withcomments'] = 1;
  1576. }
  1577. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1578. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1579. $this->is_comment_feed = true;
  1580. if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots ) )
  1581. $this->is_home = true;
  1582. // Correct is_* for page_on_front and page_for_posts
  1583. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1584. $_query = wp_parse_args($this->query);
  1585. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1586. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1587. unset($_query['pagename']);
  1588. unset( $_query['embed'] );
  1589. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1590. $this->is_page = true;
  1591. $this->is_home = false;
  1592. $qv['page_id'] = get_option('page_on_front');
  1593. // Correct <!--nextpage--> for page_on_front
  1594. if ( !empty($qv['paged']) ) {
  1595. $qv['page'] = $qv['paged'];
  1596. unset($qv['paged']);
  1597. }
  1598. }
  1599. }
  1600. if ( '' != $qv['pagename'] ) {
  1601. $this->queried_object = get_page_by_path( $qv['pagename'] );
  1602. if ( $this->queried_object && 'attachment' == $this->queried_object->post_type ) {
  1603. if ( preg_match( "/^[^%]*%(?:postname)%/", get_option( 'permalink_structure' ) ) ) {
  1604. // See if we also have a post with the same slug
  1605. $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' );
  1606. if ( $post ) {
  1607. $this->queried_object = $post;
  1608. $this->is_page = false;
  1609. $this->is_single = true;
  1610. }
  1611. }
  1612. }
  1613. if ( ! empty( $this->queried_object ) ) {
  1614. $this->queried_object_id = (int) $this->queried_object->ID;
  1615. } else {
  1616. unset( $this->queried_object );
  1617. }
  1618. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1619. $this->is_page = false;
  1620. $this->is_home = true;
  1621. $this->is_posts_page = true;
  1622. }
  1623. }
  1624. if ( $qv['page_id'] ) {
  1625. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1626. $this->is_page = false;
  1627. $this->is_home = true;
  1628. $this->is_posts_page = true;
  1629. }
  1630. }
  1631. if ( !empty($qv['post_type']) ) {
  1632. if ( is_array($qv['post_type']) )
  1633. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1634. else
  1635. $qv['post_type'] = sanitize_key($qv['post_type']);
  1636. }
  1637. if ( ! empty( $qv['post_status'] ) ) {
  1638. if ( is_array( $qv['post_status'] ) )
  1639. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1640. else
  1641. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1642. }
  1643. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1644. $this->is_comment_feed = false;
  1645. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1646. // Done correcting is_* for page_on_front and page_for_posts
  1647. if ( '404' == $qv['error'] )
  1648. $this->set_404();
  1649. $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 );
  1650. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1651. $this->query_vars_changed = false;
  1652. /**
  1653. * Fires after the main query vars have been parsed.
  1654. *
  1655. * @since 1.5.0
  1656. *
  1657. * @param WP_Query &$this The WP_Query instance (passed by reference).
  1658. */