PageRenderTime 74ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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. $hash = md5( serialize( $this->query_vars ) );
  1887. if ( $hash != $this->query_vars_hash ) {
  1888. $this->query_vars_changed = true;
  1889. $this->query_vars_hash = $hash;
  1890. }
  1891. unset($hash);
  1892. // First let's clear some variables
  1893. $distinct = '';
  1894. $whichauthor = '';
  1895. $whichmimetype = '';
  1896. $where = '';
  1897. $limits = '';
  1898. $join = '';
  1899. $search = '';
  1900. $groupby = '';
  1901. $fields = '';
  1902. $post_status_join = false;
  1903. $page = 1;
  1904. if ( isset( $q['caller_get_posts'] ) ) {
  1905. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1906. if ( !isset( $q['ignore_sticky_posts'] ) )
  1907. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1908. }
  1909. if ( !isset( $q['ignore_sticky_posts'] ) )
  1910. $q['ignore_sticky_posts'] = false;
  1911. if ( !isset($q['suppress_filters']) )
  1912. $q['suppress_filters'] = false;
  1913. if ( !isset($q['cache_results']) ) {
  1914. if ( wp_using_ext_object_cache() )
  1915. $q['cache_results'] = false;
  1916. else
  1917. $q['cache_results'] = true;
  1918. }
  1919. if ( !isset($q['update_post_term_cache']) )
  1920. $q['update_post_term_cache'] = true;
  1921. if ( !isset($q['update_post_meta_cache']) )
  1922. $q['update_post_meta_cache'] = true;
  1923. if ( !isset($q['post_type']) ) {
  1924. if ( $this->is_search )
  1925. $q['post_type'] = 'any';
  1926. else
  1927. $q['post_type'] = '';
  1928. }
  1929. $post_type = $q['post_type'];
  1930. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  1931. $q['posts_per_page'] = get_option('posts_per_page');
  1932. if ( isset($q['showposts']) && $q['showposts'] ) {
  1933. $q['showposts'] = (int) $q['showposts'];
  1934. $q['posts_per_page'] = $q['showposts'];
  1935. }
  1936. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1937. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1938. if ( !isset($q['nopaging']) ) {
  1939. if ( $q['posts_per_page'] == -1 ) {
  1940. $q['nopaging'] = true;
  1941. } else {
  1942. $q['nopaging'] = false;
  1943. }
  1944. }
  1945. if ( $this->is_feed ) {
  1946. $q['posts_per_page'] = get_option('posts_per_rss');
  1947. $q['nopaging'] = false;
  1948. }
  1949. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1950. if ( $q['posts_per_page'] < -1 )
  1951. $q['posts_per_page'] = abs($q['posts_per_page']);
  1952. else if ( $q['posts_per_page'] == 0 )
  1953. $q['posts_per_page'] = 1;
  1954. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1955. $q['comments_per_page'] = get_option('comments_per_page');
  1956. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1957. $this->is_page = true;
  1958. $this->is_home = false;
  1959. $q['page_id'] = get_option('page_on_front');
  1960. }
  1961. if ( isset($q['page']) ) {
  1962. $q['page'] = trim($q['page'], '/');
  1963. $q['page'] = absint($q['page']);
  1964. }
  1965. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1966. if ( isset($q['no_found_rows']) )
  1967. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1968. else
  1969. $q['no_found_rows'] = false;
  1970. switch ( $q['fields'] ) {
  1971. case 'ids':
  1972. $fields = "$wpdb->posts.ID";
  1973. break;
  1974. case 'id=>parent':
  1975. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  1976. break;
  1977. default:
  1978. $fields = "$wpdb->posts.*";
  1979. }
  1980. if ( '' !== $q['menu_order'] )
  1981. $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
  1982. // The "m" parameter is meant for months but accepts datetimes of varying specificity
  1983. if ( $q['m'] ) {
  1984. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  1985. if ( strlen($q['m']) > 5 )
  1986. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  1987. if ( strlen($q['m']) > 7 )
  1988. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  1989. if ( strlen($q['m']) > 9 )
  1990. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  1991. if ( strlen($q['m']) > 11 )
  1992. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  1993. if ( strlen($q['m']) > 13 )
  1994. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  1995. }
  1996. // Handle the other individual date parameters
  1997. $date_parameters = array();
  1998. if ( '' !== $q['hour'] )
  1999. $date_parameters['hour'] = $q['hour'];
  2000. if ( '' !== $q['minute'] )
  2001. $date_parameters['minute'] = $q['minute'];
  2002. if ( '' !== $q['second'] )
  2003. $date_parameters['second'] = $q['second'];
  2004. if ( $q['year'] )
  2005. $date_parameters['year'] = $q['year'];
  2006. if ( $q['monthnum'] )
  2007. $date_parameters['monthnum'] = $q['monthnum'];
  2008. if ( $q['w'] )
  2009. $date_parameters['week'] = $q['w'];
  2010. if ( $q['day'] )
  2011. $date_parameters['day'] = $q['day'];
  2012. if ( $date_parameters ) {
  2013. $date_query = new WP_Date_Query( array( $date_parameters ) );
  2014. $where .= $date_query->get_sql();
  2015. }
  2016. unset( $date_parameters, $date_query );
  2017. // Handle complex date queries
  2018. if ( ! empty( $q['date_query'] ) ) {
  2019. $this->date_query = new WP_Date_Query( $q['date_query'] );
  2020. $where .= $this->date_query->get_sql();
  2021. }
  2022. // If we've got a post_type AND it's not "any" post_type.
  2023. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  2024. foreach ( (array)$q['post_type'] as $_post_type ) {
  2025. $ptype_obj = get_post_type_object($_post_type);
  2026. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  2027. continue;
  2028. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  2029. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  2030. $q['name'] = $q[ $ptype_obj->query_var ];
  2031. } else {
  2032. // Hierarchical post_types will operate through the
  2033. $q['pagename'] = $q[ $ptype_obj->query_var ];
  2034. $q['name'] = '';
  2035. }
  2036. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  2037. break;
  2038. } //end foreach
  2039. unset($ptype_obj);
  2040. }
  2041. if ( '' != $q['name'] ) {
  2042. $q['name'] = sanitize_title_for_query( $q['name'] );
  2043. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  2044. } elseif ( '' != $q['pagename'] ) {
  2045. if ( isset($this->queried_object_id) ) {
  2046. $reqpage = $this->queried_object_id;
  2047. } else {
  2048. if ( 'page' != $q['post_type'] ) {
  2049. foreach ( (array)$q['post_type'] as $_post_type ) {
  2050. $ptype_obj = get_post_type_object($_post_type);
  2051. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  2052. continue;
  2053. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  2054. if ( $reqpage )
  2055. break;
  2056. }
  2057. unset($ptype_obj);
  2058. } else {
  2059. $reqpage = get_page_by_path($q['pagename']);
  2060. }
  2061. if ( !empty($reqpage) )
  2062. $reqpage = $reqpage->ID;
  2063. else
  2064. $reqpage = 0;
  2065. }
  2066. $page_for_posts = get_option('page_for_posts');
  2067. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  2068. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  2069. $q['name'] = $q['pagename'];
  2070. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  2071. $reqpage_obj = get_post( $reqpage );
  2072. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  2073. $this->is_attachment = true;
  2074. $post_type = $q['post_type'] = 'attachment';
  2075. $this->is_page = true;
  2076. $q['attachment_id'] = $reqpage;
  2077. }
  2078. }
  2079. } elseif ( '' != $q['attachment'] ) {
  2080. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  2081. $q['name'] = $q['attachment'];
  2082. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  2083. }
  2084. if ( intval($q['comments_popup']) )
  2085. $q['p'] = absint($q['comments_popup']);
  2086. // If an attachment is requested by number, let it supersede any post number.
  2087. if ( $q['attachment_id'] )
  2088. $q['p'] = absint($q['attachment_id']);
  2089. // If a post number is specified, load that post
  2090. if ( $q['p'] ) {
  2091. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  2092. } elseif ( $q['post__in'] ) {
  2093. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  2094. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  2095. } elseif ( $q['post__not_in'] ) {
  2096. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  2097. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  2098. }
  2099. if ( is_numeric( $q['post_parent'] ) ) {
  2100. $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  2101. } elseif ( $q['post_parent__in'] ) {
  2102. $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
  2103. $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
  2104. } elseif ( $q['post_parent__not_in'] ) {
  2105. $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
  2106. $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
  2107. }
  2108. if ( $q['page_id'] ) {
  2109. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  2110. $q['p'] = $q['page_id'];
  2111. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  2112. }
  2113. }
  2114. // If a search pattern is specified, load the posts that match.
  2115. if ( ! empty( $q['s'] ) )
  2116. $search = $this->parse_search( $q );
  2117. /**
  2118. * Filter the search SQL that is used in the WHERE clause of WP_Query.
  2119. *
  2120. * @since 3.0.0
  2121. *
  2122. * @param string $search Search SQL for WHERE clause.
  2123. * @param WP_Query $this The current WP_Query object.
  2124. */
  2125. $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
  2126. // Taxonomies
  2127. if ( !$this->is_singular ) {
  2128. $this->parse_tax_query( $q );
  2129. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
  2130. $join .= $clauses['join'];
  2131. $where .= $clauses['where'];
  2132. }
  2133. if ( $this->is_tax ) {
  2134. if ( empty($post_type) ) {
  2135. // Do a fully inclusive search for currently registered post types of queried taxonomies
  2136. $post_type = array();
  2137. $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
  2138. foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
  2139. $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
  2140. if ( array_intersect( $taxonomies, $object_taxonomies ) )
  2141. $post_type[] = $pt;
  2142. }
  2143. if ( ! $post_type )
  2144. $post_type = 'any';
  2145. elseif ( count( $post_type ) == 1 )
  2146. $post_type = $post_type[0];
  2147. $post_status_join = true;
  2148. } elseif ( in_array('attachment', (array) $post_type) ) {
  2149. $post_status_join = true;
  2150. }
  2151. }
  2152. // Back-compat
  2153. if ( !empty($this->tax_query->queries) ) {
  2154. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  2155. if ( !empty( $tax_query_in_and ) ) {
  2156. if ( !isset( $q['taxonomy'] ) ) {
  2157. foreach ( $tax_query_in_and as $a_tax_query ) {
  2158. if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
  2159. $q['taxonomy'] = $a_tax_query['taxonomy'];
  2160. if ( 'slug' == $a_tax_query['field'] )
  2161. $q['term'] = $a_tax_query['terms'][0];
  2162. else
  2163. $q['term_id'] = $a_tax_query['terms'][0];
  2164. break;
  2165. }
  2166. }
  2167. }
  2168. $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
  2169. if ( ! empty( $cat_query ) ) {
  2170. $cat_query = reset( $cat_query );
  2171. if ( ! empty( $cat_query['terms'][0] ) ) {
  2172. $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
  2173. if ( $the_cat ) {
  2174. $this->set( 'cat', $the_cat->term_id );
  2175. $this->set( 'category_name', $the_cat->slug );
  2176. }
  2177. unset( $the_cat );
  2178. }
  2179. }
  2180. unset( $cat_query );
  2181. $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
  2182. if ( ! empty( $tag_query ) ) {
  2183. $tag_query = reset( $tag_query );
  2184. if ( ! empty( $tag_query['terms'][0] ) ) {
  2185. $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
  2186. if ( $the_tag )
  2187. $this->set( 'tag_id', $the_tag->term_id );
  2188. unset( $the_tag );
  2189. }
  2190. }
  2191. unset( $tag_query );
  2192. }
  2193. }
  2194. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
  2195. $groupby = "{$wpdb->posts}.ID";
  2196. }
  2197. // Author/user stuff
  2198. if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
  2199. $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
  2200. $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
  2201. foreach ( $authors as $author ) {
  2202. $key = $author > 0 ? 'author__in' : 'author__not_in';
  2203. $q[$key][] = abs( $author );
  2204. }
  2205. $q['author'] = implode( ',', $authors );
  2206. }
  2207. if ( ! empty( $q['author__not_in'] ) ) {
  2208. $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
  2209. $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
  2210. } elseif ( ! empty( $q['author__in'] ) ) {
  2211. $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
  2212. $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
  2213. }
  2214. // Author stuff for nice URLs
  2215. if ( '' != $q['author_name'] ) {
  2216. if ( strpos($q['author_name'], '/') !== false ) {
  2217. $q['author_name'] = explode('/', $q['author_name']);
  2218. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  2219. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  2220. } else {
  2221. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
  2222. }
  2223. }
  2224. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  2225. $q['author'] = get_user_by('slug', $q['author_name']);
  2226. if ( $q['author'] )
  2227. $q['author'] = $q['author']->ID;
  2228. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  2229. }
  2230. // MIME-Type stuff for attachment browsing
  2231. if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
  2232. $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
  2233. $where .= $search . $whichauthor . $whichmimetype;
  2234. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  2235. $q['order'] = 'DESC';
  2236. // Order by
  2237. if ( empty($q['orderby']) ) {
  2238. $orderby = "$wpdb->posts.post_date " . $q['order'];
  2239. } elseif ( 'none' == $q['orderby'] ) {
  2240. $orderby = '';
  2241. } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
  2242. $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
  2243. } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
  2244. $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
  2245. } else {
  2246. // Used to filter values
  2247. $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2248. if ( !empty($q['meta_key']) ) {
  2249. $allowed_keys[] = $q['meta_key'];
  2250. $allowed_keys[] = 'meta_value';
  2251. $allowed_keys[] = 'meta_value_num';
  2252. }
  2253. $q['orderby'] = urldecode($q['orderby']);
  2254. $q['orderby'] = addslashes_gpc($q['orderby']);
  2255. $orderby_array = array();
  2256. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  2257. // Only allow certain values for safety
  2258. if ( ! in_array($orderby, $allowed_keys) )
  2259. continue;
  2260. switch ( $orderby ) {
  2261. case 'menu_order':
  2262. $orderby = "$wpdb->posts.menu_order";
  2263. break;
  2264. case 'ID':
  2265. $orderby = "$wpdb->posts.ID";
  2266. break;
  2267. case 'rand':
  2268. $orderby = 'RAND()';
  2269. break;
  2270. case $q['meta_key']:
  2271. case 'meta_value':
  2272. if ( isset( $q['meta_type'] ) ) {
  2273. $meta_type = $this->meta_query->get_cast_for_type( $q['meta_type'] );
  2274. $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
  2275. } else {
  2276. $orderby = "$wpdb->postmeta.meta_value";
  2277. }
  2278. break;
  2279. case 'meta_value_num':
  2280. $orderby = "$wpdb->postmeta.meta_value+0";
  2281. break;
  2282. case 'comment_count':
  2283. $orderby = "$wpdb->posts.comment_count";
  2284. break;
  2285. default:
  2286. $orderby = "$wpdb->posts.post_" . $orderby;
  2287. }
  2288. $orderby_array[] = $orderby;
  2289. }
  2290. $orderby = implode( ',', $orderby_array );
  2291. if ( empty( $orderby ) )
  2292. $orderby = "$wpdb->posts.post_date ".$q['order'];
  2293. else
  2294. $orderby .= " {$q['order']}";
  2295. }
  2296. // Order search results by relevance only when another "orderby" is not specified in the query.
  2297. if ( ! empty( $q['s'] ) ) {
  2298. $search_orderby = '';
  2299. if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
  2300. $search_orderby = $this->parse_search_order( $q );
  2301. /**
  2302. * Filter the ORDER BY used when ordering search results.
  2303. *
  2304. * @since 3.7.0
  2305. *
  2306. * @param string $search_orderby The ORDER BY clause.
  2307. * @param WP_Query $this The current WP_Query instance.
  2308. */
  2309. $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
  2310. if ( $search_orderby )
  2311. $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
  2312. }
  2313. if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
  2314. $post_type_cap = 'multiple_post_type';
  2315. } else {
  2316. if ( is_array( $post_type ) )
  2317. $post_type = reset( $post_type );
  2318. $post_type_object = get_post_type_object( $post_type );
  2319. if ( empty( $post_type_object ) )
  2320. $post_type_cap = $post_type;
  2321. }
  2322. if ( 'any' == $post_type ) {
  2323. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  2324. if ( empty( $in_search_post_types ) )
  2325. $where .= ' AND 1=0 ';
  2326. else
  2327. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
  2328. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2329. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2330. } elseif ( ! empty( $post_type ) ) {
  2331. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  2332. $post_type_object = get_post_type_object ( $post_type );
  2333. } elseif ( $this->is_attachment ) {
  2334. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  2335. $post_type_object = get_post_type_object ( 'attachment' );
  2336. } elseif ( $this->is_page ) {
  2337. $where .= " AND $wpdb->posts.post_type = 'page'";
  2338. $post_type_object = get_post_type_object ( 'page' );
  2339. } else {
  2340. $where .= " AND $wpdb->posts.post_type = 'post'";
  2341. $post_type_object = get_post_type_object ( 'post' );
  2342. }
  2343. $edit_cap = 'edit_post';
  2344. $read_cap = 'read_post';
  2345. if ( ! empty( $post_type_object ) ) {
  2346. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  2347. $read_private_cap = $post_type_object->cap->read_private_posts;
  2348. } else {
  2349. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  2350. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  2351. }
  2352. $user_id = get_current_user_id();
  2353. if ( ! empty( $q['post_status'] ) ) {
  2354. $statuswheres = array();
  2355. $q_status = $q['post_status'];
  2356. if ( ! is_array( $q_status ) )
  2357. $q_status = explode(',', $q_status);
  2358. $r_status = array();
  2359. $p_status = array();
  2360. $e_status = array();
  2361. if ( in_array('any', $q_status) ) {
  2362. foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  2363. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  2364. } else {
  2365. foreach ( get_post_stati() as $status ) {
  2366. if ( in_array( $status, $q_status ) ) {
  2367. if ( 'private' == $status )
  2368. $p_status[] = "$wpdb->posts.post_status = '$status'";
  2369. else
  2370. $r_status[] = "$wpdb->posts.post_status = '$status'";
  2371. }
  2372. }
  2373. }
  2374. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2375. $r_status = array_merge($r_status, $p_status);
  2376. unset($p_status);
  2377. }
  2378. if ( !empty($e_status) ) {
  2379. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2380. }
  2381. if ( !empty($r_status) ) {
  2382. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2383. $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
  2384. else
  2385. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2386. }
  2387. if ( !empty($p_status) ) {
  2388. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2389. $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
  2390. else
  2391. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2392. }
  2393. if ( $post_status_join ) {
  2394. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2395. foreach ( $statuswheres as $index => $statuswhere )
  2396. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  2397. }
  2398. foreach ( $statuswheres as $statuswhere )
  2399. $where .= " AND $statuswhere";
  2400. } elseif ( !$this->is_singular ) {
  2401. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  2402. // Add public states.
  2403. $public_states = get_post_stati( array('public' => true) );
  2404. foreach ( (array) $public_states as $state ) {
  2405. if ( 'publish' == $state ) // Publish is hard-coded above.
  2406. continue;
  2407. $where .= " OR $wpdb->posts.post_status = '$state'";
  2408. }
  2409. if ( $this->is_admin ) {
  2410. // Add protected states that should show in the admin all list.
  2411. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2412. foreach ( (array) $admin_all_states as $state )
  2413. $where .= " OR $wpdb->posts.post_status = '$state'";
  2414. }
  2415. if ( is_user_logged_in() ) {
  2416. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2417. $private_states = get_post_stati( array('private' => true) );
  2418. foreach ( (array) $private_states as $state )
  2419. $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_id AND $wpdb->posts.post_status = '$state'";
  2420. }
  2421. $where .= ')';
  2422. }
  2423. if ( !empty( $this->meta_query->queries ) ) {
  2424. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  2425. $join .= $clauses['join'];
  2426. $where .= $clauses['where'];
  2427. }
  2428. // Apply filters on where and join prior to paging so that any
  2429. // manipulations to them are reflected in the paging by day queries.
  2430. if ( !$q['suppress_filters'] ) {
  2431. $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
  2432. $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
  2433. }
  2434. // Paging
  2435. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2436. $page = absint($q['paged']);
  2437. if ( !$page )
  2438. $page = 1;
  2439. if ( empty($q['offset']) ) {
  2440. $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2441. } else { // we're ignoring $page and using 'offset'
  2442. $q['offset'] = absint($q['offset']);
  2443. $pgstrt = $q['offset'] . ', ';
  2444. }
  2445. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2446. }
  2447. // Comments feeds
  2448. if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2449. if ( $this->is_archive || $this->is_search ) {
  2450. $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  2451. $cwhere = "WHERE comment_approved = '1' $where";
  2452. $cgroupby = "$wpdb->comments.comment_id";
  2453. } else { // Other non singular e.g. front
  2454. $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  2455. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2456. $cgroupby = '';
  2457. }
  2458. if ( !$q['suppress_filters'] ) {
  2459. $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
  2460. $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
  2461. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
  2462. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2463. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2464. }
  2465. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2466. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2467. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2468. $this->comment_count = count($this->comments);
  2469. $post_ids = array();
  2470. foreach ( $this->comments as $comment )
  2471. $post_ids[] = (int) $comment->comment_post_ID;
  2472. $post_ids = join(',', $post_ids);
  2473. $join = '';
  2474. if ( $post_ids )
  2475. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2476. else
  2477. $where = "AND 0";
  2478. }
  2479. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2480. // Apply post-paging filters on where and join. Only plugins that
  2481. // manipulate paging queries should use these hooks.
  2482. if ( !$q['suppress_filters'] ) {
  2483. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2484. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2485. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2486. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2487. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2488. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2489. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2490. // Filter all clauses at once, for convenience
  2491. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2492. foreach ( $pieces as $piece )
  2493. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2494. }
  2495. // Announce current selection parameters. For use by caching plugins.
  2496. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2497. // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
  2498. if ( !$q['suppress_filters'] ) {
  2499. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2500. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2501. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2502. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2503. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2504. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2505. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2506. // Filter all clauses at once, for convenience
  2507. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
  2508. foreach ( $pieces as $piece )
  2509. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2510. }
  2511. if ( ! empty($groupby) )
  2512. $groupby = 'GROUP BY ' . $groupby;
  2513. if ( !empty( $orderby ) )
  2514. $orderby = 'ORDER BY ' . $orderby;
  2515. $found_rows = '';
  2516. if ( !$q['no_found_rows'] && !empty($limits) )
  2517. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2518. $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2519. if ( !$q['suppress_filters'] ) {
  2520. $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
  2521. }
  2522. if ( 'ids' == $q['fields'] ) {
  2523. $this->posts = $wpdb->get_col( $this->request );
  2524. $this->post_count = count( $this->posts );
  2525. $this->set_found_posts( $q, $limits );
  2526. return $this->posts;
  2527. }
  2528. if ( 'id=>parent' == $q['fields'] ) {
  2529. $this->posts = $wpdb->get_results( $this->request );
  2530. $this->post_count = count( $this->posts );
  2531. $this->set_found_posts( $q, $limits );
  2532. $r = array();
  2533. foreach ( $this->posts as $post )
  2534. $r[ $post->ID ] = $post->post_parent;
  2535. return $r;
  2536. }
  2537. $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
  2538. $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
  2539. if ( $split_the_query ) {
  2540. // First get the IDs and then fill in the objects
  2541. $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2542. $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
  2543. $ids = $wpdb->get_col( $this->request );
  2544. if ( $ids ) {
  2545. $this->posts = $ids;
  2546. $this->set_found_posts( $q, $limits );
  2547. _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
  2548. } else {
  2549. $this->posts = array();
  2550. }
  2551. } else {
  2552. $this->posts = $wpdb->get_results( $this->request );
  2553. $this->set_found_posts( $q, $limits );
  2554. }
  2555. // Convert to WP_Post objects
  2556. if ( $this->posts )
  2557. $this->posts = array_map( 'get_post', $this->posts );
  2558. // Raw results filter. Prior to status checks.
  2559. if ( !$q['suppress_filters'] )
  2560. $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  2561. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2562. $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
  2563. $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2564. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
  2565. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2566. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2567. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2568. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2569. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2570. $this->comments = $wpdb->get_results($comments_request);
  2571. $this->comment_count = count($this->comments);
  2572. }
  2573. // Check post status to determine if post should be displayed.
  2574. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2575. $status = get_post_status($this->posts[0]);
  2576. $post_status_obj = get_post_status_object($status);
  2577. //$type = get_post_type($this->posts[0]);
  2578. if ( !$post_status_obj->public ) {
  2579. if ( ! is_user_logged_in() ) {
  2580. // User must be logged in to view unpublished posts.
  2581. $this->posts = array();
  2582. } else {
  2583. if ( $post_status_obj->protected ) {
  2584. // User must have edit permissions on the draft to preview.
  2585. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2586. $this->posts = array();
  2587. } else {
  2588. $this->is_preview = true;
  2589. if ( 'future' != $status )
  2590. $this->posts[0]->post_date = current_time('mysql');
  2591. }
  2592. } elseif ( $post_status_obj->private ) {
  2593. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2594. $this->posts = array();
  2595. } else {
  2596. $this->posts = array();
  2597. }
  2598. }
  2599. }
  2600. if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
  2601. $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
  2602. }
  2603. // Put sticky posts at the top of the posts array
  2604. $sticky_posts = get_option('sticky_posts');
  2605. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  2606. $num_posts = count($this->posts);
  2607. $sticky_offset = 0;
  2608. // Loop over posts and relocate stickies to the front.
  2609. for ( $i = 0; $i < $num_posts; $i++ ) {
  2610. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2611. $sticky_post = $this->posts[$i];
  2612. // Remove sticky from current position
  2613. array_splice($this->posts, $i, 1);
  2614. // Move to front, after other stickies
  2615. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2616. // Increment the sticky offset. The next sticky will be placed at this offset.
  2617. $sticky_offset++;
  2618. // Remove post from sticky posts array
  2619. $offset = array_search($sticky_post->ID, $sticky_posts);
  2620. unset( $sticky_posts[$offset] );
  2621. }
  2622. }
  2623. // If any posts have been excluded specifically, Ignore those that are sticky.
  2624. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  2625. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2626. // Fetch sticky posts that weren't in the query results
  2627. if ( !empty($sticky_posts) ) {
  2628. $stickies = get_posts( array(
  2629. 'post__in' => $sticky_posts,
  2630. 'post_type' => $post_type,
  2631. 'post_status' => 'publish',
  2632. 'nopaging' => true
  2633. ) );
  2634. foreach ( $stickies as $sticky_post ) {
  2635. array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
  2636. $sticky_offset++;
  2637. }
  2638. }
  2639. }
  2640. if ( !$q['suppress_filters'] )
  2641. $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
  2642. // Ensure that any posts added/modified via one of the filters above are
  2643. // of the type WP_Post and are filtered.
  2644. if ( $this->posts ) {
  2645. $this->post_count = count( $this->posts );
  2646. $this->posts = array_map( 'get_post', $this->posts );
  2647. if ( $q['cache_results'] )
  2648. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2649. $this->post = reset( $this->posts );
  2650. } else {
  2651. $this->post_count = 0;
  2652. $this->posts = array();
  2653. }
  2654. return $this->posts;
  2655. }
  2656. /**
  2657. * Set up the amount of found posts and the number of pages (if limit clause was used)
  2658. * for the current query.
  2659. *
  2660. * @since 3.5.0
  2661. * @access private
  2662. */
  2663. function set_found_posts( $q, $limits ) {
  2664. global $wpdb;
  2665. // Bail if posts is an empty array. Continue if posts is an empty string,
  2666. // null, or false to accommodate caching plugins that fill posts later.
  2667. if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
  2668. return;
  2669. if ( ! empty( $limits ) )
  2670. $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
  2671. else
  2672. $this->found_posts = count( $this->posts );
  2673. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  2674. if ( ! empty( $limits ) )
  2675. $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
  2676. }
  2677. /**
  2678. * Set up the next post and iterate current post index.
  2679. *
  2680. * @since 1.5.0
  2681. * @access public
  2682. *
  2683. * @return WP_Post Next post.
  2684. */
  2685. function next_post() {
  2686. $this->current_post++;
  2687. $this->post = $this->posts[$this->current_post];
  2688. return $this->post;
  2689. }
  2690. /**
  2691. * Sets up the current post.
  2692. *
  2693. * Retrieves the next post, sets up the post, sets the 'in the loop'
  2694. * property to true.
  2695. *
  2696. * @since 1.5.0
  2697. * @access public
  2698. * @uses $post
  2699. * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
  2700. */
  2701. function the_post() {
  2702. global $post;
  2703. $this->in_the_loop = true;
  2704. if ( $this->current_post == -1 ) // loop has just started
  2705. do_action_ref_array('loop_start', array(&$this));
  2706. $post = $this->next_post();
  2707. setup_postdata($post);
  2708. }
  2709. /**
  2710. * Whether there are more posts available in the loop.
  2711. *
  2712. * Calls action 'loop_end', when the loop is complete.
  2713. *
  2714. * @since 1.5.0
  2715. * @access public
  2716. * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  2717. *
  2718. * @return bool True if posts are available, false if end of loop.
  2719. */
  2720. function have_posts() {
  2721. if ( $this->current_post + 1 < $this->post_count ) {
  2722. return true;
  2723. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  2724. do_action_ref_array('loop_end', array(&$this));
  2725. // Do some cleaning up after the loop
  2726. $this->rewind_posts();
  2727. }
  2728. $this->in_the_loop = false;
  2729. return false;
  2730. }
  2731. /**
  2732. * Rewind the posts and reset post index.
  2733. *
  2734. * @since 1.5.0
  2735. * @access public
  2736. */
  2737. function rewind_posts() {
  2738. $this->current_post = -1;
  2739. if ( $this->post_count > 0 ) {
  2740. $this->post = $this->posts[0];
  2741. }
  2742. }
  2743. /**
  2744. * Iterate current comment index and return comment object.
  2745. *
  2746. * @since 2.2.0
  2747. * @access public
  2748. *
  2749. * @return object Comment object.
  2750. */
  2751. function next_comment() {
  2752. $this->current_comment++;
  2753. $this->comment = $this->comments[$this->current_comment];
  2754. return $this->comment;
  2755. }
  2756. /**
  2757. * Sets up the current comment.
  2758. *
  2759. * @since 2.2.0
  2760. * @access public
  2761. * @global object $comment Current comment.
  2762. * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
  2763. */
  2764. function the_comment() {
  2765. global $comment;
  2766. $comment = $this->next_comment();
  2767. if ( $this->current_comment == 0 ) {
  2768. do_action('comment_loop_start');
  2769. }
  2770. }
  2771. /**
  2772. * Whether there are more comments available.
  2773. *
  2774. * Automatically rewinds comments when finished.
  2775. *
  2776. * @since 2.2.0
  2777. * @access public
  2778. *
  2779. * @return bool True, if more comments. False, if no more posts.
  2780. */
  2781. function have_comments() {
  2782. if ( $this->current_comment + 1 < $this->comment_count ) {
  2783. return true;
  2784. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  2785. $this->rewind_comments();
  2786. }
  2787. return false;
  2788. }
  2789. /**
  2790. * Rewind the comments, resets the comment index and comment to first.
  2791. *
  2792. * @since 2.2.0
  2793. * @access public
  2794. */
  2795. function rewind_comments() {
  2796. $this->current_comment = -1;
  2797. if ( $this->comment_count > 0 ) {
  2798. $this->comment = $this->comments[0];
  2799. }
  2800. }
  2801. /**
  2802. * Sets up the WordPress query by parsing query string.
  2803. *
  2804. * @since 1.5.0
  2805. * @access public
  2806. *
  2807. * @param string $query URL query string.
  2808. * @return array List of posts.
  2809. */
  2810. function query( $query ) {
  2811. $this->init();
  2812. $this->query = $this->query_vars = wp_parse_args( $query );
  2813. return $this->get_posts();
  2814. }
  2815. /**
  2816. * Retrieve queried object.
  2817. *
  2818. * If queried object is not set, then the queried object will be set from
  2819. * the category, tag, taxonomy, posts page, single post, page, or author
  2820. * query variable. After it is set up, it will be returned.
  2821. *
  2822. * @since 1.5.0
  2823. * @access public
  2824. *
  2825. * @return object
  2826. */
  2827. function get_queried_object() {
  2828. if ( isset($this->queried_object) )
  2829. return $this->queried_object;
  2830. $this->queried_object = null;
  2831. $this->queried_object_id = 0;
  2832. if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  2833. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  2834. $query = reset( $tax_query_in_and );
  2835. if ( 'term_id' == $query['field'] )
  2836. $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
  2837. elseif ( $query['terms'] )
  2838. $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
  2839. if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
  2840. $this->queried_object = $term;
  2841. $this->queried_object_id = (int) $term->term_id;
  2842. if ( $this->is_category )
  2843. _make_cat_compat( $this->queried_object );
  2844. }
  2845. } elseif ( $this->is_post_type_archive ) {
  2846. $post_type = $this->get( 'post_type' );
  2847. if ( is_array( $post_type ) )
  2848. $post_type = reset( $post_type );
  2849. $this->queried_object = get_post_type_object( $post_type );
  2850. } elseif ( $this->is_posts_page ) {
  2851. $page_for_posts = get_option('page_for_posts');
  2852. $this->queried_object = get_post( $page_for_posts );
  2853. $this->queried_object_id = (int) $this->queried_object->ID;
  2854. } elseif ( $this->is_singular && !is_null($this->post) ) {
  2855. $this->queried_object = $this->post;
  2856. $this->queried_object_id = (int) $this->post->ID;
  2857. } elseif ( $this->is_author ) {
  2858. $this->queried_object_id = (int) $this->get('author');
  2859. $this->queried_object = get_userdata( $this->queried_object_id );
  2860. }
  2861. return $this->queried_object;
  2862. }
  2863. /**
  2864. * Retrieve ID of the current queried object.
  2865. *
  2866. * @since 1.5.0
  2867. * @access public
  2868. *
  2869. * @return int
  2870. */
  2871. function get_queried_object_id() {
  2872. $this->get_queried_object();
  2873. if ( isset($this->queried_object_id) ) {
  2874. return $this->queried_object_id;
  2875. }
  2876. return 0;
  2877. }
  2878. /**
  2879. * Constructor.
  2880. *
  2881. * Sets up the WordPress query, if parameter is not empty.
  2882. *
  2883. * @since 1.5.0
  2884. * @access public
  2885. *
  2886. * @param string $query URL query string.
  2887. * @return WP_Query
  2888. */
  2889. function __construct($query = '') {
  2890. if ( ! empty($query) ) {
  2891. $this->query($query);
  2892. }
  2893. }
  2894. /**
  2895. * Is the query for an existing archive page?
  2896. *
  2897. * Month, Year, Category, Author, Post Type archive...
  2898. *
  2899. * @since 3.1.0
  2900. *
  2901. * @return bool
  2902. */
  2903. function is_archive() {
  2904. return (bool) $this->is_archive;
  2905. }
  2906. /**
  2907. * Is the query for an existing post type archive page?
  2908. *
  2909. * @since 3.1.0
  2910. *
  2911. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  2912. * @return bool
  2913. */
  2914. function is_post_type_archive( $post_types = '' ) {
  2915. if ( empty( $post_types ) || ! $this->is_post_type_archive )
  2916. return (bool) $this->is_post_type_archive;
  2917. $post_type = $this->get( 'post_type' );
  2918. if ( is_array( $post_type ) )
  2919. $post_type = reset( $post_type );
  2920. $post_type_object = get_post_type_object( $post_type );
  2921. return in_array( $post_type_object->name, (array) $post_types );
  2922. }
  2923. /**
  2924. * Is the query for an existing attachment page?
  2925. *
  2926. * @since 3.1.0
  2927. *
  2928. * @return bool
  2929. */
  2930. function is_attachment() {
  2931. return (bool) $this->is_attachment;
  2932. }
  2933. /**
  2934. * Is the query for an existing author archive page?
  2935. *
  2936. * If the $author parameter is specified, this function will additionally
  2937. * check if the query is for one of the authors specified.
  2938. *
  2939. * @since 3.1.0
  2940. *
  2941. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  2942. * @return bool
  2943. */
  2944. function is_author( $author = '' ) {
  2945. if ( !$this->is_author )
  2946. return false;
  2947. if ( empty($author) )
  2948. return true;
  2949. $author_obj = $this->get_queried_object();
  2950. $author = (array) $author;
  2951. if ( in_array( $author_obj->ID, $author ) )
  2952. return true;
  2953. elseif ( in_array( $author_obj->nickname, $author ) )
  2954. return true;
  2955. elseif ( in_array( $author_obj->user_nicename, $author ) )
  2956. return true;
  2957. return false;
  2958. }
  2959. /**
  2960. * Is the query for an existing category archive page?
  2961. *
  2962. * If the $category parameter is specified, this function will additionally
  2963. * check if the query is for one of the categories specified.
  2964. *
  2965. * @since 3.1.0
  2966. *
  2967. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  2968. * @return bool
  2969. */
  2970. function is_category( $category = '' ) {
  2971. if ( !$this->is_category )
  2972. return false;
  2973. if ( empty($category) )
  2974. return true;
  2975. $cat_obj = $this->get_queried_object();
  2976. $category = (array) $category;
  2977. if ( in_array( $cat_obj->term_id, $category ) )
  2978. return true;
  2979. elseif ( in_array( $cat_obj->name, $category ) )
  2980. return true;
  2981. elseif ( in_array( $cat_obj->slug, $category ) )
  2982. return true;
  2983. return false;
  2984. }
  2985. /**
  2986. * Is the query for an existing tag archive page?
  2987. *
  2988. * If the $tag parameter is specified, this function will additionally
  2989. * check if the query is for one of the tags specified.
  2990. *
  2991. * @since 3.1.0
  2992. *
  2993. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  2994. * @return bool
  2995. */
  2996. function is_tag( $tag = '' ) {
  2997. if ( ! $this->is_tag )
  2998. return false;
  2999. if ( empty( $tag ) )
  3000. return true;
  3001. $tag_obj = $this->get_queried_object();
  3002. $tag = (array) $tag;
  3003. if ( in_array( $tag_obj->term_id, $tag ) )
  3004. return true;
  3005. elseif ( in_array( $tag_obj->name, $tag ) )
  3006. return true;
  3007. elseif ( in_array( $tag_obj->slug, $tag ) )
  3008. return true;
  3009. return false;
  3010. }
  3011. /**
  3012. * Is the query for an existing taxonomy archive page?
  3013. *
  3014. * If the $taxonomy parameter is specified, this function will additionally
  3015. * check if the query is for that specific $taxonomy.
  3016. *
  3017. * If the $term parameter is specified in addition to the $taxonomy parameter,
  3018. * this function will additionally check if the query is for one of the terms
  3019. * specified.
  3020. *
  3021. * @since 3.1.0
  3022. *
  3023. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  3024. * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  3025. * @return bool
  3026. */
  3027. function is_tax( $taxonomy = '', $term = '' ) {
  3028. global $wp_taxonomies;
  3029. if ( !$this->is_tax )
  3030. return false;
  3031. if ( empty( $taxonomy ) )
  3032. return true;
  3033. $queried_object = $this->get_queried_object();
  3034. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
  3035. $term_array = (array) $term;
  3036. // Check that the taxonomy matches.
  3037. if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
  3038. return false;
  3039. // Only a Taxonomy provided.
  3040. if ( empty( $term ) )
  3041. return true;
  3042. return isset( $queried_object->term_id ) &&
  3043. count( array_intersect(
  3044. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
  3045. $term_array
  3046. ) );
  3047. }
  3048. /**
  3049. * Whether the current URL is within the comments popup window.
  3050. *
  3051. * @since 3.1.0
  3052. *
  3053. * @return bool
  3054. */
  3055. function is_comments_popup() {
  3056. return (bool) $this->is_comments_popup;
  3057. }
  3058. /**
  3059. * Is the query for an existing date archive?
  3060. *
  3061. * @since 3.1.0
  3062. *
  3063. * @return bool
  3064. */
  3065. function is_date() {
  3066. return (bool) $this->is_date;
  3067. }
  3068. /**
  3069. * Is the query for an existing day archive?
  3070. *
  3071. * @since 3.1.0
  3072. *
  3073. * @return bool
  3074. */
  3075. function is_day() {
  3076. return (bool) $this->is_day;
  3077. }
  3078. /**
  3079. * Is the query for a feed?
  3080. *
  3081. * @since 3.1.0
  3082. *
  3083. * @param string|array $feeds Optional feed types to check.
  3084. * @return bool
  3085. */
  3086. function is_feed( $feeds = '' ) {
  3087. if ( empty( $feeds ) || ! $this->is_feed )
  3088. return (bool) $this->is_feed;
  3089. $qv = $this->get( 'feed' );
  3090. if ( 'feed' == $qv )
  3091. $qv = get_default_feed();
  3092. return in_array( $qv, (array) $feeds );
  3093. }
  3094. /**
  3095. * Is the query for a comments feed?
  3096. *
  3097. * @since 3.1.0
  3098. *
  3099. * @return bool
  3100. */
  3101. function is_comment_feed() {
  3102. return (bool) $this->is_comment_feed;
  3103. }
  3104. /**
  3105. * Is the query for the front page of the site?
  3106. *
  3107. * This is for what is displayed at your site's main URL.
  3108. *
  3109. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  3110. *
  3111. * If you set a static page for the front page of your site, this function will return
  3112. * true when viewing that page.
  3113. *
  3114. * Otherwise the same as @see WP_Query::is_home()
  3115. *
  3116. * @since 3.1.0
  3117. * @uses is_home()
  3118. * @uses get_option()
  3119. *
  3120. * @return bool True, if front of site.
  3121. */
  3122. function is_front_page() {
  3123. // most likely case
  3124. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
  3125. return true;
  3126. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
  3127. return true;
  3128. else
  3129. return false;
  3130. }
  3131. /**
  3132. * Is the query for the blog homepage?
  3133. *
  3134. * This is the page which shows the time based blog content of your site.
  3135. *
  3136. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  3137. *
  3138. * If you set a static page for the front page of your site, this function will return
  3139. * true only on the page you set as the "Posts page".
  3140. *
  3141. * @see WP_Query::is_front_page()
  3142. *
  3143. * @since 3.1.0
  3144. *
  3145. * @return bool True if blog view homepage.
  3146. */
  3147. function is_home() {
  3148. return (bool) $this->is_home;
  3149. }
  3150. /**
  3151. * Is the query for an existing month archive?
  3152. *
  3153. * @since 3.1.0
  3154. *
  3155. * @return bool
  3156. */
  3157. function is_month() {
  3158. return (bool) $this->is_month;
  3159. }
  3160. /**
  3161. * Is the query for an existing single page?
  3162. *
  3163. * If the $page parameter is specified, this function will additionally
  3164. * check if the query is for one of the pages specified.
  3165. *
  3166. * @see WP_Query::is_single()
  3167. * @see WP_Query::is_singular()
  3168. *
  3169. * @since 3.1.0
  3170. *
  3171. * @param mixed $page Page ID, title, slug, or array of such.
  3172. * @return bool
  3173. */
  3174. function is_page( $page = '' ) {
  3175. if ( !$this->is_page )
  3176. return false;
  3177. if ( empty( $page ) )
  3178. return true;
  3179. $page_obj = $this->get_queried_object();
  3180. $page = (array) $page;
  3181. if ( in_array( $page_obj->ID, $page ) )
  3182. return true;
  3183. elseif ( in_array( $page_obj->post_title, $page ) )
  3184. return true;
  3185. else if ( in_array( $page_obj->post_name, $page ) )
  3186. return true;
  3187. return false;
  3188. }
  3189. /**
  3190. * Is the query for paged result and not for the first page?
  3191. *
  3192. * @since 3.1.0
  3193. *
  3194. * @return bool
  3195. */
  3196. function is_paged() {
  3197. return (bool) $this->is_paged;
  3198. }
  3199. /**
  3200. * Is the query for a post or page preview?
  3201. *
  3202. * @since 3.1.0
  3203. *
  3204. * @return bool
  3205. */
  3206. function is_preview() {
  3207. return (bool) $this->is_preview;
  3208. }
  3209. /**
  3210. * Is the query for the robots file?
  3211. *
  3212. * @since 3.1.0
  3213. *
  3214. * @return bool
  3215. */
  3216. function is_robots() {
  3217. return (bool) $this->is_robots;
  3218. }
  3219. /**
  3220. * Is the query for a search?
  3221. *
  3222. * @since 3.1.0
  3223. *
  3224. * @return bool
  3225. */
  3226. function is_search() {
  3227. return (bool) $this->is_search;
  3228. }
  3229. /**
  3230. * Is the query for an existing single post?
  3231. *
  3232. * Works for any post type, except attachments and pages
  3233. *
  3234. * If the $post parameter is specified, this function will additionally
  3235. * check if the query is for one of the Posts specified.
  3236. *
  3237. * @see WP_Query::is_page()
  3238. * @see WP_Query::is_singular()
  3239. *
  3240. * @since 3.1.0
  3241. *
  3242. * @param mixed $post Post ID, title, slug, or array of such.
  3243. * @return bool
  3244. */
  3245. function is_single( $post = '' ) {
  3246. if ( !$this->is_single )
  3247. return false;
  3248. if ( empty($post) )
  3249. return true;
  3250. $post_obj = $this->get_queried_object();
  3251. $post = (array) $post;
  3252. if ( in_array( $post_obj->ID, $post ) )
  3253. return true;
  3254. elseif ( in_array( $post_obj->post_title, $post ) )
  3255. return true;
  3256. elseif ( in_array( $post_obj->post_name, $post ) )
  3257. return true;
  3258. return false;
  3259. }
  3260. /**
  3261. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  3262. *
  3263. * If the $post_types parameter is specified, this function will additionally
  3264. * check if the query is for one of the Posts Types specified.
  3265. *
  3266. * @see WP_Query::is_page()
  3267. * @see WP_Query::is_single()
  3268. *
  3269. * @since 3.1.0
  3270. *
  3271. * @param mixed $post_types Optional. Post Type or array of Post Types
  3272. * @return bool
  3273. */
  3274. function is_singular( $post_types = '' ) {
  3275. if ( empty( $post_types ) || !$this->is_singular )
  3276. return (bool) $this->is_singular;
  3277. $post_obj = $this->get_queried_object();
  3278. return in_array( $post_obj->post_type, (array) $post_types );
  3279. }
  3280. /**
  3281. * Is the query for a specific time?
  3282. *
  3283. * @since 3.1.0
  3284. *
  3285. * @return bool
  3286. */
  3287. function is_time() {
  3288. return (bool) $this->is_time;
  3289. }
  3290. /**
  3291. * Is the query for a trackback endpoint call?
  3292. *
  3293. * @since 3.1.0
  3294. *
  3295. * @return bool
  3296. */
  3297. function is_trackback() {
  3298. return (bool) $this->is_trackback;
  3299. }
  3300. /**
  3301. * Is the query for an existing year archive?
  3302. *
  3303. * @since 3.1.0
  3304. *
  3305. * @return bool
  3306. */
  3307. function is_year() {
  3308. return (bool) $this->is_year;
  3309. }
  3310. /**
  3311. * Is the query a 404 (returns no results)?
  3312. *
  3313. * @since 3.1.0
  3314. *
  3315. * @return bool
  3316. */
  3317. function is_404() {
  3318. return (bool) $this->is_404;
  3319. }
  3320. /**
  3321. * Is the query the main query?
  3322. *
  3323. * @since 3.3.0
  3324. *
  3325. * @return bool
  3326. */
  3327. function is_main_query() {
  3328. global $wp_the_query;
  3329. return $wp_the_query === $this;
  3330. }
  3331. /**
  3332. * After looping through a nested query, this function
  3333. * restores the $post global to the current post in this query.
  3334. *
  3335. * @since 3.7.0
  3336. *
  3337. * @return bool
  3338. */
  3339. function reset_postdata() {
  3340. if ( ! empty( $this->post ) ) {
  3341. $GLOBALS['post'] = $this->post;
  3342. setup_postdata( $this->post );
  3343. }
  3344. }
  3345. }
  3346. /**
  3347. * Redirect old slugs to the correct permalink.
  3348. *
  3349. * Attempts to find the current slug from the past slugs.
  3350. *
  3351. * @since 2.1.0
  3352. * @uses $wp_query
  3353. * @uses $wpdb
  3354. *
  3355. * @return null If no link is found, null is returned.
  3356. */
  3357. function wp_old_slug_redirect() {
  3358. global $wp_query;
  3359. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  3360. global $wpdb;
  3361. // Guess the current post_type based on the query vars.
  3362. if ( get_query_var('post_type') )
  3363. $post_type = get_query_var('post_type');
  3364. elseif ( !empty($wp_query->query_vars['pagename']) )
  3365. $post_type = 'page';
  3366. else
  3367. $post_type = 'post';
  3368. if ( is_array( $post_type ) ) {
  3369. if ( count( $post_type ) > 1 )
  3370. return;
  3371. $post_type = array_shift( $post_type );
  3372. }
  3373. // Do not attempt redirect for hierarchical post types
  3374. if ( is_post_type_hierarchical( $post_type ) )
  3375. return;
  3376. $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']);
  3377. // if year, monthnum, or day have been specified, make our query more precise
  3378. // just in case there are multiple identical _wp_old_slug values
  3379. if ( '' != $wp_query->query_vars['year'] )
  3380. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
  3381. if ( '' != $wp_query->query_vars['monthnum'] )
  3382. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
  3383. if ( '' != $wp_query->query_vars['day'] )
  3384. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
  3385. $id = (int) $wpdb->get_var($query);
  3386. if ( ! $id )
  3387. return;
  3388. $link = get_permalink($id);
  3389. if ( !$link )
  3390. return;
  3391. wp_redirect( $link, 301 ); // Permanent redirect
  3392. exit;
  3393. endif;
  3394. }
  3395. /**
  3396. * Set up global post data.
  3397. *
  3398. * @since 1.5.0
  3399. *
  3400. * @param object $post Post data.
  3401. * @uses do_action_ref_array() Calls 'the_post'
  3402. * @return bool True when finished.
  3403. */
  3404. function setup_postdata( $post ) {
  3405. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  3406. $id = (int) $post->ID;
  3407. $authordata = get_userdata($post->post_author);
  3408. $currentday = mysql2date('d.m.y', $post->post_date, false);
  3409. $currentmonth = mysql2date('m', $post->post_date, false);
  3410. $numpages = 1;
  3411. $multipage = 0;
  3412. $page = get_query_var('page');
  3413. if ( ! $page )
  3414. $page = 1;
  3415. if ( is_single() || is_page() || is_feed() )
  3416. $more = 1;
  3417. $content = $post->post_content;
  3418. if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
  3419. if ( $page > 1 )
  3420. $more = 1;
  3421. $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
  3422. $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
  3423. $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
  3424. // Ignore nextpage at the beginning of the content.
  3425. if ( 0 === strpos( $content, '<!--nextpage-->' ) )
  3426. $content = substr( $content, 15 );
  3427. $pages = explode('<!--nextpage-->', $content);
  3428. $numpages = count($pages);
  3429. if ( $numpages > 1 )
  3430. $multipage = 1;
  3431. } else {
  3432. $pages = array( $post->post_content );
  3433. }
  3434. do_action_ref_array('the_post', array(&$post));
  3435. return true;
  3436. }