PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/query.php

https://github.com/dipakdotyadav/WordPress
PHP | 3667 lines | 1846 code | 464 blank | 1357 comment | 539 complexity | 7bc731a981b1d537f3b1a0230b0327e5 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * WordPress Query API
  4. *
  5. * The query API attempts to get which part of WordPress to the user is on. It
  6. * also provides functionality to 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. if ( !empty($wp_query->post) ) {
  110. $GLOBALS['post'] = $wp_query->post;
  111. setup_postdata($wp_query->post);
  112. }
  113. }
  114. /*
  115. * Query type checks.
  116. */
  117. /**
  118. * Is the query for an existing archive page?
  119. *
  120. * Month, Year, Category, Author, Post Type archive...
  121. *
  122. * @see WP_Query::is_archive()
  123. * @since 1.5.0
  124. * @uses $wp_query
  125. *
  126. * @return bool
  127. */
  128. function is_archive() {
  129. global $wp_query;
  130. if ( ! isset( $wp_query ) ) {
  131. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  132. return false;
  133. }
  134. return $wp_query->is_archive();
  135. }
  136. /**
  137. * Is the query for an existing post type archive page?
  138. *
  139. * @see WP_Query::is_post_type_archive()
  140. * @since 3.1.0
  141. * @uses $wp_query
  142. *
  143. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  144. * @return bool
  145. */
  146. function is_post_type_archive( $post_types = '' ) {
  147. global $wp_query;
  148. if ( ! isset( $wp_query ) ) {
  149. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  150. return false;
  151. }
  152. return $wp_query->is_post_type_archive( $post_types );
  153. }
  154. /**
  155. * Is the query for an existing attachment page?
  156. *
  157. * @see WP_Query::is_attachment()
  158. * @since 2.0.0
  159. * @uses $wp_query
  160. *
  161. * @return bool
  162. */
  163. function is_attachment() {
  164. global $wp_query;
  165. if ( ! isset( $wp_query ) ) {
  166. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  167. return false;
  168. }
  169. return $wp_query->is_attachment();
  170. }
  171. /**
  172. * Is the query for an existing author archive page?
  173. *
  174. * If the $author parameter is specified, this function will additionally
  175. * check if the query is for one of the authors specified.
  176. *
  177. * @see WP_Query::is_author()
  178. * @since 1.5.0
  179. * @uses $wp_query
  180. *
  181. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  182. * @return bool
  183. */
  184. function is_author( $author = '' ) {
  185. global $wp_query;
  186. if ( ! isset( $wp_query ) ) {
  187. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  188. return false;
  189. }
  190. return $wp_query->is_author( $author );
  191. }
  192. /**
  193. * Is the query for an existing category archive page?
  194. *
  195. * If the $category parameter is specified, this function will additionally
  196. * check if the query is for one of the categories specified.
  197. *
  198. * @see WP_Query::is_category()
  199. * @since 1.5.0
  200. * @uses $wp_query
  201. *
  202. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  203. * @return bool
  204. */
  205. function is_category( $category = '' ) {
  206. global $wp_query;
  207. if ( ! isset( $wp_query ) ) {
  208. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  209. return false;
  210. }
  211. return $wp_query->is_category( $category );
  212. }
  213. /**
  214. * Is the query for an existing tag archive page?
  215. *
  216. * If the $tag parameter is specified, this function will additionally
  217. * check if the query is for one of the tags specified.
  218. *
  219. * @see WP_Query::is_tag()
  220. * @since 2.3.0
  221. * @uses $wp_query
  222. *
  223. * @param mixed $slug Optional. Tag slug or array of slugs.
  224. * @return bool
  225. */
  226. function is_tag( $slug = '' ) {
  227. global $wp_query;
  228. if ( ! isset( $wp_query ) ) {
  229. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  230. return false;
  231. }
  232. return $wp_query->is_tag( $slug );
  233. }
  234. /**
  235. * Is the query for an existing taxonomy archive page?
  236. *
  237. * If the $taxonomy parameter is specified, this function will additionally
  238. * check if the query is for that specific $taxonomy.
  239. *
  240. * If the $term parameter is specified in addition to the $taxonomy parameter,
  241. * this function will additionally check if the query is for one of the terms
  242. * specified.
  243. *
  244. * @see WP_Query::is_tax()
  245. * @since 2.5.0
  246. * @uses $wp_query
  247. *
  248. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  249. * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  250. * @return bool
  251. */
  252. function is_tax( $taxonomy = '', $term = '' ) {
  253. global $wp_query;
  254. if ( ! isset( $wp_query ) ) {
  255. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  256. return false;
  257. }
  258. return $wp_query->is_tax( $taxonomy, $term );
  259. }
  260. /**
  261. * Whether the current URL is within the comments popup window.
  262. *
  263. * @see WP_Query::is_comments_popup()
  264. * @since 1.5.0
  265. * @uses $wp_query
  266. *
  267. * @return bool
  268. */
  269. function is_comments_popup() {
  270. global $wp_query;
  271. if ( ! isset( $wp_query ) ) {
  272. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  273. return false;
  274. }
  275. return $wp_query->is_comments_popup();
  276. }
  277. /**
  278. * Is the query for an existing date archive?
  279. *
  280. * @see WP_Query::is_date()
  281. * @since 1.5.0
  282. * @uses $wp_query
  283. *
  284. * @return bool
  285. */
  286. function is_date() {
  287. global $wp_query;
  288. if ( ! isset( $wp_query ) ) {
  289. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  290. return false;
  291. }
  292. return $wp_query->is_date();
  293. }
  294. /**
  295. * Is the query for an existing day archive?
  296. *
  297. * @see WP_Query::is_day()
  298. * @since 1.5.0
  299. * @uses $wp_query
  300. *
  301. * @return bool
  302. */
  303. function is_day() {
  304. global $wp_query;
  305. if ( ! isset( $wp_query ) ) {
  306. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  307. return false;
  308. }
  309. return $wp_query->is_day();
  310. }
  311. /**
  312. * Is the query for a feed?
  313. *
  314. * @see WP_Query::is_feed()
  315. * @since 1.5.0
  316. * @uses $wp_query
  317. *
  318. * @param string|array $feeds Optional feed types to check.
  319. * @return bool
  320. */
  321. function is_feed( $feeds = '' ) {
  322. global $wp_query;
  323. if ( ! isset( $wp_query ) ) {
  324. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  325. return false;
  326. }
  327. return $wp_query->is_feed( $feeds );
  328. }
  329. /**
  330. * Is the query for a comments feed?
  331. *
  332. * @see WP_Query::is_comments_feed()
  333. * @since 3.0.0
  334. * @uses $wp_query
  335. *
  336. * @return bool
  337. */
  338. function is_comment_feed() {
  339. global $wp_query;
  340. if ( ! isset( $wp_query ) ) {
  341. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  342. return false;
  343. }
  344. return $wp_query->is_comment_feed();
  345. }
  346. /**
  347. * Is the query for the front page of the site?
  348. *
  349. * This is for what is displayed at your site's main URL.
  350. *
  351. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  352. *
  353. * If you set a static page for the front page of your site, this function will return
  354. * true when viewing that page.
  355. *
  356. * Otherwise the same as @see is_home()
  357. *
  358. * @see WP_Query::is_front_page()
  359. * @since 2.5.0
  360. * @uses is_home()
  361. * @uses get_option()
  362. *
  363. * @return bool True, if front of site.
  364. */
  365. function is_front_page() {
  366. global $wp_query;
  367. if ( ! isset( $wp_query ) ) {
  368. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  369. return false;
  370. }
  371. return $wp_query->is_front_page();
  372. }
  373. /**
  374. * Is the query for the blog homepage?
  375. *
  376. * This is the page which shows the time based blog content of your site.
  377. *
  378. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  379. *
  380. * If you set a static page for the front page of your site, this function will return
  381. * true only on the page you set as the "Posts page".
  382. *
  383. * @see is_front_page()
  384. *
  385. * @see WP_Query::is_home()
  386. * @since 1.5.0
  387. * @uses $wp_query
  388. *
  389. * @return bool True if blog view homepage.
  390. */
  391. function is_home() {
  392. global $wp_query;
  393. if ( ! isset( $wp_query ) ) {
  394. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  395. return false;
  396. }
  397. return $wp_query->is_home();
  398. }
  399. /**
  400. * Is the query for an existing month archive?
  401. *
  402. * @see WP_Query::is_month()
  403. * @since 1.5.0
  404. * @uses $wp_query
  405. *
  406. * @return bool
  407. */
  408. function is_month() {
  409. global $wp_query;
  410. if ( ! isset( $wp_query ) ) {
  411. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  412. return false;
  413. }
  414. return $wp_query->is_month();
  415. }
  416. /**
  417. * Is the query for an existing single page?
  418. *
  419. * If the $page parameter is specified, this function will additionally
  420. * check if the query is for one of the pages specified.
  421. *
  422. * @see is_single()
  423. * @see is_singular()
  424. *
  425. * @see WP_Query::is_page()
  426. * @since 1.5.0
  427. * @uses $wp_query
  428. *
  429. * @param mixed $page Page ID, title, slug, or array of such.
  430. * @return bool
  431. */
  432. function is_page( $page = '' ) {
  433. global $wp_query;
  434. if ( ! isset( $wp_query ) ) {
  435. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  436. return false;
  437. }
  438. return $wp_query->is_page( $page );
  439. }
  440. /**
  441. * Is the query for paged result and not for the first page?
  442. *
  443. * @see WP_Query::is_paged()
  444. * @since 1.5.0
  445. * @uses $wp_query
  446. *
  447. * @return bool
  448. */
  449. function is_paged() {
  450. global $wp_query;
  451. if ( ! isset( $wp_query ) ) {
  452. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  453. return false;
  454. }
  455. return $wp_query->is_paged();
  456. }
  457. /**
  458. * Is the query for a post or page preview?
  459. *
  460. * @see WP_Query::is_preview()
  461. * @since 2.0.0
  462. * @uses $wp_query
  463. *
  464. * @return bool
  465. */
  466. function is_preview() {
  467. global $wp_query;
  468. if ( ! isset( $wp_query ) ) {
  469. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  470. return false;
  471. }
  472. return $wp_query->is_preview();
  473. }
  474. /**
  475. * Is the query for the robots file?
  476. *
  477. * @see WP_Query::is_robots()
  478. * @since 2.1.0
  479. * @uses $wp_query
  480. *
  481. * @return bool
  482. */
  483. function is_robots() {
  484. global $wp_query;
  485. if ( ! isset( $wp_query ) ) {
  486. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  487. return false;
  488. }
  489. return $wp_query->is_robots();
  490. }
  491. /**
  492. * Is the query for a search?
  493. *
  494. * @see WP_Query::is_search()
  495. * @since 1.5.0
  496. * @uses $wp_query
  497. *
  498. * @return bool
  499. */
  500. function is_search() {
  501. global $wp_query;
  502. if ( ! isset( $wp_query ) ) {
  503. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  504. return false;
  505. }
  506. return $wp_query->is_search();
  507. }
  508. /**
  509. * Is the query for an existing single post?
  510. *
  511. * Works for any post type, except attachments and pages
  512. *
  513. * If the $post parameter is specified, this function will additionally
  514. * check if the query is for one of the Posts specified.
  515. *
  516. * @see is_page()
  517. * @see is_singular()
  518. *
  519. * @see WP_Query::is_single()
  520. * @since 1.5.0
  521. * @uses $wp_query
  522. *
  523. * @param mixed $post Post ID, title, slug, or array of such.
  524. * @return bool
  525. */
  526. function is_single( $post = '' ) {
  527. global $wp_query;
  528. if ( ! isset( $wp_query ) ) {
  529. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  530. return false;
  531. }
  532. return $wp_query->is_single( $post );
  533. }
  534. /**
  535. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  536. *
  537. * If the $post_types parameter is specified, this function will additionally
  538. * check if the query is for one of the Posts Types specified.
  539. *
  540. * @see is_page()
  541. * @see is_single()
  542. *
  543. * @see WP_Query::is_singular()
  544. * @since 1.5.0
  545. * @uses $wp_query
  546. *
  547. * @param mixed $post_types Optional. Post Type or array of Post Types
  548. * @return bool
  549. */
  550. function is_singular( $post_types = '' ) {
  551. global $wp_query;
  552. if ( ! isset( $wp_query ) ) {
  553. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  554. return false;
  555. }
  556. return $wp_query->is_singular( $post_types );
  557. }
  558. /**
  559. * Is the query for a specific time?
  560. *
  561. * @see WP_Query::is_time()
  562. * @since 1.5.0
  563. * @uses $wp_query
  564. *
  565. * @return bool
  566. */
  567. function is_time() {
  568. global $wp_query;
  569. if ( ! isset( $wp_query ) ) {
  570. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  571. return false;
  572. }
  573. return $wp_query->is_time();
  574. }
  575. /**
  576. * Is the query for a trackback endpoint call?
  577. *
  578. * @see WP_Query::is_trackback()
  579. * @since 1.5.0
  580. * @uses $wp_query
  581. *
  582. * @return bool
  583. */
  584. function is_trackback() {
  585. global $wp_query;
  586. if ( ! isset( $wp_query ) ) {
  587. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  588. return false;
  589. }
  590. return $wp_query->is_trackback();
  591. }
  592. /**
  593. * Is the query for an existing year archive?
  594. *
  595. * @see WP_Query::is_year()
  596. * @since 1.5.0
  597. * @uses $wp_query
  598. *
  599. * @return bool
  600. */
  601. function is_year() {
  602. global $wp_query;
  603. if ( ! isset( $wp_query ) ) {
  604. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  605. return false;
  606. }
  607. return $wp_query->is_year();
  608. }
  609. /**
  610. * Is the query a 404 (returns no results)?
  611. *
  612. * @see WP_Query::is_404()
  613. * @since 1.5.0
  614. * @uses $wp_query
  615. *
  616. * @return bool
  617. */
  618. function is_404() {
  619. global $wp_query;
  620. if ( ! isset( $wp_query ) ) {
  621. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  622. return false;
  623. }
  624. return $wp_query->is_404();
  625. }
  626. /**
  627. * Is the query the main query?
  628. *
  629. * @since 3.3.0
  630. *
  631. * @return bool
  632. */
  633. function is_main_query() {
  634. global $wp_query;
  635. return $wp_query->is_main_query();
  636. }
  637. /*
  638. * The Loop. Post loop control.
  639. */
  640. /**
  641. * Whether current WordPress query has results to loop over.
  642. *
  643. * @see WP_Query::have_posts()
  644. * @since 1.5.0
  645. * @uses $wp_query
  646. *
  647. * @return bool
  648. */
  649. function have_posts() {
  650. global $wp_query;
  651. return $wp_query->have_posts();
  652. }
  653. /**
  654. * Whether the caller is in the Loop.
  655. *
  656. * @since 2.0.0
  657. * @uses $wp_query
  658. *
  659. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  660. */
  661. function in_the_loop() {
  662. global $wp_query;
  663. return $wp_query->in_the_loop;
  664. }
  665. /**
  666. * Rewind the loop posts.
  667. *
  668. * @see WP_Query::rewind_posts()
  669. * @since 1.5.0
  670. * @uses $wp_query
  671. *
  672. * @return null
  673. */
  674. function rewind_posts() {
  675. global $wp_query;
  676. return $wp_query->rewind_posts();
  677. }
  678. /**
  679. * Iterate the post index in the loop.
  680. *
  681. * @see WP_Query::the_post()
  682. * @since 1.5.0
  683. * @uses $wp_query
  684. */
  685. function the_post() {
  686. global $wp_query;
  687. $wp_query->the_post();
  688. }
  689. /*
  690. * Comments loop.
  691. */
  692. /**
  693. * Whether there are comments to loop over.
  694. *
  695. * @see WP_Query::have_comments()
  696. * @since 2.2.0
  697. * @uses $wp_query
  698. *
  699. * @return bool
  700. */
  701. function have_comments() {
  702. global $wp_query;
  703. return $wp_query->have_comments();
  704. }
  705. /**
  706. * Iterate comment index in the comment loop.
  707. *
  708. * @see WP_Query::the_comment()
  709. * @since 2.2.0
  710. * @uses $wp_query
  711. *
  712. * @return object
  713. */
  714. function the_comment() {
  715. global $wp_query;
  716. return $wp_query->the_comment();
  717. }
  718. /*
  719. * WP_Query
  720. */
  721. /**
  722. * The WordPress Query class.
  723. *
  724. * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  725. *
  726. * @since 1.5.0
  727. */
  728. class WP_Query {
  729. /**
  730. * Query vars set by the user
  731. *
  732. * @since 1.5.0
  733. * @access public
  734. * @var array
  735. */
  736. var $query;
  737. /**
  738. * Query vars, after parsing
  739. *
  740. * @since 1.5.0
  741. * @access public
  742. * @var array
  743. */
  744. var $query_vars = array();
  745. /**
  746. * Taxonomy query, as passed to get_tax_sql()
  747. *
  748. * @since 3.1.0
  749. * @access public
  750. * @var object WP_Tax_Query
  751. */
  752. var $tax_query;
  753. /**
  754. * Metadata query container
  755. *
  756. * @since 3.2.0
  757. * @access public
  758. * @var object WP_Meta_Query
  759. */
  760. var $meta_query = false;
  761. /**
  762. * Holds the data for a single object that is queried.
  763. *
  764. * Holds the contents of a post, page, category, attachment.
  765. *
  766. * @since 1.5.0
  767. * @access public
  768. * @var object|array
  769. */
  770. var $queried_object;
  771. /**
  772. * The ID of the queried object.
  773. *
  774. * @since 1.5.0
  775. * @access public
  776. * @var int
  777. */
  778. var $queried_object_id;
  779. /**
  780. * Get post database query.
  781. *
  782. * @since 2.0.1
  783. * @access public
  784. * @var string
  785. */
  786. var $request;
  787. /**
  788. * List of posts.
  789. *
  790. * @since 1.5.0
  791. * @access public
  792. * @var array
  793. */
  794. var $posts;
  795. /**
  796. * The amount of posts for the current query.
  797. *
  798. * @since 1.5.0
  799. * @access public
  800. * @var int
  801. */
  802. var $post_count = 0;
  803. /**
  804. * Index of the current item in the loop.
  805. *
  806. * @since 1.5.0
  807. * @access public
  808. * @var int
  809. */
  810. var $current_post = -1;
  811. /**
  812. * Whether the loop has started and the caller is in the loop.
  813. *
  814. * @since 2.0.0
  815. * @access public
  816. * @var bool
  817. */
  818. var $in_the_loop = false;
  819. /**
  820. * The current post ID.
  821. *
  822. * @since 1.5.0
  823. * @access public
  824. * @var object
  825. */
  826. var $post;
  827. /**
  828. * The list of comments for current post.
  829. *
  830. * @since 2.2.0
  831. * @access public
  832. * @var array
  833. */
  834. var $comments;
  835. /**
  836. * The amount of comments for the posts.
  837. *
  838. * @since 2.2.0
  839. * @access public
  840. * @var int
  841. */
  842. var $comment_count = 0;
  843. /**
  844. * The index of the comment in the comment loop.
  845. *
  846. * @since 2.2.0
  847. * @access public
  848. * @var int
  849. */
  850. var $current_comment = -1;
  851. /**
  852. * Current comment ID.
  853. *
  854. * @since 2.2.0
  855. * @access public
  856. * @var int
  857. */
  858. var $comment;
  859. /**
  860. * The amount of found posts for the current query.
  861. *
  862. * If limit clause was not used, equals $post_count.
  863. *
  864. * @since 2.1.0
  865. * @access public
  866. * @var int
  867. */
  868. var $found_posts = 0;
  869. /**
  870. * The amount of pages.
  871. *
  872. * @since 2.1.0
  873. * @access public
  874. * @var int
  875. */
  876. var $max_num_pages = 0;
  877. /**
  878. * The amount of comment pages.
  879. *
  880. * @since 2.7.0
  881. * @access public
  882. * @var int
  883. */
  884. var $max_num_comment_pages = 0;
  885. /**
  886. * Set if query is single post.
  887. *
  888. * @since 1.5.0
  889. * @access public
  890. * @var bool
  891. */
  892. var $is_single = false;
  893. /**
  894. * Set if query is preview of blog.
  895. *
  896. * @since 2.0.0
  897. * @access public
  898. * @var bool
  899. */
  900. var $is_preview = false;
  901. /**
  902. * Set if query returns a page.
  903. *
  904. * @since 1.5.0
  905. * @access public
  906. * @var bool
  907. */
  908. var $is_page = false;
  909. /**
  910. * Set if query is an archive list.
  911. *
  912. * @since 1.5.0
  913. * @access public
  914. * @var bool
  915. */
  916. var $is_archive = false;
  917. /**
  918. * Set if query is part of a date.
  919. *
  920. * @since 1.5.0
  921. * @access public
  922. * @var bool
  923. */
  924. var $is_date = false;
  925. /**
  926. * Set if query contains a year.
  927. *
  928. * @since 1.5.0
  929. * @access public
  930. * @var bool
  931. */
  932. var $is_year = false;
  933. /**
  934. * Set if query contains a month.
  935. *
  936. * @since 1.5.0
  937. * @access public
  938. * @var bool
  939. */
  940. var $is_month = false;
  941. /**
  942. * Set if query contains a day.
  943. *
  944. * @since 1.5.0
  945. * @access public
  946. * @var bool
  947. */
  948. var $is_day = false;
  949. /**
  950. * Set if query contains time.
  951. *
  952. * @since 1.5.0
  953. * @access public
  954. * @var bool
  955. */
  956. var $is_time = false;
  957. /**
  958. * Set if query contains an author.
  959. *
  960. * @since 1.5.0
  961. * @access public
  962. * @var bool
  963. */
  964. var $is_author = false;
  965. /**
  966. * Set if query contains category.
  967. *
  968. * @since 1.5.0
  969. * @access public
  970. * @var bool
  971. */
  972. var $is_category = false;
  973. /**
  974. * Set if query contains tag.
  975. *
  976. * @since 2.3.0
  977. * @access public
  978. * @var bool
  979. */
  980. var $is_tag = false;
  981. /**
  982. * Set if query contains taxonomy.
  983. *
  984. * @since 2.5.0
  985. * @access public
  986. * @var bool
  987. */
  988. var $is_tax = false;
  989. /**
  990. * Set if query was part of a search result.
  991. *
  992. * @since 1.5.0
  993. * @access public
  994. * @var bool
  995. */
  996. var $is_search = false;
  997. /**
  998. * Set if query is feed display.
  999. *
  1000. * @since 1.5.0
  1001. * @access public
  1002. * @var bool
  1003. */
  1004. var $is_feed = false;
  1005. /**
  1006. * Set if query is comment feed display.
  1007. *
  1008. * @since 2.2.0
  1009. * @access public
  1010. * @var bool
  1011. */
  1012. var $is_comment_feed = false;
  1013. /**
  1014. * Set if query is trackback.
  1015. *
  1016. * @since 1.5.0
  1017. * @access public
  1018. * @var bool
  1019. */
  1020. var $is_trackback = false;
  1021. /**
  1022. * Set if query is blog homepage.
  1023. *
  1024. * @since 1.5.0
  1025. * @access public
  1026. * @var bool
  1027. */
  1028. var $is_home = false;
  1029. /**
  1030. * Set if query couldn't found anything.
  1031. *
  1032. * @since 1.5.0
  1033. * @access public
  1034. * @var bool
  1035. */
  1036. var $is_404 = false;
  1037. /**
  1038. * Set if query is within comments popup window.
  1039. *
  1040. * @since 1.5.0
  1041. * @access public
  1042. * @var bool
  1043. */
  1044. var $is_comments_popup = false;
  1045. /**
  1046. * Set if query is paged
  1047. *
  1048. * @since 1.5.0
  1049. * @access public
  1050. * @var bool
  1051. */
  1052. var $is_paged = false;
  1053. /**
  1054. * Set if query is part of administration page.
  1055. *
  1056. * @since 1.5.0
  1057. * @access public
  1058. * @var bool
  1059. */
  1060. var $is_admin = false;
  1061. /**
  1062. * Set if query is an attachment.
  1063. *
  1064. * @since 2.0.0
  1065. * @access public
  1066. * @var bool
  1067. */
  1068. var $is_attachment = false;
  1069. /**
  1070. * Set if is single, is a page, or is an attachment.
  1071. *
  1072. * @since 2.1.0
  1073. * @access public
  1074. * @var bool
  1075. */
  1076. var $is_singular = false;
  1077. /**
  1078. * Set if query is for robots.
  1079. *
  1080. * @since 2.1.0
  1081. * @access public
  1082. * @var bool
  1083. */
  1084. var $is_robots = false;
  1085. /**
  1086. * Set if query contains posts.
  1087. *
  1088. * Basically, the homepage if the option isn't set for the static homepage.
  1089. *
  1090. * @since 2.1.0
  1091. * @access public
  1092. * @var bool
  1093. */
  1094. var $is_posts_page = false;
  1095. /**
  1096. * Set if query is for a post type archive.
  1097. *
  1098. * @since 3.1.0
  1099. * @access public
  1100. * @var bool
  1101. */
  1102. var $is_post_type_archive = false;
  1103. /**
  1104. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1105. * whether we have to re-parse because something has changed
  1106. *
  1107. * @since 3.1.0
  1108. * @access private
  1109. */
  1110. var $query_vars_hash = false;
  1111. /**
  1112. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1113. * via pre_get_posts hooks.
  1114. *
  1115. * @since 3.1.1
  1116. * @access private
  1117. */
  1118. var $query_vars_changed = true;
  1119. /**
  1120. * Set if post thumbnails are cached
  1121. *
  1122. * @since 3.2.0
  1123. * @access public
  1124. * @var bool
  1125. */
  1126. var $thumbnails_cached = false;
  1127. /**
  1128. * Resets query flags to false.
  1129. *
  1130. * The query flags are what page info WordPress was able to figure out.
  1131. *
  1132. * @since 2.0.0
  1133. * @access private
  1134. */
  1135. function init_query_flags() {
  1136. $this->is_single = false;
  1137. $this->is_preview = false;
  1138. $this->is_page = false;
  1139. $this->is_archive = false;
  1140. $this->is_date = false;
  1141. $this->is_year = false;
  1142. $this->is_month = false;
  1143. $this->is_day = false;
  1144. $this->is_time = false;
  1145. $this->is_author = false;
  1146. $this->is_category = false;
  1147. $this->is_tag = false;
  1148. $this->is_tax = false;
  1149. $this->is_search = false;
  1150. $this->is_feed = false;
  1151. $this->is_comment_feed = false;
  1152. $this->is_trackback = false;
  1153. $this->is_home = false;
  1154. $this->is_404 = false;
  1155. $this->is_comments_popup = false;
  1156. $this->is_paged = false;
  1157. $this->is_admin = false;
  1158. $this->is_attachment = false;
  1159. $this->is_singular = false;
  1160. $this->is_robots = false;
  1161. $this->is_posts_page = false;
  1162. $this->is_post_type_archive = false;
  1163. }
  1164. /**
  1165. * Initiates object properties and sets default values.
  1166. *
  1167. * @since 1.5.0
  1168. * @access public
  1169. */
  1170. function init() {
  1171. unset($this->posts);
  1172. unset($this->query);
  1173. $this->query_vars = array();
  1174. unset($this->queried_object);
  1175. unset($this->queried_object_id);
  1176. $this->post_count = 0;
  1177. $this->current_post = -1;
  1178. $this->in_the_loop = false;
  1179. unset( $this->request );
  1180. unset( $this->post );
  1181. unset( $this->comments );
  1182. unset( $this->comment );
  1183. $this->comment_count = 0;
  1184. $this->current_comment = -1;
  1185. $this->found_posts = 0;
  1186. $this->max_num_pages = 0;
  1187. $this->max_num_comment_pages = 0;
  1188. $this->init_query_flags();
  1189. }
  1190. /**
  1191. * Reparse the query vars.
  1192. *
  1193. * @since 1.5.0
  1194. * @access public
  1195. */
  1196. function parse_query_vars() {
  1197. $this->parse_query();
  1198. }
  1199. /**
  1200. * Fills in the query variables, which do not exist within the parameter.
  1201. *
  1202. * @since 2.1.0
  1203. * @access public
  1204. *
  1205. * @param array $array Defined query variables.
  1206. * @return array Complete query variables with undefined ones filled in empty.
  1207. */
  1208. function fill_query_vars($array) {
  1209. $keys = array(
  1210. 'error'
  1211. , 'm'
  1212. , 'p'
  1213. , 'post_parent'
  1214. , 'subpost'
  1215. , 'subpost_id'
  1216. , 'attachment'
  1217. , 'attachment_id'
  1218. , 'name'
  1219. , 'static'
  1220. , 'pagename'
  1221. , 'page_id'
  1222. , 'second'
  1223. , 'minute'
  1224. , 'hour'
  1225. , 'day'
  1226. , 'monthnum'
  1227. , 'year'
  1228. , 'w'
  1229. , 'category_name'
  1230. , 'tag'
  1231. , 'cat'
  1232. , 'tag_id'
  1233. , 'author_name'
  1234. , 'feed'
  1235. , 'tb'
  1236. , 'paged'
  1237. , 'comments_popup'
  1238. , 'meta_key'
  1239. , 'meta_value'
  1240. , 'preview'
  1241. , 's'
  1242. , 'sentence'
  1243. , 'fields'
  1244. , 'menu_order'
  1245. );
  1246. foreach ( $keys as $key ) {
  1247. if ( !isset($array[$key]) )
  1248. $array[$key] = '';
  1249. }
  1250. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1251. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in' );
  1252. foreach ( $array_keys as $key ) {
  1253. if ( !isset($array[$key]) )
  1254. $array[$key] = array();
  1255. }
  1256. return $array;
  1257. }
  1258. /**
  1259. * Parse a query string and set query type booleans.
  1260. *
  1261. * @since 1.5.0
  1262. * @access public
  1263. *
  1264. * @param string|array $query Optional query.
  1265. */
  1266. function parse_query( $query = '' ) {
  1267. if ( ! empty( $query ) ) {
  1268. $this->init();
  1269. $this->query = $this->query_vars = wp_parse_args( $query );
  1270. } elseif ( ! isset( $this->query ) ) {
  1271. $this->query = $this->query_vars;
  1272. }
  1273. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1274. $qv = &$this->query_vars;
  1275. $this->query_vars_changed = true;
  1276. if ( ! empty($qv['robots']) )
  1277. $this->is_robots = true;
  1278. $qv['p'] = absint($qv['p']);
  1279. $qv['page_id'] = absint($qv['page_id']);
  1280. $qv['year'] = absint($qv['year']);
  1281. $qv['monthnum'] = absint($qv['monthnum']);
  1282. $qv['day'] = absint($qv['day']);
  1283. $qv['w'] = absint($qv['w']);
  1284. $qv['m'] = absint($qv['m']);
  1285. $qv['paged'] = absint($qv['paged']);
  1286. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1287. $qv['pagename'] = trim( $qv['pagename'] );
  1288. $qv['name'] = trim( $qv['name'] );
  1289. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1290. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1291. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1292. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  1293. // Compat. Map subpost to attachment.
  1294. if ( '' != $qv['subpost'] )
  1295. $qv['attachment'] = $qv['subpost'];
  1296. if ( '' != $qv['subpost_id'] )
  1297. $qv['attachment_id'] = $qv['subpost_id'];
  1298. $qv['attachment_id'] = absint($qv['attachment_id']);
  1299. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1300. $this->is_single = true;
  1301. $this->is_attachment = true;
  1302. } elseif ( '' != $qv['name'] ) {
  1303. $this->is_single = true;
  1304. } elseif ( $qv['p'] ) {
  1305. $this->is_single = true;
  1306. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1307. // If year, month, day, hour, minute, and second are set, a single
  1308. // post is being queried.
  1309. $this->is_single = true;
  1310. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1311. $this->is_page = true;
  1312. $this->is_single = false;
  1313. } else {
  1314. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1315. if ( !empty($qv['s']) ) {
  1316. $this->is_search = true;
  1317. }
  1318. if ( '' !== $qv['second'] ) {
  1319. $this->is_time = true;
  1320. $this->is_date = true;
  1321. }
  1322. if ( '' !== $qv['minute'] ) {
  1323. $this->is_time = true;
  1324. $this->is_date = true;
  1325. }
  1326. if ( '' !== $qv['hour'] ) {
  1327. $this->is_time = true;
  1328. $this->is_date = true;
  1329. }
  1330. if ( $qv['day'] ) {
  1331. if ( ! $this->is_date ) {
  1332. $this->is_day = true;
  1333. $this->is_date = true;
  1334. }
  1335. }
  1336. if ( $qv['monthnum'] ) {
  1337. if ( ! $this->is_date ) {
  1338. $this->is_month = true;
  1339. $this->is_date = true;
  1340. }
  1341. }
  1342. if ( $qv['year'] ) {
  1343. if ( ! $this->is_date ) {
  1344. $this->is_year = true;
  1345. $this->is_date = true;
  1346. }
  1347. }
  1348. if ( $qv['m'] ) {
  1349. $this->is_date = true;
  1350. if ( strlen($qv['m']) > 9 ) {
  1351. $this->is_time = true;
  1352. } else if ( strlen($qv['m']) > 7 ) {
  1353. $this->is_day = true;
  1354. } else if ( strlen($qv['m']) > 5 ) {
  1355. $this->is_month = true;
  1356. } else {
  1357. $this->is_year = true;
  1358. }
  1359. }
  1360. if ( '' != $qv['w'] ) {
  1361. $this->is_date = true;
  1362. }
  1363. $this->query_vars_hash = false;
  1364. $this->parse_tax_query( $qv );
  1365. foreach ( $this->tax_query->queries as $tax_query ) {
  1366. if ( 'NOT IN' != $tax_query['operator'] ) {
  1367. switch ( $tax_query['taxonomy'] ) {
  1368. case 'category':
  1369. $this->is_category = true;
  1370. break;
  1371. case 'post_tag':
  1372. $this->is_tag = true;
  1373. break;
  1374. default:
  1375. $this->is_tax = true;
  1376. }
  1377. }
  1378. }
  1379. unset( $tax_query );
  1380. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1381. $this->is_author = false;
  1382. } else {
  1383. $this->is_author = true;
  1384. }
  1385. if ( '' != $qv['author_name'] )
  1386. $this->is_author = true;
  1387. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1388. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1389. if ( ! empty( $post_type_obj->has_archive ) )
  1390. $this->is_post_type_archive = true;
  1391. }
  1392. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1393. $this->is_archive = true;
  1394. }
  1395. if ( '' != $qv['feed'] )
  1396. $this->is_feed = true;
  1397. if ( '' != $qv['tb'] )
  1398. $this->is_trackback = true;
  1399. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1400. $this->is_paged = true;
  1401. if ( '' != $qv['comments_popup'] )
  1402. $this->is_comments_popup = true;
  1403. // if we're previewing inside the write screen
  1404. if ( '' != $qv['preview'] )
  1405. $this->is_preview = true;
  1406. if ( is_admin() )
  1407. $this->is_admin = true;
  1408. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1409. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1410. $qv['withcomments'] = 1;
  1411. }
  1412. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1413. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1414. $this->is_comment_feed = true;
  1415. 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 ) )
  1416. $this->is_home = true;
  1417. // Correct is_* for page_on_front and page_for_posts
  1418. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1419. $_query = wp_parse_args($this->query);
  1420. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1421. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1422. unset($_query['pagename']);
  1423. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1424. $this->is_page = true;
  1425. $this->is_home = false;
  1426. $qv['page_id'] = get_option('page_on_front');
  1427. // Correct <!--nextpage--> for page_on_front
  1428. if ( !empty($qv['paged']) ) {
  1429. $qv['page'] = $qv['paged'];
  1430. unset($qv['paged']);
  1431. }
  1432. }
  1433. }
  1434. if ( '' != $qv['pagename'] ) {
  1435. $this->queried_object = get_page_by_path($qv['pagename']);
  1436. if ( !empty($this->queried_object) )
  1437. $this->queried_object_id = (int) $this->queried_object->ID;
  1438. else
  1439. unset($this->queried_object);
  1440. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1441. $this->is_page = false;
  1442. $this->is_home = true;
  1443. $this->is_posts_page = true;
  1444. }
  1445. }
  1446. if ( $qv['page_id'] ) {
  1447. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1448. $this->is_page = false;
  1449. $this->is_home = true;
  1450. $this->is_posts_page = true;
  1451. }
  1452. }
  1453. if ( !empty($qv['post_type']) ) {
  1454. if ( is_array($qv['post_type']) )
  1455. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1456. else
  1457. $qv['post_type'] = sanitize_key($qv['post_type']);
  1458. }
  1459. if ( ! empty( $qv['post_status'] ) ) {
  1460. if ( is_array( $qv['post_status'] ) )
  1461. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1462. else
  1463. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1464. }
  1465. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1466. $this->is_comment_feed = false;
  1467. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1468. // Done correcting is_* for page_on_front and page_for_posts
  1469. if ( '404' == $qv['error'] )
  1470. $this->set_404();
  1471. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1472. $this->query_vars_changed = false;
  1473. do_action_ref_array('parse_query', array(&$this));
  1474. }
  1475. /*
  1476. * Parses various taxonomy related query vars.
  1477. *
  1478. * @access protected
  1479. * @since 3.1.0
  1480. *
  1481. * @param array &$q The query variables
  1482. */
  1483. function parse_tax_query( &$q ) {
  1484. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1485. $tax_query = $q['tax_query'];
  1486. } else {
  1487. $tax_query = array();
  1488. }
  1489. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1490. $tax_query[] = array(
  1491. 'taxonomy' => $q['taxonomy'],
  1492. 'terms' => array( $q['term'] ),
  1493. 'field' => 'slug',
  1494. );
  1495. }
  1496. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1497. if ( 'post_tag' == $taxonomy )
  1498. continue; // Handled further down in the $q['tag'] block
  1499. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1500. $tax_query_defaults = array(
  1501. 'taxonomy' => $taxonomy,
  1502. 'field' => 'slug',
  1503. );
  1504. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1505. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1506. }
  1507. $term = $q[$t->query_var];
  1508. if ( strpos($term, '+') !== false ) {
  1509. $terms = preg_split( '/[+]+/', $term );
  1510. foreach ( $terms as $term ) {
  1511. $tax_query[] = array_merge( $tax_query_defaults, array(
  1512. 'terms' => array( $term )
  1513. ) );
  1514. }
  1515. } else {
  1516. $tax_query[] = array_merge( $tax_query_defaults, array(
  1517. 'terms' => preg_split( '/[,]+/', $term )
  1518. ) );
  1519. }
  1520. }
  1521. }
  1522. // Category stuff
  1523. if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
  1524. $q['cat'] = ''.urldecode($q['cat']).'';
  1525. $q['cat'] = addslashes_gpc($q['cat']);
  1526. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  1527. $q['cat'] = '';
  1528. $req_cats = array();
  1529. foreach ( (array) $cat_array as $cat ) {
  1530. $cat = intval($cat);
  1531. $req_cats[] = $cat;
  1532. $in = ($cat > 0);
  1533. $cat = abs($cat);
  1534. if ( $in ) {
  1535. $q['category__in'][] = $cat;
  1536. $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
  1537. } else {
  1538. $q['category__not_in'][] = $cat;
  1539. $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
  1540. }
  1541. }
  1542. $q['cat'] = implode(',', $req_cats);
  1543. }
  1544. if ( !empty($q['category__in']) ) {
  1545. $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
  1546. $tax_query[] = array(
  1547. 'taxonomy' => 'category',
  1548. 'terms' => $q['category__in'],
  1549. 'field' => 'term_id',
  1550. 'include_children' => false
  1551. );
  1552. }
  1553. if ( !empty($q['category__not_in']) ) {
  1554. $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
  1555. $tax_query[] = array(
  1556. 'taxonomy' => 'category',
  1557. 'terms' => $q['category__not_in'],
  1558. 'operator' => 'NOT IN',
  1559. 'include_children' => false
  1560. );
  1561. }
  1562. if ( !empty($q['category__and']) ) {
  1563. $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
  1564. $tax_query[] = array(
  1565. 'taxonomy' => 'category',
  1566. 'terms' => $q['category__and'],
  1567. 'field' => 'term_id',
  1568. 'operator' => 'AND',
  1569. 'include_children' => false
  1570. );
  1571. }
  1572. // Tag stuff
  1573. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1574. if ( strpos($q['tag'], ',') !== false ) {
  1575. $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
  1576. foreach ( (array) $tags as $tag ) {
  1577. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1578. $q['tag_slug__in'][] = $tag;
  1579. }
  1580. } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
  1581. $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
  1582. foreach ( (array) $tags as $tag ) {
  1583. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1584. $q['tag_slug__and'][] = $tag;
  1585. }
  1586. } else {
  1587. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1588. $q['tag_slug__in'][] = $q['tag'];
  1589. }
  1590. }
  1591. if ( !empty($q['tag_id']) ) {
  1592. $q['tag_id'] = absint( $q['tag_id'] );
  1593. $tax_query[] = array(
  1594. 'taxonomy' => 'post_tag',
  1595. 'terms' => $q['tag_id']
  1596. );
  1597. }
  1598. if ( !empty($q['tag__in']) ) {
  1599. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1600. $tax_query[] = array(
  1601. 'taxonomy' => 'post_tag',
  1602. 'terms' => $q['tag__in']
  1603. );
  1604. }
  1605. if ( !empty($q['tag__not_in']) ) {
  1606. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1607. $tax_query[] = array(
  1608. 'taxonomy' => 'post_tag',
  1609. 'terms' => $q['tag__not_in'],
  1610. 'operator' => 'NOT IN'
  1611. );
  1612. }
  1613. if ( !empty($q['tag__and']) ) {
  1614. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1615. $tax_query[] = array(
  1616. 'taxonomy' => 'post_tag',
  1617. 'terms' => $q['tag__and'],
  1618. 'operator' => 'AND'
  1619. );
  1620. }
  1621. if ( !empty($q['tag_slug__in']) ) {
  1622. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
  1623. $tax_query[] = array(
  1624. 'taxonomy' => 'post_tag',
  1625. 'terms' => $q['tag_slug__in'],
  1626. 'field' => 'slug'
  1627. );
  1628. }
  1629. if ( !empty($q['tag_slug__and']) ) {
  1630. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
  1631. $tax_query[] = array(
  1632. 'taxonomy' => 'post_tag',
  1633. 'terms' => $q['tag_slug__and'],
  1634. 'field' => 'slug',
  1635. 'operator' => 'AND'
  1636. );
  1637. }
  1638. $this->tax_query = new WP_Tax_Query( $tax_query );
  1639. }
  1640. /**
  1641. * Sets the 404 property and saves whether query is feed.
  1642. *
  1643. * @since 2.0.0
  1644. * @access public
  1645. */
  1646. function set_404() {
  1647. $is_feed = $this->is_feed;
  1648. $this->init_query_flags();
  1649. $this->is_404 = true;
  1650. $this->is_feed = $is_feed;
  1651. }
  1652. /**
  1653. * Retrieve query variable.
  1654. *
  1655. * @since 1.5.0
  1656. * @access public
  1657. *
  1658. * @param string $query_var Query variable key.
  1659. * @return mixed
  1660. */
  1661. function get($query_var) {
  1662. if ( isset($this->query_vars[$query_var]) )
  1663. return $this->query_vars[$query_var];
  1664. return '';
  1665. }
  1666. /**
  1667. * Set query variable.
  1668. *
  1669. * @since 1.5.0
  1670. * @access public
  1671. *
  1672. * @param string $query_var Query variable key.
  1673. * @param mixed $value Query variable value.
  1674. */
  1675. function set($query_var, $value) {
  1676. $this->query_vars[$query_var] = $value;
  1677. }
  1678. /**
  1679. * Retrieve the posts based on query variables.
  1680. *
  1681. * There are a few filters and actions that can be used to modify the post
  1682. * database query.
  1683. *
  1684. * @since 1.5.0
  1685. * @access public
  1686. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1687. *
  1688. * @return array List of posts.
  1689. */
  1690. function get_posts() {
  1691. global $wpdb, $user_ID, $_wp_using_ext_object_cache;
  1692. $this->parse_query();
  1693. do_action_ref_array('pre_get_posts', array(&$this));
  1694. // Shorthand.
  1695. $q = &$this->query_vars;
  1696. // Fill again in case pre_get_posts unset some vars.
  1697. $q = $this->fill_query_vars($q);
  1698. // Parse meta query
  1699. $this->meta_query = new WP_Meta_Query();
  1700. $this->meta_query->parse_query_vars( $q );
  1701. // Set a flag if a pre_get_posts hook changed the query vars.
  1702. $hash = md5( serialize( $this->query_vars ) );
  1703. if ( $hash != $this->query_vars_hash ) {
  1704. $this->query_vars_changed = true;
  1705. $this->query_vars_hash = $hash;
  1706. }
  1707. unset($hash);
  1708. // First let's clear some variables
  1709. $distinct = '';
  1710. $whichauthor = '';
  1711. $whichmimetype = '';
  1712. $where = '';
  1713. $limits = '';
  1714. $join = '';
  1715. $search = '';
  1716. $groupby = '';
  1717. $fields = '';
  1718. $post_status_join = false;
  1719. $page = 1;
  1720. if ( isset( $q['caller_get_posts'] ) ) {
  1721. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1722. if ( !isset( $q['ignore_sticky_posts'] ) )
  1723. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1724. }
  1725. if ( !isset( $q['ignore_sticky_posts'] ) )
  1726. $q['ignore_sticky_posts'] = false;
  1727. if ( !isset($q['suppress_filters']) )
  1728. $q['suppress_filters'] = false;
  1729. if ( !isset($q['cache_results']) ) {
  1730. if ( $_wp_using_ext_object_cache )
  1731. $q['cache_results'] = false;
  1732. else
  1733. $q['cache_results'] = true;
  1734. }
  1735. if ( !isset($q['update_post_term_cache']) )
  1736. $q['update_post_term_cache'] = true;
  1737. if ( !isset($q['update_post_meta_cache']) )
  1738. $q['update_post_meta_cache'] = true;
  1739. if ( !isset($q['post_type']) ) {
  1740. if ( $this->is_search )
  1741. $q['post_type'] = 'any';
  1742. else
  1743. $q['post_type'] = '';
  1744. }
  1745. $post_type = $q['post_type'];
  1746. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  1747. $q['posts_per_page'] = get_option('posts_per_page');
  1748. if ( isset($q['showposts']) && $q['showposts'] ) {
  1749. $q['showposts'] = (int) $q['showposts'];
  1750. $q['posts_per_page'] = $q['showposts'];
  1751. }
  1752. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1753. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1754. if ( !isset($q['nopaging']) ) {
  1755. if ( $q['posts_per_page'] == -1 ) {
  1756. $q['nopaging'] = true;
  1757. } else {
  1758. $q['nopaging'] = false;
  1759. }
  1760. }
  1761. if ( $this->is_feed ) {
  1762. $q['posts_per_page'] = get_option('posts_per_rss');
  1763. $q['nopaging'] = false;
  1764. }
  1765. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1766. if ( $q['posts_per_page'] < -1 )
  1767. $q['posts_per_page'] = abs($q['posts_per_page']);
  1768. else if ( $q['posts_per_page'] == 0 )
  1769. $q['posts_per_page'] = 1;
  1770. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1771. $q['comments_per_page'] = get_option('comments_per_page');
  1772. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1773. $this->is_page = true;
  1774. $this->is_home = false;
  1775. $q['page_id'] = get_option('page_on_front');
  1776. }
  1777. if ( isset($q['page']) ) {
  1778. $q['page'] = trim($q['page'], '/');
  1779. $q['page'] = absint($q['page']);
  1780. }
  1781. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1782. if ( isset($q['no_found_rows']) )
  1783. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1784. else
  1785. $q['no_found_rows'] = false;
  1786. switch ( $q['fields'] ) {
  1787. case 'ids':
  1788. $fields = "$wpdb->posts.ID";
  1789. break;
  1790. case 'id=>parent':
  1791. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  1792. break;
  1793. default:
  1794. $fields = "$wpdb->posts.*";
  1795. }
  1796. if ( '' !== $q['menu_order'] )
  1797. $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
  1798. // If a month is specified in the querystring, load that month
  1799. if ( $q['m'] ) {
  1800. $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  1801. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  1802. if ( strlen($q['m']) > 5 )
  1803. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  1804. if ( strlen($q['m']) > 7 )
  1805. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  1806. if ( strlen($q['m']) > 9 )
  1807. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  1808. if ( strlen($q['m']) > 11 )
  1809. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  1810. if ( strlen($q['m']) > 13 )
  1811. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  1812. }
  1813. if ( '' !== $q['hour'] )
  1814. $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
  1815. if ( '' !== $q['minute'] )
  1816. $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
  1817. if ( '' !== $q['second'] )
  1818. $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
  1819. if ( $q['year'] )
  1820. $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
  1821. if ( $q['monthnum'] )
  1822. $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
  1823. if ( $q['day'] )
  1824. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
  1825. // If we've got a post_type AND it's not "any" post_type.
  1826. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  1827. foreach ( (array)$q['post_type'] as $_post_type ) {
  1828. $ptype_obj = get_post_type_object($_post_type);
  1829. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  1830. continue;
  1831. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  1832. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  1833. $q['name'] = $q[ $ptype_obj->query_var ];
  1834. } else {
  1835. // Hierarchical post_types will operate through the
  1836. $q['pagename'] = $q[ $ptype_obj->query_var ];
  1837. $q['name'] = '';
  1838. }
  1839. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  1840. break;
  1841. } //end foreach
  1842. unset($ptype_obj);
  1843. }
  1844. if ( '' != $q['name'] ) {
  1845. $q['name'] = sanitize_title_for_query( $q['name'] );
  1846. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  1847. } elseif ( '' != $q['pagename'] ) {
  1848. if ( isset($this->queried_object_id) ) {
  1849. $reqpage = $this->queried_object_id;
  1850. } else {
  1851. if ( 'page' != $q['post_type'] ) {
  1852. foreach ( (array)$q['post_type'] as $_post_type ) {
  1853. $ptype_obj = get_post_type_object($_post_type);
  1854. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  1855. continue;
  1856. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  1857. if ( $reqpage )
  1858. break;
  1859. }
  1860. unset($ptype_obj);
  1861. } else {
  1862. $reqpage = get_page_by_path($q['pagename']);
  1863. }
  1864. if ( !empty($reqpage) )
  1865. $reqpage = $reqpage->ID;
  1866. else
  1867. $reqpage = 0;
  1868. }
  1869. $page_for_posts = get_option('page_for_posts');
  1870. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  1871. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  1872. $q['name'] = $q['pagename'];
  1873. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  1874. $reqpage_obj = get_post( $reqpage );
  1875. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  1876. $this->is_attachment = true;
  1877. $post_type = $q['post_type'] = 'attachment';
  1878. $this->is_page = true;
  1879. $q['attachment_id'] = $reqpage;
  1880. }
  1881. }
  1882. } elseif ( '' != $q['attachment'] ) {
  1883. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  1884. $q['name'] = $q['attachment'];
  1885. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  1886. }
  1887. if ( $q['w'] )
  1888. $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
  1889. if ( intval($q['comments_popup']) )
  1890. $q['p'] = absint($q['comments_popup']);
  1891. // If an attachment is requested by number, let it supersede any post number.
  1892. if ( $q['attachment_id'] )
  1893. $q['p'] = absint($q['attachment_id']);
  1894. // If a post number is specified, load that post
  1895. if ( $q['p'] ) {
  1896. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  1897. } elseif ( $q['post__in'] ) {
  1898. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  1899. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  1900. } elseif ( $q['post__not_in'] ) {
  1901. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  1902. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  1903. }
  1904. if ( is_numeric( $q['post_parent'] ) ) {
  1905. $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  1906. } elseif ( $q['post_parent__in'] ) {
  1907. $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
  1908. $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
  1909. } elseif ( $q['post_parent__not_in'] ) {
  1910. $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
  1911. $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
  1912. }
  1913. if ( $q['page_id'] ) {
  1914. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  1915. $q['p'] = $q['page_id'];
  1916. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  1917. }
  1918. }
  1919. // If a search pattern is specified, load the posts that match
  1920. if ( !empty($q['s']) ) {
  1921. // added slashes screw with quote grouping when done early, so done later
  1922. $q['s'] = stripslashes($q['s']);
  1923. if ( empty( $_GET['s'] ) && $this->is_main_query() )
  1924. $q['s'] = urldecode($q['s']);
  1925. if ( !empty($q['sentence']) ) {
  1926. $q['search_terms'] = array($q['s']);
  1927. } else {
  1928. preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
  1929. $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
  1930. }
  1931. $n = !empty($q['exact']) ? '' : '%';
  1932. $searchand = '';
  1933. foreach( (array) $q['search_terms'] as $term ) {
  1934. $term = esc_sql( like_escape( $term ) );
  1935. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  1936. $searchand = ' AND ';
  1937. }
  1938. if ( !empty($search) ) {
  1939. $search = " AND ({$search}) ";
  1940. if ( !is_user_logged_in() )
  1941. $search .= " AND ($wpdb->posts.post_password = '') ";
  1942. }
  1943. }
  1944. // Allow plugins to contextually add/remove/modify the search section of the database query
  1945. $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
  1946. // Taxonomies
  1947. if ( !$this->is_singular ) {
  1948. $this->parse_tax_query( $q );
  1949. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
  1950. $join .= $clauses['join'];
  1951. $where .= $clauses['where'];
  1952. }
  1953. if ( $this->is_tax ) {
  1954. if ( empty($post_type) ) {
  1955. // Do a fully inclusive search for currently registered post types of queried taxonomies
  1956. $post_type = array();
  1957. $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
  1958. foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
  1959. $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
  1960. if ( array_intersect( $taxonomies, $object_taxonomies ) )
  1961. $post_type[] = $pt;
  1962. }
  1963. if ( ! $post_type )
  1964. $post_type = 'any';
  1965. $post_status_join = true;
  1966. } elseif ( in_array('attachment', (array) $post_type) ) {
  1967. $post_status_join = true;
  1968. }
  1969. }
  1970. // Back-compat
  1971. if ( !empty($this->tax_query->queries) ) {
  1972. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  1973. if ( !empty( $tax_query_in_and ) ) {
  1974. if ( !isset( $q['taxonomy'] ) ) {
  1975. foreach ( $tax_query_in_and as $a_tax_query ) {
  1976. if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
  1977. $q['taxonomy'] = $a_tax_query['taxonomy'];
  1978. if ( 'slug' == $a_tax_query['field'] )
  1979. $q['term'] = $a_tax_query['terms'][0];
  1980. else
  1981. $q['term_id'] = $a_tax_query['terms'][0];
  1982. break;
  1983. }
  1984. }
  1985. }
  1986. $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
  1987. if ( !empty( $cat_query ) ) {
  1988. $cat_query = reset( $cat_query );
  1989. $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
  1990. if ( $the_cat ) {
  1991. $this->set( 'cat', $the_cat->term_id );
  1992. $this->set( 'category_name', $the_cat->slug );
  1993. }
  1994. unset( $the_cat );
  1995. }
  1996. unset( $cat_query );
  1997. $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
  1998. if ( !empty( $tag_query ) ) {
  1999. $tag_query = reset( $tag_query );
  2000. $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
  2001. if ( $the_tag ) {
  2002. $this->set( 'tag_id', $the_tag->term_id );
  2003. }
  2004. unset( $the_tag );
  2005. }
  2006. unset( $tag_query );
  2007. }
  2008. }
  2009. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
  2010. $groupby = "{$wpdb->posts}.ID";
  2011. }
  2012. // Author/user stuff
  2013. if ( empty($q['author']) || ($q['author'] == '0') ) {
  2014. $whichauthor = '';
  2015. } else {
  2016. $q['author'] = (string)urldecode($q['author']);
  2017. $q['author'] = addslashes_gpc($q['author']);
  2018. if ( strpos($q['author'], '-') !== false ) {
  2019. $eq = '!=';
  2020. $andor = 'AND';
  2021. $q['author'] = explode('-', $q['author']);
  2022. $q['author'] = (string)absint($q['author'][1]);
  2023. } else {
  2024. $eq = '=';
  2025. $andor = 'OR';
  2026. }
  2027. $author_array = preg_split('/[,\s]+/', $q['author']);
  2028. $_author_array = array();
  2029. foreach ( $author_array as $key => $_author )
  2030. $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
  2031. $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
  2032. unset($author_array, $_author_array);
  2033. }
  2034. // Author stuff for nice URLs
  2035. if ( '' != $q['author_name'] ) {
  2036. if ( strpos($q['author_name'], '/') !== false ) {
  2037. $q['author_name'] = explode('/', $q['author_name']);
  2038. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  2039. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  2040. } else {
  2041. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
  2042. }
  2043. }
  2044. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  2045. $q['author'] = get_user_by('slug', $q['author_name']);
  2046. if ( $q['author'] )
  2047. $q['author'] = $q['author']->ID;
  2048. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  2049. }
  2050. // MIME-Type stuff for attachment browsing
  2051. if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
  2052. $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
  2053. $where .= $search . $whichauthor . $whichmimetype;
  2054. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  2055. $q['order'] = 'DESC';
  2056. // Order by
  2057. if ( empty($q['orderby']) ) {
  2058. $orderby = "$wpdb->posts.post_date " . $q['order'];
  2059. } elseif ( 'none' == $q['orderby'] ) {
  2060. $orderby = '';
  2061. } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
  2062. $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
  2063. } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
  2064. $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
  2065. } else {
  2066. // Used to filter values
  2067. $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2068. if ( !empty($q['meta_key']) ) {
  2069. $allowed_keys[] = $q['meta_key'];
  2070. $allowed_keys[] = 'meta_value';
  2071. $allowed_keys[] = 'meta_value_num';
  2072. }
  2073. $q['orderby'] = urldecode($q['orderby']);
  2074. $q['orderby'] = addslashes_gpc($q['orderby']);
  2075. $orderby_array = array();
  2076. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  2077. // Only allow certain values for safety
  2078. if ( ! in_array($orderby, $allowed_keys) )
  2079. continue;
  2080. switch ( $orderby ) {
  2081. case 'menu_order':
  2082. $orderby = "$wpdb->posts.menu_order";
  2083. break;
  2084. case 'ID':
  2085. $orderby = "$wpdb->posts.ID";
  2086. break;
  2087. case 'rand':
  2088. $orderby = 'RAND()';
  2089. break;
  2090. case $q['meta_key']:
  2091. case 'meta_value':
  2092. $orderby = "$wpdb->postmeta.meta_value";
  2093. break;
  2094. case 'meta_value_num':
  2095. $orderby = "$wpdb->postmeta.meta_value+0";
  2096. break;
  2097. case 'comment_count':
  2098. $orderby = "$wpdb->posts.comment_count";
  2099. break;
  2100. default:
  2101. $orderby = "$wpdb->posts.post_" . $orderby;
  2102. }
  2103. $orderby_array[] = $orderby;
  2104. }
  2105. $orderby = implode( ',', $orderby_array );
  2106. if ( empty( $orderby ) )
  2107. $orderby = "$wpdb->posts.post_date ".$q['order'];
  2108. else
  2109. $orderby .= " {$q['order']}";
  2110. }
  2111. if ( is_array( $post_type ) ) {
  2112. $post_type_cap = 'multiple_post_type';
  2113. } else {
  2114. $post_type_object = get_post_type_object( $post_type );
  2115. if ( empty( $post_type_object ) )
  2116. $post_type_cap = $post_type;
  2117. }
  2118. if ( 'any' == $post_type ) {
  2119. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  2120. if ( ! empty( $in_search_post_types ) )
  2121. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
  2122. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2123. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2124. } elseif ( ! empty( $post_type ) ) {
  2125. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  2126. $post_type_object = get_post_type_object ( $post_type );
  2127. } elseif ( $this->is_attachment ) {
  2128. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  2129. $post_type_object = get_post_type_object ( 'attachment' );
  2130. } elseif ( $this->is_page ) {
  2131. $where .= " AND $wpdb->posts.post_type = 'page'";
  2132. $post_type_object = get_post_type_object ( 'page' );
  2133. } else {
  2134. $where .= " AND $wpdb->posts.post_type = 'post'";
  2135. $post_type_object = get_post_type_object ( 'post' );
  2136. }
  2137. if ( ! empty( $post_type_object ) ) {
  2138. $edit_cap = $post_type_object->cap->edit_post;
  2139. $read_cap = $post_type_object->cap->read_post;
  2140. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  2141. $read_private_cap = $post_type_object->cap->read_private_posts;
  2142. } else {
  2143. $edit_cap = 'edit_' . $post_type_cap;
  2144. $read_cap = 'read_' . $post_type_cap;
  2145. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  2146. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  2147. }
  2148. if ( ! empty( $q['post_status'] ) ) {
  2149. $statuswheres = array();
  2150. $q_status = $q['post_status'];
  2151. if ( ! is_array( $q_status ) )
  2152. $q_status = explode(',', $q_status);
  2153. $r_status = array();
  2154. $p_status = array();
  2155. $e_status = array();
  2156. if ( in_array('any', $q_status) ) {
  2157. foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  2158. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  2159. } else {
  2160. foreach ( get_post_stati() as $status ) {
  2161. if ( in_array( $status, $q_status ) ) {
  2162. if ( 'private' == $status )
  2163. $p_status[] = "$wpdb->posts.post_status = '$status'";
  2164. else
  2165. $r_status[] = "$wpdb->posts.post_status = '$status'";
  2166. }
  2167. }
  2168. }
  2169. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2170. $r_status = array_merge($r_status, $p_status);
  2171. unset($p_status);
  2172. }
  2173. if ( !empty($e_status) ) {
  2174. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2175. }
  2176. if ( !empty($r_status) ) {
  2177. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2178. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
  2179. else
  2180. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2181. }
  2182. if ( !empty($p_status) ) {
  2183. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2184. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
  2185. else
  2186. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2187. }
  2188. if ( $post_status_join ) {
  2189. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2190. foreach ( $statuswheres as $index => $statuswhere )
  2191. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  2192. }
  2193. foreach ( $statuswheres as $statuswhere )
  2194. $where .= " AND $statuswhere";
  2195. } elseif ( !$this->is_singular ) {
  2196. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  2197. // Add public states.
  2198. $public_states = get_post_stati( array('public' => true) );
  2199. foreach ( (array) $public_states as $state ) {
  2200. if ( 'publish' == $state ) // Publish is hard-coded above.
  2201. continue;
  2202. $where .= " OR $wpdb->posts.post_status = '$state'";
  2203. }
  2204. if ( $this->is_admin ) {
  2205. // Add protected states that should show in the admin all list.
  2206. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2207. foreach ( (array) $admin_all_states as $state )
  2208. $where .= " OR $wpdb->posts.post_status = '$state'";
  2209. }
  2210. if ( is_user_logged_in() ) {
  2211. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2212. $private_states = get_post_stati( array('private' => true) );
  2213. foreach ( (array) $private_states as $state )
  2214. $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'";
  2215. }
  2216. $where .= ')';
  2217. }
  2218. if ( !empty( $this->meta_query->queries ) ) {
  2219. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  2220. $join .= $clauses['join'];
  2221. $where .= $clauses['where'];
  2222. }
  2223. // Apply filters on where and join prior to paging so that any
  2224. // manipulations to them are reflected in the paging by day queries.
  2225. if ( !$q['suppress_filters'] ) {
  2226. $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
  2227. $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
  2228. }
  2229. // Paging
  2230. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2231. $page = absint($q['paged']);
  2232. if ( !$page )
  2233. $page = 1;
  2234. if ( empty($q['offset']) ) {
  2235. $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2236. } else { // we're ignoring $page and using 'offset'
  2237. $q['offset'] = absint($q['offset']);
  2238. $pgstrt = $q['offset'] . ', ';
  2239. }
  2240. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2241. }
  2242. // Comments feeds
  2243. if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2244. if ( $this->is_archive || $this->is_search ) {
  2245. $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  2246. $cwhere = "WHERE comment_approved = '1' $where";
  2247. $cgroupby = "$wpdb->comments.comment_id";
  2248. } else { // Other non singular e.g. front
  2249. $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  2250. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2251. $cgroupby = '';
  2252. }
  2253. if ( !$q['suppress_filters'] ) {
  2254. $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
  2255. $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
  2256. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
  2257. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2258. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2259. }
  2260. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2261. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2262. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2263. $this->comment_count = count($this->comments);
  2264. $post_ids = array();
  2265. foreach ( $this->comments as $comment )
  2266. $post_ids[] = (int) $comment->comment_post_ID;
  2267. $post_ids = join(',', $post_ids);
  2268. $join = '';
  2269. if ( $post_ids )
  2270. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2271. else
  2272. $where = "AND 0";
  2273. }
  2274. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2275. // Apply post-paging filters on where and join. Only plugins that
  2276. // manipulate paging queries should use these hooks.
  2277. if ( !$q['suppress_filters'] ) {
  2278. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2279. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2280. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2281. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2282. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2283. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2284. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2285. // Filter all clauses at once, for convenience
  2286. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2287. foreach ( $pieces as $piece )
  2288. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2289. }
  2290. // Announce current selection parameters. For use by caching plugins.
  2291. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2292. // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
  2293. if ( !$q['suppress_filters'] ) {
  2294. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2295. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2296. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2297. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2298. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2299. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2300. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2301. // Filter all clauses at once, for convenience
  2302. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
  2303. foreach ( $pieces as $piece )
  2304. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2305. }
  2306. if ( ! empty($groupby) )
  2307. $groupby = 'GROUP BY ' . $groupby;
  2308. if ( !empty( $orderby ) )
  2309. $orderby = 'ORDER BY ' . $orderby;
  2310. $found_rows = '';
  2311. if ( !$q['no_found_rows'] && !empty($limits) )
  2312. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2313. $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2314. if ( !$q['suppress_filters'] ) {
  2315. $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
  2316. }
  2317. if ( 'ids' == $q['fields'] ) {
  2318. $this->posts = $wpdb->get_col( $this->request );
  2319. $this->post_count = count( $this->posts );
  2320. $this->set_found_posts( $q, $limits );
  2321. return $this->posts;
  2322. }
  2323. if ( 'id=>parent' == $q['fields'] ) {
  2324. $this->posts = $wpdb->get_results( $this->request );
  2325. $this->post_count = count( $this->posts );
  2326. $this->set_found_posts( $q, $limits );
  2327. $r = array();
  2328. foreach ( $this->posts as $post )
  2329. $r[ $post->ID ] = $post->post_parent;
  2330. return $r;
  2331. }
  2332. $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
  2333. $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
  2334. if ( $split_the_query ) {
  2335. // First get the IDs and then fill in the objects
  2336. $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2337. $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
  2338. $ids = $wpdb->get_col( $this->request );
  2339. if ( $ids ) {
  2340. $this->posts = $ids;
  2341. $this->set_found_posts( $q, $limits );
  2342. _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
  2343. } else {
  2344. $this->posts = array();
  2345. }
  2346. } else {
  2347. $this->posts = $wpdb->get_results( $this->request );
  2348. $this->set_found_posts( $q, $limits );
  2349. }
  2350. // Convert to WP_Post objects
  2351. if ( $this->posts )
  2352. $this->posts = array_map( 'get_post', $this->posts );
  2353. // Raw results filter. Prior to status checks.
  2354. if ( !$q['suppress_filters'] )
  2355. $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  2356. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2357. $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
  2358. $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2359. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
  2360. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2361. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2362. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2363. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2364. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2365. $this->comments = $wpdb->get_results($comments_request);
  2366. $this->comment_count = count($this->comments);
  2367. }
  2368. // Check post status to determine if post should be displayed.
  2369. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2370. $status = get_post_status($this->posts[0]);
  2371. $post_status_obj = get_post_status_object($status);
  2372. //$type = get_post_type($this->posts[0]);
  2373. if ( !$post_status_obj->public ) {
  2374. if ( ! is_user_logged_in() ) {
  2375. // User must be logged in to view unpublished posts.
  2376. $this->posts = array();
  2377. } else {
  2378. if ( $post_status_obj->protected ) {
  2379. // User must have edit permissions on the draft to preview.
  2380. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2381. $this->posts = array();
  2382. } else {
  2383. $this->is_preview = true;
  2384. if ( 'future' != $status )
  2385. $this->posts[0]->post_date = current_time('mysql');
  2386. }
  2387. } elseif ( $post_status_obj->private ) {
  2388. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2389. $this->posts = array();
  2390. } else {
  2391. $this->posts = array();
  2392. }
  2393. }
  2394. }
  2395. if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
  2396. $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
  2397. }
  2398. // Put sticky posts at the top of the posts array
  2399. $sticky_posts = get_option('sticky_posts');
  2400. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  2401. $num_posts = count($this->posts);
  2402. $sticky_offset = 0;
  2403. // Loop over posts and relocate stickies to the front.
  2404. for ( $i = 0; $i < $num_posts; $i++ ) {
  2405. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2406. $sticky_post = $this->posts[$i];
  2407. // Remove sticky from current position
  2408. array_splice($this->posts, $i, 1);
  2409. // Move to front, after other stickies
  2410. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2411. // Increment the sticky offset. The next sticky will be placed at this offset.
  2412. $sticky_offset++;
  2413. // Remove post from sticky posts array
  2414. $offset = array_search($sticky_post->ID, $sticky_posts);
  2415. unset( $sticky_posts[$offset] );
  2416. }
  2417. }
  2418. // If any posts have been excluded specifically, Ignore those that are sticky.
  2419. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  2420. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2421. // Fetch sticky posts that weren't in the query results
  2422. if ( !empty($sticky_posts) ) {
  2423. $stickies = get_posts( array(
  2424. 'post__in' => $sticky_posts,
  2425. 'post_type' => $post_type,
  2426. 'post_status' => 'publish',
  2427. 'nopaging' => true
  2428. ) );
  2429. foreach ( $stickies as $sticky_post ) {
  2430. array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
  2431. $sticky_offset++;
  2432. }
  2433. }
  2434. }
  2435. if ( !$q['suppress_filters'] )
  2436. $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
  2437. // Ensure that any posts added/modified via one of the filters above are
  2438. // of the type WP_Post and are filtered.
  2439. if ( $this->posts ) {
  2440. $this->post_count = count( $this->posts );
  2441. $this->posts = array_map( 'get_post', $this->posts );
  2442. if ( $q['cache_results'] )
  2443. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2444. $this->post = reset( $this->posts );
  2445. } else {
  2446. $this->post_count = 0;
  2447. $this->posts = array();
  2448. }
  2449. return $this->posts;
  2450. }
  2451. /**
  2452. * Set up the amount of found posts and the number of pages (if limit clause was used)
  2453. * for the current query.
  2454. *
  2455. * @since 3.5.0
  2456. * @access private
  2457. */
  2458. function set_found_posts( $q, $limits ) {
  2459. global $wpdb;
  2460. // Bail if posts is an empty array. Continue if posts is an empty string
  2461. // null, or false to accommodate caching plugins that fill posts later.
  2462. if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
  2463. return;
  2464. if ( ! empty( $limits ) )
  2465. $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
  2466. else
  2467. $this->found_posts = count( $this->posts );
  2468. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  2469. if ( ! empty( $limits ) )
  2470. $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
  2471. }
  2472. /**
  2473. * Set up the next post and iterate current post index.
  2474. *
  2475. * @since 1.5.0
  2476. * @access public
  2477. *
  2478. * @return WP_Post Next post.
  2479. */
  2480. function next_post() {
  2481. $this->current_post++;
  2482. $this->post = $this->posts[$this->current_post];
  2483. return $this->post;
  2484. }
  2485. /**
  2486. * Sets up the current post.
  2487. *
  2488. * Retrieves the next post, sets up the post, sets the 'in the loop'
  2489. * property to true.
  2490. *
  2491. * @since 1.5.0
  2492. * @access public
  2493. * @uses $post
  2494. * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
  2495. */
  2496. function the_post() {
  2497. global $post;
  2498. $this->in_the_loop = true;
  2499. if ( $this->current_post == -1 ) // loop has just started
  2500. do_action_ref_array('loop_start', array(&$this));
  2501. $post = $this->next_post();
  2502. setup_postdata($post);
  2503. }
  2504. /**
  2505. * Whether there are more posts available in the loop.
  2506. *
  2507. * Calls action 'loop_end', when the loop is complete.
  2508. *
  2509. * @since 1.5.0
  2510. * @access public
  2511. * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  2512. *
  2513. * @return bool True if posts are available, false if end of loop.
  2514. */
  2515. function have_posts() {
  2516. if ( $this->current_post + 1 < $this->post_count ) {
  2517. return true;
  2518. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  2519. do_action_ref_array('loop_end', array(&$this));
  2520. // Do some cleaning up after the loop
  2521. $this->rewind_posts();
  2522. }
  2523. $this->in_the_loop = false;
  2524. return false;
  2525. }
  2526. /**
  2527. * Rewind the posts and reset post index.
  2528. *
  2529. * @since 1.5.0
  2530. * @access public
  2531. */
  2532. function rewind_posts() {
  2533. $this->current_post = -1;
  2534. if ( $this->post_count > 0 ) {
  2535. $this->post = $this->posts[0];
  2536. }
  2537. }
  2538. /**
  2539. * Iterate current comment index and return comment object.
  2540. *
  2541. * @since 2.2.0
  2542. * @access public
  2543. *
  2544. * @return object Comment object.
  2545. */
  2546. function next_comment() {
  2547. $this->current_comment++;
  2548. $this->comment = $this->comments[$this->current_comment];
  2549. return $this->comment;
  2550. }
  2551. /**
  2552. * Sets up the current comment.
  2553. *
  2554. * @since 2.2.0
  2555. * @access public
  2556. * @global object $comment Current comment.
  2557. * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
  2558. */
  2559. function the_comment() {
  2560. global $comment;
  2561. $comment = $this->next_comment();
  2562. if ( $this->current_comment == 0 ) {
  2563. do_action('comment_loop_start');
  2564. }
  2565. }
  2566. /**
  2567. * Whether there are more comments available.
  2568. *
  2569. * Automatically rewinds comments when finished.
  2570. *
  2571. * @since 2.2.0
  2572. * @access public
  2573. *
  2574. * @return bool True, if more comments. False, if no more posts.
  2575. */
  2576. function have_comments() {
  2577. if ( $this->current_comment + 1 < $this->comment_count ) {
  2578. return true;
  2579. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  2580. $this->rewind_comments();
  2581. }
  2582. return false;
  2583. }
  2584. /**
  2585. * Rewind the comments, resets the comment index and comment to first.
  2586. *
  2587. * @since 2.2.0
  2588. * @access public
  2589. */
  2590. function rewind_comments() {
  2591. $this->current_comment = -1;
  2592. if ( $this->comment_count > 0 ) {
  2593. $this->comment = $this->comments[0];
  2594. }
  2595. }
  2596. /**
  2597. * Sets up the WordPress query by parsing query string.
  2598. *
  2599. * @since 1.5.0
  2600. * @access public
  2601. *
  2602. * @param string $query URL query string.
  2603. * @return array List of posts.
  2604. */
  2605. function query( $query ) {
  2606. $this->init();
  2607. $this->query = $this->query_vars = wp_parse_args( $query );
  2608. return $this->get_posts();
  2609. }
  2610. /**
  2611. * Retrieve queried object.
  2612. *
  2613. * If queried object is not set, then the queried object will be set from
  2614. * the category, tag, taxonomy, posts page, single post, page, or author
  2615. * query variable. After it is set up, it will be returned.
  2616. *
  2617. * @since 1.5.0
  2618. * @access public
  2619. *
  2620. * @return object
  2621. */
  2622. function get_queried_object() {
  2623. if ( isset($this->queried_object) )
  2624. return $this->queried_object;
  2625. $this->queried_object = null;
  2626. $this->queried_object_id = 0;
  2627. if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  2628. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  2629. $query = reset( $tax_query_in_and );
  2630. if ( 'term_id' == $query['field'] )
  2631. $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
  2632. elseif ( $query['terms'] )
  2633. $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
  2634. if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
  2635. $this->queried_object = $term;
  2636. $this->queried_object_id = (int) $term->term_id;
  2637. if ( $this->is_category )
  2638. _make_cat_compat( $this->queried_object );
  2639. }
  2640. } elseif ( $this->is_post_type_archive ) {
  2641. $this->queried_object = get_post_type_object( $this->get('post_type') );
  2642. } elseif ( $this->is_posts_page ) {
  2643. $page_for_posts = get_option('page_for_posts');
  2644. $this->queried_object = get_post( $page_for_posts );
  2645. $this->queried_object_id = (int) $this->queried_object->ID;
  2646. } elseif ( $this->is_singular && !is_null($this->post) ) {
  2647. $this->queried_object = $this->post;
  2648. $this->queried_object_id = (int) $this->post->ID;
  2649. } elseif ( $this->is_author ) {
  2650. $this->queried_object_id = (int) $this->get('author');
  2651. $this->queried_object = get_userdata( $this->queried_object_id );
  2652. }
  2653. return $this->queried_object;
  2654. }
  2655. /**
  2656. * Retrieve ID of the current queried object.
  2657. *
  2658. * @since 1.5.0
  2659. * @access public
  2660. *
  2661. * @return int
  2662. */
  2663. function get_queried_object_id() {
  2664. $this->get_queried_object();
  2665. if ( isset($this->queried_object_id) ) {
  2666. return $this->queried_object_id;
  2667. }
  2668. return 0;
  2669. }
  2670. /**
  2671. * Constructor.
  2672. *
  2673. * Sets up the WordPress query, if parameter is not empty.
  2674. *
  2675. * @since 1.5.0
  2676. * @access public
  2677. *
  2678. * @param string $query URL query string.
  2679. * @return WP_Query
  2680. */
  2681. function __construct($query = '') {
  2682. if ( ! empty($query) ) {
  2683. $this->query($query);
  2684. }
  2685. }
  2686. /**
  2687. * Is the query for an existing archive page?
  2688. *
  2689. * Month, Year, Category, Author, Post Type archive...
  2690. *
  2691. * @since 3.1.0
  2692. *
  2693. * @return bool
  2694. */
  2695. function is_archive() {
  2696. return (bool) $this->is_archive;
  2697. }
  2698. /**
  2699. * Is the query for an existing post type archive page?
  2700. *
  2701. * @since 3.1.0
  2702. *
  2703. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  2704. * @return bool
  2705. */
  2706. function is_post_type_archive( $post_types = '' ) {
  2707. if ( empty( $post_types ) || !$this->is_post_type_archive )
  2708. return (bool) $this->is_post_type_archive;
  2709. $post_type_object = $this->get_queried_object();
  2710. return in_array( $post_type_object->name, (array) $post_types );
  2711. }
  2712. /**
  2713. * Is the query for an existing attachment page?
  2714. *
  2715. * @since 3.1.0
  2716. *
  2717. * @return bool
  2718. */
  2719. function is_attachment() {
  2720. return (bool) $this->is_attachment;
  2721. }
  2722. /**
  2723. * Is the query for an existing author archive page?
  2724. *
  2725. * If the $author parameter is specified, this function will additionally
  2726. * check if the query is for one of the authors specified.
  2727. *
  2728. * @since 3.1.0
  2729. *
  2730. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  2731. * @return bool
  2732. */
  2733. function is_author( $author = '' ) {
  2734. if ( !$this->is_author )
  2735. return false;
  2736. if ( empty($author) )
  2737. return true;
  2738. $author_obj = $this->get_queried_object();
  2739. $author = (array) $author;
  2740. if ( in_array( $author_obj->ID, $author ) )
  2741. return true;
  2742. elseif ( in_array( $author_obj->nickname, $author ) )
  2743. return true;
  2744. elseif ( in_array( $author_obj->user_nicename, $author ) )
  2745. return true;
  2746. return false;
  2747. }
  2748. /**
  2749. * Is the query for an existing category archive page?
  2750. *
  2751. * If the $category parameter is specified, this function will additionally
  2752. * check if the query is for one of the categories specified.
  2753. *
  2754. * @since 3.1.0
  2755. *
  2756. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  2757. * @return bool
  2758. */
  2759. function is_category( $category = '' ) {
  2760. if ( !$this->is_category )
  2761. return false;
  2762. if ( empty($category) )
  2763. return true;
  2764. $cat_obj = $this->get_queried_object();
  2765. $category = (array) $category;
  2766. if ( in_array( $cat_obj->term_id, $category ) )
  2767. return true;
  2768. elseif ( in_array( $cat_obj->name, $category ) )
  2769. return true;
  2770. elseif ( in_array( $cat_obj->slug, $category ) )
  2771. return true;
  2772. return false;
  2773. }
  2774. /**
  2775. * Is the query for an existing tag archive page?
  2776. *
  2777. * If the $tag parameter is specified, this function will additionally
  2778. * check if the query is for one of the tags specified.
  2779. *
  2780. * @since 3.1.0
  2781. *
  2782. * @param mixed $slug Optional. Tag slug or array of slugs.
  2783. * @return bool
  2784. */
  2785. function is_tag( $slug = '' ) {
  2786. if ( !$this->is_tag )
  2787. return false;
  2788. if ( empty( $slug ) )
  2789. return true;
  2790. $tag_obj = $this->get_queried_object();
  2791. $slug = (array) $slug;
  2792. if ( in_array( $tag_obj->slug, $slug ) )
  2793. return true;
  2794. return false;
  2795. }
  2796. /**
  2797. * Is the query for an existing taxonomy archive page?
  2798. *
  2799. * If the $taxonomy parameter is specified, this function will additionally
  2800. * check if the query is for that specific $taxonomy.
  2801. *
  2802. * If the $term parameter is specified in addition to the $taxonomy parameter,
  2803. * this function will additionally check if the query is for one of the terms
  2804. * specified.
  2805. *
  2806. * @since 3.1.0
  2807. *
  2808. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  2809. * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  2810. * @return bool
  2811. */
  2812. function is_tax( $taxonomy = '', $term = '' ) {
  2813. global $wp_taxonomies;
  2814. if ( !$this->is_tax )
  2815. return false;
  2816. if ( empty( $taxonomy ) )
  2817. return true;
  2818. $queried_object = $this->get_queried_object();
  2819. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
  2820. $term_array = (array) $term;
  2821. // Check that the taxonomy matches.
  2822. if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
  2823. return false;
  2824. // Only a Taxonomy provided.
  2825. if ( empty( $term ) )
  2826. return true;
  2827. return isset( $queried_object->term_id ) &&
  2828. count( array_intersect(
  2829. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
  2830. $term_array
  2831. ) );
  2832. }
  2833. /**
  2834. * Whether the current URL is within the comments popup window.
  2835. *
  2836. * @since 3.1.0
  2837. *
  2838. * @return bool
  2839. */
  2840. function is_comments_popup() {
  2841. return (bool) $this->is_comments_popup;
  2842. }
  2843. /**
  2844. * Is the query for an existing date archive?
  2845. *
  2846. * @since 3.1.0
  2847. *
  2848. * @return bool
  2849. */
  2850. function is_date() {
  2851. return (bool) $this->is_date;
  2852. }
  2853. /**
  2854. * Is the query for an existing day archive?
  2855. *
  2856. * @since 3.1.0
  2857. *
  2858. * @return bool
  2859. */
  2860. function is_day() {
  2861. return (bool) $this->is_day;
  2862. }
  2863. /**
  2864. * Is the query for a feed?
  2865. *
  2866. * @since 3.1.0
  2867. *
  2868. * @param string|array $feeds Optional feed types to check.
  2869. * @return bool
  2870. */
  2871. function is_feed( $feeds = '' ) {
  2872. if ( empty( $feeds ) || ! $this->is_feed )
  2873. return (bool) $this->is_feed;
  2874. $qv = $this->get( 'feed' );
  2875. if ( 'feed' == $qv )
  2876. $qv = get_default_feed();
  2877. return in_array( $qv, (array) $feeds );
  2878. }
  2879. /**
  2880. * Is the query for a comments feed?
  2881. *
  2882. * @since 3.1.0
  2883. *
  2884. * @return bool
  2885. */
  2886. function is_comment_feed() {
  2887. return (bool) $this->is_comment_feed;
  2888. }
  2889. /**
  2890. * Is the query for the front page of the site?
  2891. *
  2892. * This is for what is displayed at your site's main URL.
  2893. *
  2894. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  2895. *
  2896. * If you set a static page for the front page of your site, this function will return
  2897. * true when viewing that page.
  2898. *
  2899. * Otherwise the same as @see WP_Query::is_home()
  2900. *
  2901. * @since 3.1.0
  2902. * @uses is_home()
  2903. * @uses get_option()
  2904. *
  2905. * @return bool True, if front of site.
  2906. */
  2907. function is_front_page() {
  2908. // most likely case
  2909. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
  2910. return true;
  2911. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
  2912. return true;
  2913. else
  2914. return false;
  2915. }
  2916. /**
  2917. * Is the query for the blog homepage?
  2918. *
  2919. * This is the page which shows the time based blog content of your site.
  2920. *
  2921. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  2922. *
  2923. * If you set a static page for the front page of your site, this function will return
  2924. * true only on the page you set as the "Posts page".
  2925. *
  2926. * @see WP_Query::is_front_page()
  2927. *
  2928. * @since 3.1.0
  2929. *
  2930. * @return bool True if blog view homepage.
  2931. */
  2932. function is_home() {
  2933. return (bool) $this->is_home;
  2934. }
  2935. /**
  2936. * Is the query for an existing month archive?
  2937. *
  2938. * @since 3.1.0
  2939. *
  2940. * @return bool
  2941. */
  2942. function is_month() {
  2943. return (bool) $this->is_month;
  2944. }
  2945. /**
  2946. * Is the query for an existing single page?
  2947. *
  2948. * If the $page parameter is specified, this function will additionally
  2949. * check if the query is for one of the pages specified.
  2950. *
  2951. * @see WP_Query::is_single()
  2952. * @see WP_Query::is_singular()
  2953. *
  2954. * @since 3.1.0
  2955. *
  2956. * @param mixed $page Page ID, title, slug, or array of such.
  2957. * @return bool
  2958. */
  2959. function is_page( $page = '' ) {
  2960. if ( !$this->is_page )
  2961. return false;
  2962. if ( empty( $page ) )
  2963. return true;
  2964. $page_obj = $this->get_queried_object();
  2965. $page = (array) $page;
  2966. if ( in_array( $page_obj->ID, $page ) )
  2967. return true;
  2968. elseif ( in_array( $page_obj->post_title, $page ) )
  2969. return true;
  2970. else if ( in_array( $page_obj->post_name, $page ) )
  2971. return true;
  2972. return false;
  2973. }
  2974. /**
  2975. * Is the query for paged result and not for the first page?
  2976. *
  2977. * @since 3.1.0
  2978. *
  2979. * @return bool
  2980. */
  2981. function is_paged() {
  2982. return (bool) $this->is_paged;
  2983. }
  2984. /**
  2985. * Is the query for a post or page preview?
  2986. *
  2987. * @since 3.1.0
  2988. *
  2989. * @return bool
  2990. */
  2991. function is_preview() {
  2992. return (bool) $this->is_preview;
  2993. }
  2994. /**
  2995. * Is the query for the robots file?
  2996. *
  2997. * @since 3.1.0
  2998. *
  2999. * @return bool
  3000. */
  3001. function is_robots() {
  3002. return (bool) $this->is_robots;
  3003. }
  3004. /**
  3005. * Is the query for a search?
  3006. *
  3007. * @since 3.1.0
  3008. *
  3009. * @return bool
  3010. */
  3011. function is_search() {
  3012. return (bool) $this->is_search;
  3013. }
  3014. /**
  3015. * Is the query for an existing single post?
  3016. *
  3017. * Works for any post type, except attachments and pages
  3018. *
  3019. * If the $post parameter is specified, this function will additionally
  3020. * check if the query is for one of the Posts specified.
  3021. *
  3022. * @see WP_Query::is_page()
  3023. * @see WP_Query::is_singular()
  3024. *
  3025. * @since 3.1.0
  3026. *
  3027. * @param mixed $post Post ID, title, slug, or array of such.
  3028. * @return bool
  3029. */
  3030. function is_single( $post = '' ) {
  3031. if ( !$this->is_single )
  3032. return false;
  3033. if ( empty($post) )
  3034. return true;
  3035. $post_obj = $this->get_queried_object();
  3036. $post = (array) $post;
  3037. if ( in_array( $post_obj->ID, $post ) )
  3038. return true;
  3039. elseif ( in_array( $post_obj->post_title, $post ) )
  3040. return true;
  3041. elseif ( in_array( $post_obj->post_name, $post ) )
  3042. return true;
  3043. return false;
  3044. }
  3045. /**
  3046. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  3047. *
  3048. * If the $post_types parameter is specified, this function will additionally
  3049. * check if the query is for one of the Posts Types specified.
  3050. *
  3051. * @see WP_Query::is_page()
  3052. * @see WP_Query::is_single()
  3053. *
  3054. * @since 3.1.0
  3055. *
  3056. * @param mixed $post_types Optional. Post Type or array of Post Types
  3057. * @return bool
  3058. */
  3059. function is_singular( $post_types = '' ) {
  3060. if ( empty( $post_types ) || !$this->is_singular )
  3061. return (bool) $this->is_singular;
  3062. $post_obj = $this->get_queried_object();
  3063. return in_array( $post_obj->post_type, (array) $post_types );
  3064. }
  3065. /**
  3066. * Is the query for a specific time?
  3067. *
  3068. * @since 3.1.0
  3069. *
  3070. * @return bool
  3071. */
  3072. function is_time() {
  3073. return (bool) $this->is_time;
  3074. }
  3075. /**
  3076. * Is the query for a trackback endpoint call?
  3077. *
  3078. * @since 3.1.0
  3079. *
  3080. * @return bool
  3081. */
  3082. function is_trackback() {
  3083. return (bool) $this->is_trackback;
  3084. }
  3085. /**
  3086. * Is the query for an existing year archive?
  3087. *
  3088. * @since 3.1.0
  3089. *
  3090. * @return bool
  3091. */
  3092. function is_year() {
  3093. return (bool) $this->is_year;
  3094. }
  3095. /**
  3096. * Is the query a 404 (returns no results)?
  3097. *
  3098. * @since 3.1.0
  3099. *
  3100. * @return bool
  3101. */
  3102. function is_404() {
  3103. return (bool) $this->is_404;
  3104. }
  3105. /**
  3106. * Is the query the main query?
  3107. *
  3108. * @since 3.3.0
  3109. *
  3110. * @return bool
  3111. */
  3112. function is_main_query() {
  3113. global $wp_the_query;
  3114. return $wp_the_query === $this;
  3115. }
  3116. }
  3117. /**
  3118. * Redirect old slugs to the correct permalink.
  3119. *
  3120. * Attempts to find the current slug from the past slugs.
  3121. *
  3122. * @since 2.1.0
  3123. * @uses $wp_query
  3124. * @uses $wpdb
  3125. *
  3126. * @return null If no link is found, null is returned.
  3127. */
  3128. function wp_old_slug_redirect() {
  3129. global $wp_query;
  3130. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  3131. global $wpdb;
  3132. // Guess the current post_type based on the query vars.
  3133. if ( get_query_var('post_type') )
  3134. $post_type = get_query_var('post_type');
  3135. elseif ( !empty($wp_query->query_vars['pagename']) )
  3136. $post_type = 'page';
  3137. else
  3138. $post_type = 'post';
  3139. if ( is_array( $post_type ) ) {
  3140. if ( count( $post_type ) > 1 )
  3141. return;
  3142. $post_type = array_shift( $post_type );
  3143. }
  3144. // Do not attempt redirect for hierarchical post types
  3145. if ( is_post_type_hierarchical( $post_type ) )
  3146. return;
  3147. $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']);
  3148. // if year, monthnum, or day have been specified, make our query more precise
  3149. // just in case there are multiple identical _wp_old_slug values
  3150. if ( '' != $wp_query->query_vars['year'] )
  3151. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
  3152. if ( '' != $wp_query->query_vars['monthnum'] )
  3153. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
  3154. if ( '' != $wp_query->query_vars['day'] )
  3155. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
  3156. $id = (int) $wpdb->get_var($query);
  3157. if ( ! $id )
  3158. return;
  3159. $link = get_permalink($id);
  3160. if ( !$link )
  3161. return;
  3162. wp_redirect( $link, 301 ); // Permanent redirect
  3163. exit;
  3164. endif;
  3165. }
  3166. /**
  3167. * Set up global post data.
  3168. *
  3169. * @since 1.5.0
  3170. *
  3171. * @param object $post Post data.
  3172. * @uses do_action_ref_array() Calls 'the_post'
  3173. * @return bool True when finished.
  3174. */
  3175. function setup_postdata($post) {
  3176. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  3177. $id = (int) $post->ID;
  3178. $authordata = get_userdata($post->post_author);
  3179. $currentday = mysql2date('d.m.y', $post->post_date, false);
  3180. $currentmonth = mysql2date('m', $post->post_date, false);
  3181. $numpages = 1;
  3182. $page = get_query_var('page');
  3183. if ( !$page )
  3184. $page = 1;
  3185. if ( is_single() || is_page() || is_feed() )
  3186. $more = 1;
  3187. $content = $post->post_content;
  3188. if ( strpos( $content, '<!--nextpage-->' ) ) {
  3189. if ( $page > 1 )
  3190. $more = 1;
  3191. $multipage = 1;
  3192. $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
  3193. $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  3194. $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  3195. $pages = explode('<!--nextpage-->', $content);
  3196. $numpages = count($pages);
  3197. } else {
  3198. $pages = array( $post->post_content );
  3199. $multipage = 0;
  3200. }
  3201. do_action_ref_array('the_post', array(&$post));
  3202. return true;
  3203. }