PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

https://github.com/webgefrickel/frckl-init-wordpress
PHP | 3943 lines | 2085 code | 478 blank | 1380 comment | 572 complexity | 481e1c239ed9570676ea04f283b56229 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-2.0

Large files files are truncated, but you can click here to view the full 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 http://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. * @see WP_Query::get()
  17. * @since 1.5.0
  18. * @uses $wp_query
  19. *
  20. * @param string $var The variable key to retrieve.
  21. * @return mixed
  22. */
  23. function get_query_var($var) {
  24. global $wp_query;
  25. return $wp_query->get($var);
  26. }
  27. /**
  28. * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
  29. *
  30. * @uses WP_Query::get_queried_object
  31. *
  32. * @since 3.1.0
  33. * @access public
  34. *
  35. * @return object
  36. */
  37. function get_queried_object() {
  38. global $wp_query;
  39. return $wp_query->get_queried_object();
  40. }
  41. /**
  42. * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
  43. *
  44. * @uses WP_Query::get_queried_object_id()
  45. *
  46. * @since 3.1.0
  47. * @access public
  48. *
  49. * @return int
  50. */
  51. function get_queried_object_id() {
  52. global $wp_query;
  53. return $wp_query->get_queried_object_id();
  54. }
  55. /**
  56. * Set query variable.
  57. *
  58. * @see WP_Query::set()
  59. * @since 2.2.0
  60. * @uses $wp_query
  61. *
  62. * @param string $var Query variable key.
  63. * @param mixed $value
  64. * @return null
  65. */
  66. function set_query_var($var, $value) {
  67. global $wp_query;
  68. return $wp_query->set($var, $value);
  69. }
  70. /**
  71. * Set up The Loop with query parameters.
  72. *
  73. * This will override the current WordPress Loop and shouldn't be used more than
  74. * once. This must not be used within the WordPress Loop.
  75. *
  76. * @since 1.5.0
  77. * @uses $wp_query
  78. *
  79. * @param string $query
  80. * @return array List of posts
  81. */
  82. function query_posts($query) {
  83. $GLOBALS['wp_query'] = new WP_Query();
  84. return $GLOBALS['wp_query']->query($query);
  85. }
  86. /**
  87. * Destroy the previous query and set up a new query.
  88. *
  89. * This should be used after {@link query_posts()} and before another {@link
  90. * query_posts()}. This will remove obscure bugs that occur when the previous
  91. * wp_query object is not destroyed properly before another is set up.
  92. *
  93. * @since 2.3.0
  94. * @uses $wp_query
  95. */
  96. function wp_reset_query() {
  97. $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
  98. wp_reset_postdata();
  99. }
  100. /**
  101. * After looping through a separate query, this function restores
  102. * the $post global to the current post in the main query.
  103. *
  104. * @since 3.0.0
  105. * @uses $wp_query
  106. */
  107. function wp_reset_postdata() {
  108. global $wp_query;
  109. $wp_query->reset_postdata();
  110. }
  111. /*
  112. * Query type checks.
  113. */
  114. /**
  115. * Is the query for an existing archive page?
  116. *
  117. * Month, Year, Category, Author, Post Type archive...
  118. *
  119. * @see WP_Query::is_archive()
  120. * @since 1.5.0
  121. * @uses $wp_query
  122. *
  123. * @return bool
  124. */
  125. function is_archive() {
  126. global $wp_query;
  127. if ( ! isset( $wp_query ) ) {
  128. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  129. return false;
  130. }
  131. return $wp_query->is_archive();
  132. }
  133. /**
  134. * Is the query for an existing post type archive page?
  135. *
  136. * @see WP_Query::is_post_type_archive()
  137. * @since 3.1.0
  138. * @uses $wp_query
  139. *
  140. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  141. * @return bool
  142. */
  143. function is_post_type_archive( $post_types = '' ) {
  144. global $wp_query;
  145. if ( ! isset( $wp_query ) ) {
  146. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  147. return false;
  148. }
  149. return $wp_query->is_post_type_archive( $post_types );
  150. }
  151. /**
  152. * Is the query for an existing attachment page?
  153. *
  154. * @see WP_Query::is_attachment()
  155. * @since 2.0.0
  156. * @uses $wp_query
  157. *
  158. * @return bool
  159. */
  160. function is_attachment() {
  161. global $wp_query;
  162. if ( ! isset( $wp_query ) ) {
  163. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  164. return false;
  165. }
  166. return $wp_query->is_attachment();
  167. }
  168. /**
  169. * Is the query for an existing author archive page?
  170. *
  171. * If the $author parameter is specified, this function will additionally
  172. * check if the query is for one of the authors specified.
  173. *
  174. * @see WP_Query::is_author()
  175. * @since 1.5.0
  176. * @uses $wp_query
  177. *
  178. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  179. * @return bool
  180. */
  181. function is_author( $author = '' ) {
  182. global $wp_query;
  183. if ( ! isset( $wp_query ) ) {
  184. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  185. return false;
  186. }
  187. return $wp_query->is_author( $author );
  188. }
  189. /**
  190. * Is the query for an existing category archive page?
  191. *
  192. * If the $category parameter is specified, this function will additionally
  193. * check if the query is for one of the categories specified.
  194. *
  195. * @see WP_Query::is_category()
  196. * @since 1.5.0
  197. * @uses $wp_query
  198. *
  199. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  200. * @return bool
  201. */
  202. function is_category( $category = '' ) {
  203. global $wp_query;
  204. if ( ! isset( $wp_query ) ) {
  205. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  206. return false;
  207. }
  208. return $wp_query->is_category( $category );
  209. }
  210. /**
  211. * Is the query for an existing tag archive page?
  212. *
  213. * If the $tag parameter is specified, this function will additionally
  214. * check if the query is for one of the tags specified.
  215. *
  216. * @see WP_Query::is_tag()
  217. * @since 2.3.0
  218. * @uses $wp_query
  219. *
  220. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  221. * @return bool
  222. */
  223. function is_tag( $tag = '' ) {
  224. global $wp_query;
  225. if ( ! isset( $wp_query ) ) {
  226. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  227. return false;
  228. }
  229. return $wp_query->is_tag( $tag );
  230. }
  231. /**
  232. * Is the query for an existing taxonomy archive page?
  233. *
  234. * If the $taxonomy parameter is specified, this function will additionally
  235. * check if the query is for that specific $taxonomy.
  236. *
  237. * If the $term parameter is specified in addition to the $taxonomy parameter,
  238. * this function will additionally check if the query is for one of the terms
  239. * specified.
  240. *
  241. * @see WP_Query::is_tax()
  242. * @since 2.5.0
  243. * @uses $wp_query
  244. *
  245. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  246. * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  247. * @return bool
  248. */
  249. function is_tax( $taxonomy = '', $term = '' ) {
  250. global $wp_query;
  251. if ( ! isset( $wp_query ) ) {
  252. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  253. return false;
  254. }
  255. return $wp_query->is_tax( $taxonomy, $term );
  256. }
  257. /**
  258. * Whether the current URL is within the comments popup window.
  259. *
  260. * @see WP_Query::is_comments_popup()
  261. * @since 1.5.0
  262. * @uses $wp_query
  263. *
  264. * @return bool
  265. */
  266. function is_comments_popup() {
  267. global $wp_query;
  268. if ( ! isset( $wp_query ) ) {
  269. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  270. return false;
  271. }
  272. return $wp_query->is_comments_popup();
  273. }
  274. /**
  275. * Is the query for an existing date archive?
  276. *
  277. * @see WP_Query::is_date()
  278. * @since 1.5.0
  279. * @uses $wp_query
  280. *
  281. * @return bool
  282. */
  283. function is_date() {
  284. global $wp_query;
  285. if ( ! isset( $wp_query ) ) {
  286. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  287. return false;
  288. }
  289. return $wp_query->is_date();
  290. }
  291. /**
  292. * Is the query for an existing day archive?
  293. *
  294. * @see WP_Query::is_day()
  295. * @since 1.5.0
  296. * @uses $wp_query
  297. *
  298. * @return bool
  299. */
  300. function is_day() {
  301. global $wp_query;
  302. if ( ! isset( $wp_query ) ) {
  303. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  304. return false;
  305. }
  306. return $wp_query->is_day();
  307. }
  308. /**
  309. * Is the query for a feed?
  310. *
  311. * @see WP_Query::is_feed()
  312. * @since 1.5.0
  313. * @uses $wp_query
  314. *
  315. * @param string|array $feeds Optional feed types to check.
  316. * @return bool
  317. */
  318. function is_feed( $feeds = '' ) {
  319. global $wp_query;
  320. if ( ! isset( $wp_query ) ) {
  321. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  322. return false;
  323. }
  324. return $wp_query->is_feed( $feeds );
  325. }
  326. /**
  327. * Is the query for a comments feed?
  328. *
  329. * @see WP_Query::is_comments_feed()
  330. * @since 3.0.0
  331. * @uses $wp_query
  332. *
  333. * @return bool
  334. */
  335. function is_comment_feed() {
  336. global $wp_query;
  337. if ( ! isset( $wp_query ) ) {
  338. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  339. return false;
  340. }
  341. return $wp_query->is_comment_feed();
  342. }
  343. /**
  344. * Is the query for the front page of the site?
  345. *
  346. * This is for what is displayed at your site's main URL.
  347. *
  348. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  349. *
  350. * If you set a static page for the front page of your site, this function will return
  351. * true when viewing that page.
  352. *
  353. * Otherwise the same as @see is_home()
  354. *
  355. * @see WP_Query::is_front_page()
  356. * @since 2.5.0
  357. * @uses is_home()
  358. * @uses get_option()
  359. *
  360. * @return bool True, if front of site.
  361. */
  362. function is_front_page() {
  363. global $wp_query;
  364. if ( ! isset( $wp_query ) ) {
  365. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  366. return false;
  367. }
  368. return $wp_query->is_front_page();
  369. }
  370. /**
  371. * Is the query for the blog homepage?
  372. *
  373. * This is the page which shows the time based blog content of your site.
  374. *
  375. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  376. *
  377. * If you set a static page for the front page of your site, this function will return
  378. * true only on the page you set as the "Posts page".
  379. *
  380. * @see is_front_page()
  381. *
  382. * @see WP_Query::is_home()
  383. * @since 1.5.0
  384. * @uses $wp_query
  385. *
  386. * @return bool True if blog view homepage.
  387. */
  388. function is_home() {
  389. global $wp_query;
  390. if ( ! isset( $wp_query ) ) {
  391. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  392. return false;
  393. }
  394. return $wp_query->is_home();
  395. }
  396. /**
  397. * Is the query for an existing month archive?
  398. *
  399. * @see WP_Query::is_month()
  400. * @since 1.5.0
  401. * @uses $wp_query
  402. *
  403. * @return bool
  404. */
  405. function is_month() {
  406. global $wp_query;
  407. if ( ! isset( $wp_query ) ) {
  408. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  409. return false;
  410. }
  411. return $wp_query->is_month();
  412. }
  413. /**
  414. * Is the query for an existing single page?
  415. *
  416. * If the $page parameter is specified, this function will additionally
  417. * check if the query is for one of the pages specified.
  418. *
  419. * @see is_single()
  420. * @see is_singular()
  421. *
  422. * @see WP_Query::is_page()
  423. * @since 1.5.0
  424. * @uses $wp_query
  425. *
  426. * @param mixed $page Page ID, title, slug, or array of such.
  427. * @return bool
  428. */
  429. function is_page( $page = '' ) {
  430. global $wp_query;
  431. if ( ! isset( $wp_query ) ) {
  432. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  433. return false;
  434. }
  435. return $wp_query->is_page( $page );
  436. }
  437. /**
  438. * Is the query for paged result and not for the first page?
  439. *
  440. * @see WP_Query::is_paged()
  441. * @since 1.5.0
  442. * @uses $wp_query
  443. *
  444. * @return bool
  445. */
  446. function is_paged() {
  447. global $wp_query;
  448. if ( ! isset( $wp_query ) ) {
  449. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  450. return false;
  451. }
  452. return $wp_query->is_paged();
  453. }
  454. /**
  455. * Is the query for a post or page preview?
  456. *
  457. * @see WP_Query::is_preview()
  458. * @since 2.0.0
  459. * @uses $wp_query
  460. *
  461. * @return bool
  462. */
  463. function is_preview() {
  464. global $wp_query;
  465. if ( ! isset( $wp_query ) ) {
  466. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  467. return false;
  468. }
  469. return $wp_query->is_preview();
  470. }
  471. /**
  472. * Is the query for the robots file?
  473. *
  474. * @see WP_Query::is_robots()
  475. * @since 2.1.0
  476. * @uses $wp_query
  477. *
  478. * @return bool
  479. */
  480. function is_robots() {
  481. global $wp_query;
  482. if ( ! isset( $wp_query ) ) {
  483. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  484. return false;
  485. }
  486. return $wp_query->is_robots();
  487. }
  488. /**
  489. * Is the query for a search?
  490. *
  491. * @see WP_Query::is_search()
  492. * @since 1.5.0
  493. * @uses $wp_query
  494. *
  495. * @return bool
  496. */
  497. function is_search() {
  498. global $wp_query;
  499. if ( ! isset( $wp_query ) ) {
  500. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  501. return false;
  502. }
  503. return $wp_query->is_search();
  504. }
  505. /**
  506. * Is the query for an existing single post?
  507. *
  508. * Works for any post type, except attachments and pages
  509. *
  510. * If the $post parameter is specified, this function will additionally
  511. * check if the query is for one of the Posts specified.
  512. *
  513. * @see is_page()
  514. * @see is_singular()
  515. *
  516. * @see WP_Query::is_single()
  517. * @since 1.5.0
  518. * @uses $wp_query
  519. *
  520. * @param mixed $post Post ID, title, slug, or array of such.
  521. * @return bool
  522. */
  523. function is_single( $post = '' ) {
  524. global $wp_query;
  525. if ( ! isset( $wp_query ) ) {
  526. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  527. return false;
  528. }
  529. return $wp_query->is_single( $post );
  530. }
  531. /**
  532. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  533. *
  534. * If the $post_types parameter is specified, this function will additionally
  535. * check if the query is for one of the Posts Types specified.
  536. *
  537. * @see is_page()
  538. * @see is_single()
  539. *
  540. * @see WP_Query::is_singular()
  541. * @since 1.5.0
  542. * @uses $wp_query
  543. *
  544. * @param mixed $post_types Optional. Post Type or array of Post Types
  545. * @return bool
  546. */
  547. function is_singular( $post_types = '' ) {
  548. global $wp_query;
  549. if ( ! isset( $wp_query ) ) {
  550. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  551. return false;
  552. }
  553. return $wp_query->is_singular( $post_types );
  554. }
  555. /**
  556. * Is the query for a specific time?
  557. *
  558. * @see WP_Query::is_time()
  559. * @since 1.5.0
  560. * @uses $wp_query
  561. *
  562. * @return bool
  563. */
  564. function is_time() {
  565. global $wp_query;
  566. if ( ! isset( $wp_query ) ) {
  567. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  568. return false;
  569. }
  570. return $wp_query->is_time();
  571. }
  572. /**
  573. * Is the query for a trackback endpoint call?
  574. *
  575. * @see WP_Query::is_trackback()
  576. * @since 1.5.0
  577. * @uses $wp_query
  578. *
  579. * @return bool
  580. */
  581. function is_trackback() {
  582. global $wp_query;
  583. if ( ! isset( $wp_query ) ) {
  584. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  585. return false;
  586. }
  587. return $wp_query->is_trackback();
  588. }
  589. /**
  590. * Is the query for an existing year archive?
  591. *
  592. * @see WP_Query::is_year()
  593. * @since 1.5.0
  594. * @uses $wp_query
  595. *
  596. * @return bool
  597. */
  598. function is_year() {
  599. global $wp_query;
  600. if ( ! isset( $wp_query ) ) {
  601. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  602. return false;
  603. }
  604. return $wp_query->is_year();
  605. }
  606. /**
  607. * Is the query a 404 (returns no results)?
  608. *
  609. * @see WP_Query::is_404()
  610. * @since 1.5.0
  611. * @uses $wp_query
  612. *
  613. * @return bool
  614. */
  615. function is_404() {
  616. global $wp_query;
  617. if ( ! isset( $wp_query ) ) {
  618. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  619. return false;
  620. }
  621. return $wp_query->is_404();
  622. }
  623. /**
  624. * Is the query the main query?
  625. *
  626. * @since 3.3.0
  627. *
  628. * @return bool
  629. */
  630. function is_main_query() {
  631. if ( 'pre_get_posts' === current_filter() ) {
  632. $message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
  633. 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'http://codex.wordpress.org/Function_Reference/is_main_query' ) );
  634. _doing_it_wrong( __FUNCTION__, $message, '3.7' );
  635. }
  636. global $wp_query;
  637. return $wp_query->is_main_query();
  638. }
  639. /*
  640. * The Loop. Post loop control.
  641. */
  642. /**
  643. * Whether current WordPress query has results to loop over.
  644. *
  645. * @see WP_Query::have_posts()
  646. * @since 1.5.0
  647. * @uses $wp_query
  648. *
  649. * @return bool
  650. */
  651. function have_posts() {
  652. global $wp_query;
  653. return $wp_query->have_posts();
  654. }
  655. /**
  656. * Whether the caller is in the Loop.
  657. *
  658. * @since 2.0.0
  659. * @uses $wp_query
  660. *
  661. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  662. */
  663. function in_the_loop() {
  664. global $wp_query;
  665. return $wp_query->in_the_loop;
  666. }
  667. /**
  668. * Rewind the loop posts.
  669. *
  670. * @see WP_Query::rewind_posts()
  671. * @since 1.5.0
  672. * @uses $wp_query
  673. *
  674. * @return null
  675. */
  676. function rewind_posts() {
  677. global $wp_query;
  678. return $wp_query->rewind_posts();
  679. }
  680. /**
  681. * Iterate the post index in the loop.
  682. *
  683. * @see WP_Query::the_post()
  684. * @since 1.5.0
  685. * @uses $wp_query
  686. */
  687. function the_post() {
  688. global $wp_query;
  689. $wp_query->the_post();
  690. }
  691. /*
  692. * Comments loop.
  693. */
  694. /**
  695. * Whether there are comments to loop over.
  696. *
  697. * @see WP_Query::have_comments()
  698. * @since 2.2.0
  699. * @uses $wp_query
  700. *
  701. * @return bool
  702. */
  703. function have_comments() {
  704. global $wp_query;
  705. return $wp_query->have_comments();
  706. }
  707. /**
  708. * Iterate comment index in the comment loop.
  709. *
  710. * @see WP_Query::the_comment()
  711. * @since 2.2.0
  712. * @uses $wp_query
  713. *
  714. * @return object
  715. */
  716. function the_comment() {
  717. global $wp_query;
  718. return $wp_query->the_comment();
  719. }
  720. /*
  721. * WP_Query
  722. */
  723. /**
  724. * The WordPress Query class.
  725. *
  726. * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  727. *
  728. * @since 1.5.0
  729. */
  730. class WP_Query {
  731. /**
  732. * Query vars set by the user
  733. *
  734. * @since 1.5.0
  735. * @access public
  736. * @var array
  737. */
  738. var $query;
  739. /**
  740. * Query vars, after parsing
  741. *
  742. * @since 1.5.0
  743. * @access public
  744. * @var array
  745. */
  746. var $query_vars = array();
  747. /**
  748. * Taxonomy query, as passed to get_tax_sql()
  749. *
  750. * @since 3.1.0
  751. * @access public
  752. * @var object WP_Tax_Query
  753. */
  754. var $tax_query;
  755. /**
  756. * Metadata query container
  757. *
  758. * @since 3.2.0
  759. * @access public
  760. * @var object WP_Meta_Query
  761. */
  762. var $meta_query = false;
  763. /**
  764. * Date query container
  765. *
  766. * @since 3.7.0
  767. * @access public
  768. * @var object WP_Date_Query
  769. */
  770. var $date_query = false;
  771. /**
  772. * Holds the data for a single object that is queried.
  773. *
  774. * Holds the contents of a post, page, category, attachment.
  775. *
  776. * @since 1.5.0
  777. * @access public
  778. * @var object|array
  779. */
  780. var $queried_object;
  781. /**
  782. * The ID of the queried object.
  783. *
  784. * @since 1.5.0
  785. * @access public
  786. * @var int
  787. */
  788. var $queried_object_id;
  789. /**
  790. * Get post database query.
  791. *
  792. * @since 2.0.1
  793. * @access public
  794. * @var string
  795. */
  796. var $request;
  797. /**
  798. * List of posts.
  799. *
  800. * @since 1.5.0
  801. * @access public
  802. * @var array
  803. */
  804. var $posts;
  805. /**
  806. * The amount of posts for the current query.
  807. *
  808. * @since 1.5.0
  809. * @access public
  810. * @var int
  811. */
  812. var $post_count = 0;
  813. /**
  814. * Index of the current item in the loop.
  815. *
  816. * @since 1.5.0
  817. * @access public
  818. * @var int
  819. */
  820. var $current_post = -1;
  821. /**
  822. * Whether the loop has started and the caller is in the loop.
  823. *
  824. * @since 2.0.0
  825. * @access public
  826. * @var bool
  827. */
  828. var $in_the_loop = false;
  829. /**
  830. * The current post.
  831. *
  832. * @since 1.5.0
  833. * @access public
  834. * @var WP_Post
  835. */
  836. var $post;
  837. /**
  838. * The list of comments for current post.
  839. *
  840. * @since 2.2.0
  841. * @access public
  842. * @var array
  843. */
  844. var $comments;
  845. /**
  846. * The amount of comments for the posts.
  847. *
  848. * @since 2.2.0
  849. * @access public
  850. * @var int
  851. */
  852. var $comment_count = 0;
  853. /**
  854. * The index of the comment in the comment loop.
  855. *
  856. * @since 2.2.0
  857. * @access public
  858. * @var int
  859. */
  860. var $current_comment = -1;
  861. /**
  862. * Current comment ID.
  863. *
  864. * @since 2.2.0
  865. * @access public
  866. * @var int
  867. */
  868. var $comment;
  869. /**
  870. * The amount of found posts for the current query.
  871. *
  872. * If limit clause was not used, equals $post_count.
  873. *
  874. * @since 2.1.0
  875. * @access public
  876. * @var int
  877. */
  878. var $found_posts = 0;
  879. /**
  880. * The amount of pages.
  881. *
  882. * @since 2.1.0
  883. * @access public
  884. * @var int
  885. */
  886. var $max_num_pages = 0;
  887. /**
  888. * The amount of comment pages.
  889. *
  890. * @since 2.7.0
  891. * @access public
  892. * @var int
  893. */
  894. var $max_num_comment_pages = 0;
  895. /**
  896. * Set if query is single post.
  897. *
  898. * @since 1.5.0
  899. * @access public
  900. * @var bool
  901. */
  902. var $is_single = false;
  903. /**
  904. * Set if query is preview of blog.
  905. *
  906. * @since 2.0.0
  907. * @access public
  908. * @var bool
  909. */
  910. var $is_preview = false;
  911. /**
  912. * Set if query returns a page.
  913. *
  914. * @since 1.5.0
  915. * @access public
  916. * @var bool
  917. */
  918. var $is_page = false;
  919. /**
  920. * Set if query is an archive list.
  921. *
  922. * @since 1.5.0
  923. * @access public
  924. * @var bool
  925. */
  926. var $is_archive = false;
  927. /**
  928. * Set if query is part of a date.
  929. *
  930. * @since 1.5.0
  931. * @access public
  932. * @var bool
  933. */
  934. var $is_date = false;
  935. /**
  936. * Set if query contains a year.
  937. *
  938. * @since 1.5.0
  939. * @access public
  940. * @var bool
  941. */
  942. var $is_year = false;
  943. /**
  944. * Set if query contains a month.
  945. *
  946. * @since 1.5.0
  947. * @access public
  948. * @var bool
  949. */
  950. var $is_month = false;
  951. /**
  952. * Set if query contains a day.
  953. *
  954. * @since 1.5.0
  955. * @access public
  956. * @var bool
  957. */
  958. var $is_day = false;
  959. /**
  960. * Set if query contains time.
  961. *
  962. * @since 1.5.0
  963. * @access public
  964. * @var bool
  965. */
  966. var $is_time = false;
  967. /**
  968. * Set if query contains an author.
  969. *
  970. * @since 1.5.0
  971. * @access public
  972. * @var bool
  973. */
  974. var $is_author = false;
  975. /**
  976. * Set if query contains category.
  977. *
  978. * @since 1.5.0
  979. * @access public
  980. * @var bool
  981. */
  982. var $is_category = false;
  983. /**
  984. * Set if query contains tag.
  985. *
  986. * @since 2.3.0
  987. * @access public
  988. * @var bool
  989. */
  990. var $is_tag = false;
  991. /**
  992. * Set if query contains taxonomy.
  993. *
  994. * @since 2.5.0
  995. * @access public
  996. * @var bool
  997. */
  998. var $is_tax = false;
  999. /**
  1000. * Set if query was part of a search result.
  1001. *
  1002. * @since 1.5.0
  1003. * @access public
  1004. * @var bool
  1005. */
  1006. var $is_search = false;
  1007. /**
  1008. * Set if query is feed display.
  1009. *
  1010. * @since 1.5.0
  1011. * @access public
  1012. * @var bool
  1013. */
  1014. var $is_feed = false;
  1015. /**
  1016. * Set if query is comment feed display.
  1017. *
  1018. * @since 2.2.0
  1019. * @access public
  1020. * @var bool
  1021. */
  1022. var $is_comment_feed = false;
  1023. /**
  1024. * Set if query is trackback.
  1025. *
  1026. * @since 1.5.0
  1027. * @access public
  1028. * @var bool
  1029. */
  1030. var $is_trackback = false;
  1031. /**
  1032. * Set if query is blog homepage.
  1033. *
  1034. * @since 1.5.0
  1035. * @access public
  1036. * @var bool
  1037. */
  1038. var $is_home = false;
  1039. /**
  1040. * Set if query couldn't found anything.
  1041. *
  1042. * @since 1.5.0
  1043. * @access public
  1044. * @var bool
  1045. */
  1046. var $is_404 = false;
  1047. /**
  1048. * Set if query is within comments popup window.
  1049. *
  1050. * @since 1.5.0
  1051. * @access public
  1052. * @var bool
  1053. */
  1054. var $is_comments_popup = false;
  1055. /**
  1056. * Set if query is paged
  1057. *
  1058. * @since 1.5.0
  1059. * @access public
  1060. * @var bool
  1061. */
  1062. var $is_paged = false;
  1063. /**
  1064. * Set if query is part of administration page.
  1065. *
  1066. * @since 1.5.0
  1067. * @access public
  1068. * @var bool
  1069. */
  1070. var $is_admin = false;
  1071. /**
  1072. * Set if query is an attachment.
  1073. *
  1074. * @since 2.0.0
  1075. * @access public
  1076. * @var bool
  1077. */
  1078. var $is_attachment = false;
  1079. /**
  1080. * Set if is single, is a page, or is an attachment.
  1081. *
  1082. * @since 2.1.0
  1083. * @access public
  1084. * @var bool
  1085. */
  1086. var $is_singular = false;
  1087. /**
  1088. * Set if query is for robots.
  1089. *
  1090. * @since 2.1.0
  1091. * @access public
  1092. * @var bool
  1093. */
  1094. var $is_robots = false;
  1095. /**
  1096. * Set if query contains posts.
  1097. *
  1098. * Basically, the homepage if the option isn't set for the static homepage.
  1099. *
  1100. * @since 2.1.0
  1101. * @access public
  1102. * @var bool
  1103. */
  1104. var $is_posts_page = false;
  1105. /**
  1106. * Set if query is for a post type archive.
  1107. *
  1108. * @since 3.1.0
  1109. * @access public
  1110. * @var bool
  1111. */
  1112. var $is_post_type_archive = false;
  1113. /**
  1114. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1115. * whether we have to re-parse because something has changed
  1116. *
  1117. * @since 3.1.0
  1118. * @access private
  1119. */
  1120. var $query_vars_hash = false;
  1121. /**
  1122. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1123. * via pre_get_posts hooks.
  1124. *
  1125. * @since 3.1.1
  1126. * @access private
  1127. */
  1128. var $query_vars_changed = true;
  1129. /**
  1130. * Set if post thumbnails are cached
  1131. *
  1132. * @since 3.2.0
  1133. * @access public
  1134. * @var bool
  1135. */
  1136. var $thumbnails_cached = false;
  1137. /**
  1138. * Cached list of search stopwords.
  1139. *
  1140. * @since 3.7.0
  1141. * @var array
  1142. */
  1143. private $stopwords;
  1144. /**
  1145. * Resets query flags to false.
  1146. *
  1147. * The query flags are what page info WordPress was able to figure out.
  1148. *
  1149. * @since 2.0.0
  1150. * @access private
  1151. */
  1152. function init_query_flags() {
  1153. $this->is_single = false;
  1154. $this->is_preview = false;
  1155. $this->is_page = false;
  1156. $this->is_archive = false;
  1157. $this->is_date = false;
  1158. $this->is_year = false;
  1159. $this->is_month = false;
  1160. $this->is_day = false;
  1161. $this->is_time = false;
  1162. $this->is_author = false;
  1163. $this->is_category = false;
  1164. $this->is_tag = false;
  1165. $this->is_tax = false;
  1166. $this->is_search = false;
  1167. $this->is_feed = false;
  1168. $this->is_comment_feed = false;
  1169. $this->is_trackback = false;
  1170. $this->is_home = false;
  1171. $this->is_404 = false;
  1172. $this->is_comments_popup = false;
  1173. $this->is_paged = false;
  1174. $this->is_admin = false;
  1175. $this->is_attachment = false;
  1176. $this->is_singular = false;
  1177. $this->is_robots = false;
  1178. $this->is_posts_page = false;
  1179. $this->is_post_type_archive = false;
  1180. }
  1181. /**
  1182. * Initiates object properties and sets default values.
  1183. *
  1184. * @since 1.5.0
  1185. * @access public
  1186. */
  1187. function init() {
  1188. unset($this->posts);
  1189. unset($this->query);
  1190. $this->query_vars = array();
  1191. unset($this->queried_object);
  1192. unset($this->queried_object_id);
  1193. $this->post_count = 0;
  1194. $this->current_post = -1;
  1195. $this->in_the_loop = false;
  1196. unset( $this->request );
  1197. unset( $this->post );
  1198. unset( $this->comments );
  1199. unset( $this->comment );
  1200. $this->comment_count = 0;
  1201. $this->current_comment = -1;
  1202. $this->found_posts = 0;
  1203. $this->max_num_pages = 0;
  1204. $this->max_num_comment_pages = 0;
  1205. $this->init_query_flags();
  1206. }
  1207. /**
  1208. * Reparse the query vars.
  1209. *
  1210. * @since 1.5.0
  1211. * @access public
  1212. */
  1213. function parse_query_vars() {
  1214. $this->parse_query();
  1215. }
  1216. /**
  1217. * Fills in the query variables, which do not exist within the parameter.
  1218. *
  1219. * @since 2.1.0
  1220. * @access public
  1221. *
  1222. * @param array $array Defined query variables.
  1223. * @return array Complete query variables with undefined ones filled in empty.
  1224. */
  1225. function fill_query_vars($array) {
  1226. $keys = array(
  1227. 'error'
  1228. , 'm'
  1229. , 'p'
  1230. , 'post_parent'
  1231. , 'subpost'
  1232. , 'subpost_id'
  1233. , 'attachment'
  1234. , 'attachment_id'
  1235. , 'name'
  1236. , 'static'
  1237. , 'pagename'
  1238. , 'page_id'
  1239. , 'second'
  1240. , 'minute'
  1241. , 'hour'
  1242. , 'day'
  1243. , 'monthnum'
  1244. , 'year'
  1245. , 'w'
  1246. , 'category_name'
  1247. , 'tag'
  1248. , 'cat'
  1249. , 'tag_id'
  1250. , 'author'
  1251. , 'author_name'
  1252. , 'feed'
  1253. , 'tb'
  1254. , 'paged'
  1255. , 'comments_popup'
  1256. , 'meta_key'
  1257. , 'meta_value'
  1258. , 'preview'
  1259. , 's'
  1260. , 'sentence'
  1261. , 'fields'
  1262. , 'menu_order'
  1263. );
  1264. foreach ( $keys as $key ) {
  1265. if ( !isset($array[$key]) )
  1266. $array[$key] = '';
  1267. }
  1268. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1269. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
  1270. 'author__in', 'author__not_in' );
  1271. foreach ( $array_keys as $key ) {
  1272. if ( !isset($array[$key]) )
  1273. $array[$key] = array();
  1274. }
  1275. return $array;
  1276. }
  1277. /**
  1278. * Parse a query string and set query type booleans.
  1279. *
  1280. * @since 1.5.0
  1281. * @access public
  1282. *
  1283. * @param string|array $query Optional query.
  1284. */
  1285. function parse_query( $query = '' ) {
  1286. if ( ! empty( $query ) ) {
  1287. $this->init();
  1288. $this->query = $this->query_vars = wp_parse_args( $query );
  1289. } elseif ( ! isset( $this->query ) ) {
  1290. $this->query = $this->query_vars;
  1291. }
  1292. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1293. $qv = &$this->query_vars;
  1294. $this->query_vars_changed = true;
  1295. if ( ! empty($qv['robots']) )
  1296. $this->is_robots = true;
  1297. $qv['p'] = absint($qv['p']);
  1298. $qv['page_id'] = absint($qv['page_id']);
  1299. $qv['year'] = absint($qv['year']);
  1300. $qv['monthnum'] = absint($qv['monthnum']);
  1301. $qv['day'] = absint($qv['day']);
  1302. $qv['w'] = absint($qv['w']);
  1303. $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
  1304. $qv['paged'] = absint($qv['paged']);
  1305. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1306. $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
  1307. $qv['pagename'] = trim( $qv['pagename'] );
  1308. $qv['name'] = trim( $qv['name'] );
  1309. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1310. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1311. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1312. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  1313. // Fairly insane upper bound for search string lengths.
  1314. if ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 )
  1315. $qv['s'] = '';
  1316. // Compat. Map subpost to attachment.
  1317. if ( '' != $qv['subpost'] )
  1318. $qv['attachment'] = $qv['subpost'];
  1319. if ( '' != $qv['subpost_id'] )
  1320. $qv['attachment_id'] = $qv['subpost_id'];
  1321. $qv['attachment_id'] = absint($qv['attachment_id']);
  1322. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1323. $this->is_single = true;
  1324. $this->is_attachment = true;
  1325. } elseif ( '' != $qv['name'] ) {
  1326. $this->is_single = true;
  1327. } elseif ( $qv['p'] ) {
  1328. $this->is_single = true;
  1329. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1330. // If year, month, day, hour, minute, and second are set, a single
  1331. // post is being queried.
  1332. $this->is_single = true;
  1333. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1334. $this->is_page = true;
  1335. $this->is_single = false;
  1336. } else {
  1337. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1338. if ( !empty($qv['s']) ) {
  1339. $this->is_search = true;
  1340. }
  1341. if ( '' !== $qv['second'] ) {
  1342. $this->is_time = true;
  1343. $this->is_date = true;
  1344. }
  1345. if ( '' !== $qv['minute'] ) {
  1346. $this->is_time = true;
  1347. $this->is_date = true;
  1348. }
  1349. if ( '' !== $qv['hour'] ) {
  1350. $this->is_time = true;
  1351. $this->is_date = true;
  1352. }
  1353. if ( $qv['day'] ) {
  1354. if ( ! $this->is_date ) {
  1355. $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
  1356. if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
  1357. $qv['error'] = '404';
  1358. } else {
  1359. $this->is_day = true;
  1360. $this->is_date = true;
  1361. }
  1362. }
  1363. }
  1364. if ( $qv['monthnum'] ) {
  1365. if ( ! $this->is_date ) {
  1366. if ( 12 < $qv['monthnum'] ) {
  1367. $qv['error'] = '404';
  1368. } else {
  1369. $this->is_month = true;
  1370. $this->is_date = true;
  1371. }
  1372. }
  1373. }
  1374. if ( $qv['year'] ) {
  1375. if ( ! $this->is_date ) {
  1376. $this->is_year = true;
  1377. $this->is_date = true;
  1378. }
  1379. }
  1380. if ( $qv['m'] ) {
  1381. $this->is_date = true;
  1382. if ( strlen($qv['m']) > 9 ) {
  1383. $this->is_time = true;
  1384. } else if ( strlen($qv['m']) > 7 ) {
  1385. $this->is_day = true;
  1386. } else if ( strlen($qv['m']) > 5 ) {
  1387. $this->is_month = true;
  1388. } else {
  1389. $this->is_year = true;
  1390. }
  1391. }
  1392. if ( '' != $qv['w'] ) {
  1393. $this->is_date = true;
  1394. }
  1395. $this->query_vars_hash = false;
  1396. $this->parse_tax_query( $qv );
  1397. foreach ( $this->tax_query->queries as $tax_query ) {
  1398. if ( 'NOT IN' != $tax_query['operator'] ) {
  1399. switch ( $tax_query['taxonomy'] ) {
  1400. case 'category':
  1401. $this->is_category = true;
  1402. break;
  1403. case 'post_tag':
  1404. $this->is_tag = true;
  1405. break;
  1406. default:
  1407. $this->is_tax = true;
  1408. }
  1409. }
  1410. }
  1411. unset( $tax_query );
  1412. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1413. $this->is_author = false;
  1414. } else {
  1415. $this->is_author = true;
  1416. }
  1417. if ( '' != $qv['author_name'] )
  1418. $this->is_author = true;
  1419. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1420. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1421. if ( ! empty( $post_type_obj->has_archive ) )
  1422. $this->is_post_type_archive = true;
  1423. }
  1424. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1425. $this->is_archive = true;
  1426. }
  1427. if ( '' != $qv['feed'] )
  1428. $this->is_feed = true;
  1429. if ( '' != $qv['tb'] )
  1430. $this->is_trackback = true;
  1431. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1432. $this->is_paged = true;
  1433. if ( '' != $qv['comments_popup'] )
  1434. $this->is_comments_popup = true;
  1435. // if we're previewing inside the write screen
  1436. if ( '' != $qv['preview'] )
  1437. $this->is_preview = true;
  1438. if ( is_admin() )
  1439. $this->is_admin = true;
  1440. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1441. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1442. $qv['withcomments'] = 1;
  1443. }
  1444. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1445. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1446. $this->is_comment_feed = true;
  1447. if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup || $this->is_robots ) )
  1448. $this->is_home = true;
  1449. // Correct is_* for page_on_front and page_for_posts
  1450. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1451. $_query = wp_parse_args($this->query);
  1452. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1453. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1454. unset($_query['pagename']);
  1455. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1456. $this->is_page = true;
  1457. $this->is_home = false;
  1458. $qv['page_id'] = get_option('page_on_front');
  1459. // Correct <!--nextpage--> for page_on_front
  1460. if ( !empty($qv['paged']) ) {
  1461. $qv['page'] = $qv['paged'];
  1462. unset($qv['paged']);
  1463. }
  1464. }
  1465. }
  1466. if ( '' != $qv['pagename'] ) {
  1467. $this->queried_object = get_page_by_path($qv['pagename']);
  1468. if ( !empty($this->queried_object) )
  1469. $this->queried_object_id = (int) $this->queried_object->ID;
  1470. else
  1471. unset($this->queried_object);
  1472. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1473. $this->is_page = false;
  1474. $this->is_home = true;
  1475. $this->is_posts_page = true;
  1476. }
  1477. }
  1478. if ( $qv['page_id'] ) {
  1479. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1480. $this->is_page = false;
  1481. $this->is_home = true;
  1482. $this->is_posts_page = true;
  1483. }
  1484. }
  1485. if ( !empty($qv['post_type']) ) {
  1486. if ( is_array($qv['post_type']) )
  1487. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1488. else
  1489. $qv['post_type'] = sanitize_key($qv['post_type']);
  1490. }
  1491. if ( ! empty( $qv['post_status'] ) ) {
  1492. if ( is_array( $qv['post_status'] ) )
  1493. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1494. else
  1495. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1496. }
  1497. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1498. $this->is_comment_feed = false;
  1499. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1500. // Done correcting is_* for page_on_front and page_for_posts
  1501. if ( '404' == $qv['error'] )
  1502. $this->set_404();
  1503. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1504. $this->query_vars_changed = false;
  1505. do_action_ref_array('parse_query', array(&$this));
  1506. }
  1507. /*
  1508. * Parses various taxonomy related query vars.
  1509. *
  1510. * @access protected
  1511. * @since 3.1.0
  1512. *
  1513. * @param array &$q The query variables
  1514. */
  1515. function parse_tax_query( &$q ) {
  1516. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1517. $tax_query = $q['tax_query'];
  1518. } else {
  1519. $tax_query = array();
  1520. }
  1521. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1522. $tax_query[] = array(
  1523. 'taxonomy' => $q['taxonomy'],
  1524. 'terms' => array( $q['term'] ),
  1525. 'field' => 'slug',
  1526. );
  1527. }
  1528. foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
  1529. if ( 'post_tag' == $taxonomy )
  1530. continue; // Handled further down in the $q['tag'] block
  1531. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1532. $tax_query_defaults = array(
  1533. 'taxonomy' => $taxonomy,
  1534. 'field' => 'slug',
  1535. );
  1536. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1537. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1538. }
  1539. $term = $q[$t->query_var];
  1540. if ( strpos($term, '+') !== false ) {
  1541. $terms = preg_split( '/[+]+/', $term );
  1542. foreach ( $terms as $term ) {
  1543. $tax_query[] = array_merge( $tax_query_defaults, array(
  1544. 'terms' => array( $term )
  1545. ) );
  1546. }
  1547. } else {
  1548. $tax_query[] = array_merge( $tax_query_defaults, array(
  1549. 'terms' => preg_split( '/[,]+/', $term )
  1550. ) );
  1551. }
  1552. }
  1553. }
  1554. // Category stuff
  1555. if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
  1556. $q['cat'] = ''.urldecode($q['cat']).'';
  1557. $q['cat'] = addslashes_gpc($q['cat']);
  1558. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  1559. $q['cat'] = '';
  1560. $req_cats = array();
  1561. foreach ( (array) $cat_array as $cat ) {
  1562. $cat = intval($cat);
  1563. $req_cats[] = $cat;
  1564. $in = ($cat > 0);
  1565. $cat = abs($cat);
  1566. if ( $in ) {
  1567. $q['category__in'][] = $cat;
  1568. $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
  1569. } else {
  1570. $q['category__not_in'][] = $cat;
  1571. $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
  1572. }
  1573. }
  1574. $q['cat'] = implode(',', $req_cats);
  1575. }
  1576. if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
  1577. $q['category__and'] = (array) $q['category__and'];
  1578. if ( ! isset( $q['category__in'] ) )
  1579. $q['category__in'] = array();
  1580. $q['category__in'][] = absint( reset( $q['category__and'] ) );
  1581. unset( $q['category__and'] );
  1582. }
  1583. if ( ! empty( $q['category__in'] ) ) {
  1584. $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
  1585. $tax_query[] = array(
  1586. 'taxonomy' => 'category',
  1587. 'terms' => $q['category__in'],
  1588. 'field' => 'term_id',
  1589. 'include_children' => false
  1590. );
  1591. }
  1592. if ( ! empty($q['category__not_in']) ) {
  1593. $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
  1594. $tax_query[] = array(
  1595. 'taxonomy' => 'category',
  1596. 'terms' => $q['category__not_in'],
  1597. 'operator' => 'NOT IN',
  1598. 'include_children' => false
  1599. );
  1600. }
  1601. if ( ! empty($q['category__and']) ) {
  1602. $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
  1603. $tax_query[] = array(
  1604. 'taxonomy' => 'category',
  1605. 'terms' => $q['category__and'],
  1606. 'field' => 'term_id',
  1607. 'operator' => 'AND',
  1608. 'include_children' => false
  1609. );
  1610. }
  1611. // Tag stuff
  1612. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1613. if ( strpos($q['tag'], ',') !== false ) {
  1614. $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
  1615. foreach ( (array) $tags as $tag ) {
  1616. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1617. $q['tag_slug__in'][] = $tag;
  1618. }
  1619. } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
  1620. $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
  1621. foreach ( (array) $tags as $tag ) {
  1622. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1623. $q['tag_slug__and'][] = $tag;
  1624. }
  1625. } else {
  1626. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1627. $q['tag_slug__in'][] = $q['tag'];
  1628. }
  1629. }
  1630. if ( !empty($q['tag_id']) ) {
  1631. $q['tag_id'] = absint( $q['tag_id'] );
  1632. $tax_query[] = array(
  1633. 'taxonomy' => 'post_tag',
  1634. 'terms' => $q['tag_id']
  1635. );
  1636. }
  1637. if ( !empty($q['tag__in']) ) {
  1638. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1639. $tax_query[] = array(
  1640. 'taxonomy' => 'post_tag',
  1641. 'terms' => $q['tag__in']
  1642. );
  1643. }
  1644. if ( !empty($q['tag__not_in']) ) {
  1645. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1646. $tax_query[] = array(
  1647. 'taxonomy' => 'post_tag',
  1648. 'terms' => $q['tag__not_in'],
  1649. 'operator' => 'NOT IN'
  1650. );
  1651. }
  1652. if ( !empty($q['tag__and']) ) {
  1653. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1654. $tax_query[] = array(
  1655. 'taxonomy' => 'post_tag',
  1656. 'terms' => $q['tag__and'],
  1657. 'operator' => 'AND'
  1658. );
  1659. }
  1660. if ( !empty($q['tag_slug__in']) ) {
  1661. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
  1662. $tax_query[] = array(
  1663. 'taxonomy' => 'post_tag',
  1664. 'terms' => $q['tag_slug__in'],
  1665. 'field' => 'slug'
  1666. );
  1667. }
  1668. if ( !empty($q['tag_slug__and']) ) {
  1669. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
  1670. $tax_query[] = array(
  1671. 'taxonomy' => 'post_tag',
  1672. 'terms' => $q['tag_slug__and'],
  1673. 'field' => 'slug',
  1674. 'operator' => 'AND'
  1675. );
  1676. }
  1677. $this->tax_query = new WP_Tax_Query( $tax_query );
  1678. do_action( 'parse_tax_query', $this );
  1679. }
  1680. /**
  1681. * Generate SQL for the WHERE clause based on passed search terms.
  1682. *
  1683. * @since 3.7.0
  1684. *
  1685. * @global type $wpdb
  1686. * @param array $q Query variables.
  1687. */
  1688. protected function parse_search( &$q ) {
  1689. global $wpdb;
  1690. $search = '';
  1691. // added slashes screw with quote grouping when done early, so done later
  1692. $q['s'] = stripslashes( $q['s'] );
  1693. if ( empty( $_GET['s'] ) && $this->is_main_query() )
  1694. $q['s'] = urldecode( $q['s'] );
  1695. // there are no line breaks in <input /> fields
  1696. $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
  1697. $q['search_terms_count'] = 1;
  1698. if ( ! empty( $q['sentence'] ) ) {
  1699. $q['search_terms'] = array( $q['s'] );
  1700. } else {
  1701. if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
  1702. $q['search_terms_count'] = count( $matches[0] );
  1703. $q['search_terms'] = $this->parse_search_terms( $matches[0] );
  1704. // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
  1705. if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
  1706. $q['search_terms'] = array( $q['s'] );
  1707. } else {
  1708. $q['search_terms'] = array( $q['s'] );
  1709. }
  1710. }
  1711. $n = ! empty( $q['exact'] ) ? '' : '%';
  1712. $searchand = '';
  1713. $q['search_orderby_title'] = array();
  1714. foreach ( $q['search_terms'] as $term ) {
  1715. $term = like_escape( esc_sql( $term ) );
  1716. if ( $n )
  1717. $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
  1718. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  1719. $searchand = ' AND ';
  1720. }
  1721. if ( ! empty( $search ) ) {
  1722. $search = " AND ({$search}) ";
  1723. if ( ! is_user_logged_in() )
  1724. $search .= " AND ($wpdb->posts.post_password = '') ";
  1725. }
  1726. return $search;
  1727. }
  1728. /**
  1729. * Check if the terms are suitable for searching.
  1730. *
  1731. * Uses an array of stopwords (terms) that are excluded from the separate
  1732. * term matching when searching for posts. The list of English stopwords is
  1733. * the approximate search engines list, and is translatable.
  1734. *
  1735. * @since 3.7.0
  1736. *
  1737. * @param array Terms to check.
  1738. * @return array Terms that are not stopwords.
  1739. */
  1740. protected function parse_search_terms( $terms ) {
  1741. $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
  1742. $checked = array();
  1743. $stopwords = $this->get_search_stopwords();
  1744. foreach ( $terms as $term ) {
  1745. // keep before/after spaces when term is for exact match
  1746. if ( preg_match( '/^".+"$/', $term ) )
  1747. $term = trim( $term, "\"'" );
  1748. else
  1749. $term = trim( $term, "\"' " );
  1750. // Avoid single A-Z.
  1751. if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
  1752. continue;
  1753. if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
  1754. continue;
  1755. $checked[] = $term;
  1756. }
  1757. return $checked;
  1758. }
  1759. /**
  1760. * Retrieve stopwords used when parsing search terms.
  1761. *
  1762. * @since 3.7.0
  1763. *
  1764. * @return array Stopwords.
  1765. */
  1766. protected function get_search_stopwords() {
  1767. if ( isset( $this->stopwords ) )
  1768. return $this->stopwords;
  1769. /* translators: This is a comma-separated list of very common words that should be excluded from a search,
  1770. * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
  1771. * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
  1772. */
  1773. $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
  1774. 'Comma-separated list of search stopwords in your language' ) );
  1775. foreach( $words as $word ) {
  1776. $word = trim( $word, "\r\n\t " );
  1777. if ( $word )
  1778. $stopwords[] = $word;
  1779. }
  1780. /**
  1781. * Filter stopwords used when parsing search terms.
  1782. *
  1783. * @since 3.7.0
  1784. *
  1785. * @param array $stopwords Stopwords.
  1786. */
  1787. $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
  1788. return $this->stopwords;
  1789. }
  1790. /**
  1791. * Generate SQL for the ORDER BY condition based on passed search terms.
  1792. *
  1793. * @global wpdb $wpdb
  1794. * @param array $q Query variables.
  1795. * @return string ORDER BY clause.
  1796. */
  1797. protected function parse_search_order( &$q ) {
  1798. global $wpdb;
  1799. $search_orderby = '';
  1800. if ( $q['search_terms_count'] > 1 ) {
  1801. $num_terms = count( $q['search_orderby_title'] );
  1802. $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
  1803. $search_orderby = '(CASE ';
  1804. // sentence match in 'post_title'
  1805. $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
  1806. // sanity limit, sort as sentence when more than 6 terms
  1807. // (few searches are longer than 6 terms and most titles are not)
  1808. if ( $num_terms < 7 ) {
  1809. // all words in title
  1810. $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
  1811. // any word in title, not needed when $num_terms == 1
  1812. if ( $num_terms > 1 )
  1813. $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
  1814. }
  1815. // sentence match in 'post_content'
  1816. $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
  1817. $search_orderby .= 'ELSE 5 END)';
  1818. } else {
  1819. // single word or sentence search
  1820. $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
  1821. }
  1822. return $search_orderby;
  1823. }
  1824. /**
  1825. * Sets the 404 property and saves whether query is feed.
  1826. *
  1827. * @since 2.0.0
  1828. * @access public
  1829. */
  1830. function set_404() {
  1831. $is_feed = $this->is_feed;
  1832. $this->init_query_flags();
  1833. $this->is_404 = true;
  1834. $this->is_feed = $is_feed;
  1835. }
  1836. /**
  1837. * Retrieve query variable.
  1838. *
  1839. * @since 1.5.0
  1840. * @access public
  1841. *
  1842. * @param string $query_var Query variable key.
  1843. * @return mixed
  1844. */
  1845. function get($query_var) {
  1846. if ( isset($this->query_vars[$query_var]) )
  1847. return $this->query_vars[$query_var];
  1848. return '';
  1849. }
  1850. /**
  1851. * Set query variable.
  1852. *
  1853. * @since 1.5.0
  1854. * @access public
  1855. *
  1856. * @param string $query_var Query variable key.
  1857. * @param mixed $value Query variable value.
  1858. */
  1859. function set($query_var, $value) {
  1860. $this->query_vars[$query_var] = $value;
  1861. }
  1862. /**
  1863. * Retrieve the posts based on query variables.
  1864. *
  1865. * There are a few filters and actions that can be used to modify the post
  1866. * database query.
  1867. *
  1868. * @since 1.5.0
  1869. * @access public
  1870. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1871. *
  1872. * @return array List of posts.
  1873. */
  1874. function get_posts() {
  1875. global $wpdb;
  1876. $this->parse_query();
  1877. do_action_ref_array('pre_get_posts', array(&$this));
  1878. // Shorthand.
  1879. $q = &$this->query_vars;
  1880. // Fill again in case pre_get_posts unset some vars.
  1881. $q = $this->fill_query_vars($q);
  1882. // Parse meta query
  1883. $this->meta_query = new WP_Meta_Query();
  1884. $this->meta_query->parse_query_vars( $q );
  1885. // Set a flag if a pre_get_posts hook changed the query vars.
  1886. $has

Large files files are truncated, but you can click here to view the full file