PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wordpress-3.4.2/wp-includes/query.php

https://bitbucket.org/emallove/bedforddavis.com
PHP | 4394 lines | 2163 code | 510 blank | 1721 comment | 585 complexity | 7d0da7490b2fb08cfe334a400a02db77 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, BSD-3-Clause, GPL-3.0, AGPL-1.0, LGPL-2.1

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

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