PageRenderTime 63ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

https://bitbucket.org/chimbien/mekongtest
PHP | 3679 lines | 1854 code | 467 blank | 1358 comment | 546 complexity | d71116bffbeecd103067ba92e2d4fc45 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-1.0
  1. <?php
  2. /**
  3. * WordPress Query API
  4. *
  5. * The query API attempts to get which part of WordPress the user is on. It
  6. * also provides functionality for getting URL query information.
  7. *
  8. * @link http://codex.wordpress.org/The_Loop More information on The Loop.
  9. *
  10. * @package WordPress
  11. * @subpackage Query
  12. */
  13. /**
  14. * Retrieve variable in the WP_Query class.
  15. *
  16. * @see WP_Query::get()
  17. * @since 1.5.0
  18. * @uses $wp_query
  19. *
  20. * @param string $var The variable key to retrieve.
  21. * @return mixed
  22. */
  23. function get_query_var($var) {
  24. global $wp_query;
  25. return $wp_query->get($var);
  26. }
  27. /**
  28. * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
  29. *
  30. * @uses WP_Query::get_queried_object
  31. *
  32. * @since 3.1.0
  33. * @access public
  34. *
  35. * @return object
  36. */
  37. function get_queried_object() {
  38. global $wp_query;
  39. return $wp_query->get_queried_object();
  40. }
  41. /**
  42. * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
  43. *
  44. * @uses WP_Query::get_queried_object_id()
  45. *
  46. * @since 3.1.0
  47. * @access public
  48. *
  49. * @return int
  50. */
  51. function get_queried_object_id() {
  52. global $wp_query;
  53. return $wp_query->get_queried_object_id();
  54. }
  55. /**
  56. * Set query variable.
  57. *
  58. * @see WP_Query::set()
  59. * @since 2.2.0
  60. * @uses $wp_query
  61. *
  62. * @param string $var Query variable key.
  63. * @param mixed $value
  64. * @return null
  65. */
  66. function set_query_var($var, $value) {
  67. global $wp_query;
  68. return $wp_query->set($var, $value);
  69. }
  70. /**
  71. * Set up The Loop with query parameters.
  72. *
  73. * This will override the current WordPress Loop and shouldn't be used more than
  74. * once. This must not be used within the WordPress Loop.
  75. *
  76. * @since 1.5.0
  77. * @uses $wp_query
  78. *
  79. * @param string $query
  80. * @return array List of posts
  81. */
  82. function query_posts($query) {
  83. $GLOBALS['wp_query'] = new WP_Query();
  84. return $GLOBALS['wp_query']->query($query);
  85. }
  86. /**
  87. * Destroy the previous query and set up a new query.
  88. *
  89. * This should be used after {@link query_posts()} and before another {@link
  90. * query_posts()}. This will remove obscure bugs that occur when the previous
  91. * wp_query object is not destroyed properly before another is set up.
  92. *
  93. * @since 2.3.0
  94. * @uses $wp_query
  95. */
  96. function wp_reset_query() {
  97. $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
  98. wp_reset_postdata();
  99. }
  100. /**
  101. * After looping through a separate query, this function restores
  102. * the $post global to the current post in the main query
  103. *
  104. * @since 3.0.0
  105. * @uses $wp_query
  106. */
  107. function wp_reset_postdata() {
  108. global $wp_query;
  109. 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. elseif ( count( $post_type ) == 1 )
  1966. $post_type = $post_type[0];
  1967. $post_status_join = true;
  1968. } elseif ( in_array('attachment', (array) $post_type) ) {
  1969. $post_status_join = true;
  1970. }
  1971. }
  1972. // Back-compat
  1973. if ( !empty($this->tax_query->queries) ) {
  1974. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  1975. if ( !empty( $tax_query_in_and ) ) {
  1976. if ( !isset( $q['taxonomy'] ) ) {
  1977. foreach ( $tax_query_in_and as $a_tax_query ) {
  1978. if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
  1979. $q['taxonomy'] = $a_tax_query['taxonomy'];
  1980. if ( 'slug' == $a_tax_query['field'] )
  1981. $q['term'] = $a_tax_query['terms'][0];
  1982. else
  1983. $q['term_id'] = $a_tax_query['terms'][0];
  1984. break;
  1985. }
  1986. }
  1987. }
  1988. $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
  1989. if ( ! empty( $cat_query ) ) {
  1990. $cat_query = reset( $cat_query );
  1991. if ( ! empty( $cat_query['terms'][0] ) ) {
  1992. $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
  1993. if ( $the_cat ) {
  1994. $this->set( 'cat', $the_cat->term_id );
  1995. $this->set( 'category_name', $the_cat->slug );
  1996. }
  1997. unset( $the_cat );
  1998. }
  1999. }
  2000. unset( $cat_query );
  2001. $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
  2002. if ( ! empty( $tag_query ) ) {
  2003. $tag_query = reset( $tag_query );
  2004. if ( ! empty( $tag_query['terms'][0] ) ) {
  2005. $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
  2006. if ( $the_tag )
  2007. $this->set( 'tag_id', $the_tag->term_id );
  2008. unset( $the_tag );
  2009. }
  2010. }
  2011. unset( $tag_query );
  2012. }
  2013. }
  2014. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
  2015. $groupby = "{$wpdb->posts}.ID";
  2016. }
  2017. // Author/user stuff
  2018. if ( empty($q['author']) || ($q['author'] == '0') ) {
  2019. $whichauthor = '';
  2020. } else {
  2021. $q['author'] = (string)urldecode($q['author']);
  2022. $q['author'] = addslashes_gpc($q['author']);
  2023. if ( strpos($q['author'], '-') !== false ) {
  2024. $eq = '!=';
  2025. $andor = 'AND';
  2026. $q['author'] = explode('-', $q['author']);
  2027. $q['author'] = (string)absint($q['author'][1]);
  2028. } else {
  2029. $eq = '=';
  2030. $andor = 'OR';
  2031. }
  2032. $author_array = preg_split('/[,\s]+/', $q['author']);
  2033. $_author_array = array();
  2034. foreach ( $author_array as $key => $_author )
  2035. $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
  2036. $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
  2037. unset($author_array, $_author_array);
  2038. }
  2039. // Author stuff for nice URLs
  2040. if ( '' != $q['author_name'] ) {
  2041. if ( strpos($q['author_name'], '/') !== false ) {
  2042. $q['author_name'] = explode('/', $q['author_name']);
  2043. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  2044. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  2045. } else {
  2046. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
  2047. }
  2048. }
  2049. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  2050. $q['author'] = get_user_by('slug', $q['author_name']);
  2051. if ( $q['author'] )
  2052. $q['author'] = $q['author']->ID;
  2053. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  2054. }
  2055. // MIME-Type stuff for attachment browsing
  2056. if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
  2057. $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
  2058. $where .= $search . $whichauthor . $whichmimetype;
  2059. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  2060. $q['order'] = 'DESC';
  2061. // Order by
  2062. if ( empty($q['orderby']) ) {
  2063. $orderby = "$wpdb->posts.post_date " . $q['order'];
  2064. } elseif ( 'none' == $q['orderby'] ) {
  2065. $orderby = '';
  2066. } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
  2067. $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
  2068. } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
  2069. $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
  2070. } else {
  2071. // Used to filter values
  2072. $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2073. if ( !empty($q['meta_key']) ) {
  2074. $allowed_keys[] = $q['meta_key'];
  2075. $allowed_keys[] = 'meta_value';
  2076. $allowed_keys[] = 'meta_value_num';
  2077. }
  2078. $q['orderby'] = urldecode($q['orderby']);
  2079. $q['orderby'] = addslashes_gpc($q['orderby']);
  2080. $orderby_array = array();
  2081. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  2082. // Only allow certain values for safety
  2083. if ( ! in_array($orderby, $allowed_keys) )
  2084. continue;
  2085. switch ( $orderby ) {
  2086. case 'menu_order':
  2087. $orderby = "$wpdb->posts.menu_order";
  2088. break;
  2089. case 'ID':
  2090. $orderby = "$wpdb->posts.ID";
  2091. break;
  2092. case 'rand':
  2093. $orderby = 'RAND()';
  2094. break;
  2095. case $q['meta_key']:
  2096. case 'meta_value':
  2097. $orderby = "$wpdb->postmeta.meta_value";
  2098. break;
  2099. case 'meta_value_num':
  2100. $orderby = "$wpdb->postmeta.meta_value+0";
  2101. break;
  2102. case 'comment_count':
  2103. $orderby = "$wpdb->posts.comment_count";
  2104. break;
  2105. default:
  2106. $orderby = "$wpdb->posts.post_" . $orderby;
  2107. }
  2108. $orderby_array[] = $orderby;
  2109. }
  2110. $orderby = implode( ',', $orderby_array );
  2111. if ( empty( $orderby ) )
  2112. $orderby = "$wpdb->posts.post_date ".$q['order'];
  2113. else
  2114. $orderby .= " {$q['order']}";
  2115. }
  2116. if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
  2117. $post_type_cap = 'multiple_post_type';
  2118. } else {
  2119. if ( is_array( $post_type ) )
  2120. $post_type = reset( $post_type );
  2121. $post_type_object = get_post_type_object( $post_type );
  2122. if ( empty( $post_type_object ) )
  2123. $post_type_cap = $post_type;
  2124. }
  2125. if ( 'any' == $post_type ) {
  2126. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  2127. if ( ! empty( $in_search_post_types ) )
  2128. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
  2129. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2130. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2131. } elseif ( ! empty( $post_type ) ) {
  2132. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  2133. $post_type_object = get_post_type_object ( $post_type );
  2134. } elseif ( $this->is_attachment ) {
  2135. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  2136. $post_type_object = get_post_type_object ( 'attachment' );
  2137. } elseif ( $this->is_page ) {
  2138. $where .= " AND $wpdb->posts.post_type = 'page'";
  2139. $post_type_object = get_post_type_object ( 'page' );
  2140. } else {
  2141. $where .= " AND $wpdb->posts.post_type = 'post'";
  2142. $post_type_object = get_post_type_object ( 'post' );
  2143. }
  2144. $edit_cap = 'edit_post';
  2145. $read_cap = 'read_post';
  2146. if ( ! empty( $post_type_object ) ) {
  2147. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  2148. $read_private_cap = $post_type_object->cap->read_private_posts;
  2149. } else {
  2150. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  2151. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  2152. }
  2153. if ( ! empty( $q['post_status'] ) ) {
  2154. $statuswheres = array();
  2155. $q_status = $q['post_status'];
  2156. if ( ! is_array( $q_status ) )
  2157. $q_status = explode(',', $q_status);
  2158. $r_status = array();
  2159. $p_status = array();
  2160. $e_status = array();
  2161. if ( in_array('any', $q_status) ) {
  2162. foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  2163. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  2164. } else {
  2165. foreach ( get_post_stati() as $status ) {
  2166. if ( in_array( $status, $q_status ) ) {
  2167. if ( 'private' == $status )
  2168. $p_status[] = "$wpdb->posts.post_status = '$status'";
  2169. else
  2170. $r_status[] = "$wpdb->posts.post_status = '$status'";
  2171. }
  2172. }
  2173. }
  2174. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2175. $r_status = array_merge($r_status, $p_status);
  2176. unset($p_status);
  2177. }
  2178. if ( !empty($e_status) ) {
  2179. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2180. }
  2181. if ( !empty($r_status) ) {
  2182. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2183. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
  2184. else
  2185. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2186. }
  2187. if ( !empty($p_status) ) {
  2188. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2189. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
  2190. else
  2191. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2192. }
  2193. if ( $post_status_join ) {
  2194. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2195. foreach ( $statuswheres as $index => $statuswhere )
  2196. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  2197. }
  2198. foreach ( $statuswheres as $statuswhere )
  2199. $where .= " AND $statuswhere";
  2200. } elseif ( !$this->is_singular ) {
  2201. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  2202. // Add public states.
  2203. $public_states = get_post_stati( array('public' => true) );
  2204. foreach ( (array) $public_states as $state ) {
  2205. if ( 'publish' == $state ) // Publish is hard-coded above.
  2206. continue;
  2207. $where .= " OR $wpdb->posts.post_status = '$state'";
  2208. }
  2209. if ( $this->is_admin ) {
  2210. // Add protected states that should show in the admin all list.
  2211. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2212. foreach ( (array) $admin_all_states as $state )
  2213. $where .= " OR $wpdb->posts.post_status = '$state'";
  2214. }
  2215. if ( is_user_logged_in() ) {
  2216. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2217. $private_states = get_post_stati( array('private' => true) );
  2218. foreach ( (array) $private_states as $state )
  2219. $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'";
  2220. }
  2221. $where .= ')';
  2222. }
  2223. if ( !empty( $this->meta_query->queries ) ) {
  2224. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  2225. $join .= $clauses['join'];
  2226. $where .= $clauses['where'];
  2227. }
  2228. // Apply filters on where and join prior to paging so that any
  2229. // manipulations to them are reflected in the paging by day queries.
  2230. if ( !$q['suppress_filters'] ) {
  2231. $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
  2232. $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
  2233. }
  2234. // Paging
  2235. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2236. $page = absint($q['paged']);
  2237. if ( !$page )
  2238. $page = 1;
  2239. if ( empty($q['offset']) ) {
  2240. $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2241. } else { // we're ignoring $page and using 'offset'
  2242. $q['offset'] = absint($q['offset']);
  2243. $pgstrt = $q['offset'] . ', ';
  2244. }
  2245. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2246. }
  2247. // Comments feeds
  2248. if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2249. if ( $this->is_archive || $this->is_search ) {
  2250. $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  2251. $cwhere = "WHERE comment_approved = '1' $where";
  2252. $cgroupby = "$wpdb->comments.comment_id";
  2253. } else { // Other non singular e.g. front
  2254. $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  2255. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2256. $cgroupby = '';
  2257. }
  2258. if ( !$q['suppress_filters'] ) {
  2259. $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
  2260. $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
  2261. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
  2262. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2263. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2264. }
  2265. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2266. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2267. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2268. $this->comment_count = count($this->comments);
  2269. $post_ids = array();
  2270. foreach ( $this->comments as $comment )
  2271. $post_ids[] = (int) $comment->comment_post_ID;
  2272. $post_ids = join(',', $post_ids);
  2273. $join = '';
  2274. if ( $post_ids )
  2275. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2276. else
  2277. $where = "AND 0";
  2278. }
  2279. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2280. // Apply post-paging filters on where and join. Only plugins that
  2281. // manipulate paging queries should use these hooks.
  2282. if ( !$q['suppress_filters'] ) {
  2283. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2284. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2285. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2286. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2287. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2288. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2289. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2290. // Filter all clauses at once, for convenience
  2291. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2292. foreach ( $pieces as $piece )
  2293. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2294. }
  2295. // Announce current selection parameters. For use by caching plugins.
  2296. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2297. // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
  2298. if ( !$q['suppress_filters'] ) {
  2299. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2300. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2301. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2302. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2303. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2304. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2305. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2306. // Filter all clauses at once, for convenience
  2307. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
  2308. foreach ( $pieces as $piece )
  2309. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2310. }
  2311. if ( ! empty($groupby) )
  2312. $groupby = 'GROUP BY ' . $groupby;
  2313. if ( !empty( $orderby ) )
  2314. $orderby = 'ORDER BY ' . $orderby;
  2315. $found_rows = '';
  2316. if ( !$q['no_found_rows'] && !empty($limits) )
  2317. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2318. $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2319. if ( !$q['suppress_filters'] ) {
  2320. $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
  2321. }
  2322. if ( 'ids' == $q['fields'] ) {
  2323. $this->posts = $wpdb->get_col( $this->request );
  2324. $this->post_count = count( $this->posts );
  2325. $this->set_found_posts( $q, $limits );
  2326. return $this->posts;
  2327. }
  2328. if ( 'id=>parent' == $q['fields'] ) {
  2329. $this->posts = $wpdb->get_results( $this->request );
  2330. $this->post_count = count( $this->posts );
  2331. $this->set_found_posts( $q, $limits );
  2332. $r = array();
  2333. foreach ( $this->posts as $post )
  2334. $r[ $post->ID ] = $post->post_parent;
  2335. return $r;
  2336. }
  2337. $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
  2338. $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
  2339. if ( $split_the_query ) {
  2340. // First get the IDs and then fill in the objects
  2341. $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2342. $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
  2343. $ids = $wpdb->get_col( $this->request );
  2344. if ( $ids ) {
  2345. $this->posts = $ids;
  2346. $this->set_found_posts( $q, $limits );
  2347. _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
  2348. } else {
  2349. $this->posts = array();
  2350. }
  2351. } else {
  2352. $this->posts = $wpdb->get_results( $this->request );
  2353. $this->set_found_posts( $q, $limits );
  2354. }
  2355. // Convert to WP_Post objects
  2356. if ( $this->posts )
  2357. $this->posts = array_map( 'get_post', $this->posts );
  2358. // Raw results filter. Prior to status checks.
  2359. if ( !$q['suppress_filters'] )
  2360. $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  2361. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2362. $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
  2363. $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2364. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
  2365. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2366. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2367. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2368. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2369. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2370. $this->comments = $wpdb->get_results($comments_request);
  2371. $this->comment_count = count($this->comments);
  2372. }
  2373. // Check post status to determine if post should be displayed.
  2374. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2375. $status = get_post_status($this->posts[0]);
  2376. $post_status_obj = get_post_status_object($status);
  2377. //$type = get_post_type($this->posts[0]);
  2378. if ( !$post_status_obj->public ) {
  2379. if ( ! is_user_logged_in() ) {
  2380. // User must be logged in to view unpublished posts.
  2381. $this->posts = array();
  2382. } else {
  2383. if ( $post_status_obj->protected ) {
  2384. // User must have edit permissions on the draft to preview.
  2385. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2386. $this->posts = array();
  2387. } else {
  2388. $this->is_preview = true;
  2389. if ( 'future' != $status )
  2390. $this->posts[0]->post_date = current_time('mysql');
  2391. }
  2392. } elseif ( $post_status_obj->private ) {
  2393. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2394. $this->posts = array();
  2395. } else {
  2396. $this->posts = array();
  2397. }
  2398. }
  2399. }
  2400. if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
  2401. $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
  2402. }
  2403. // Put sticky posts at the top of the posts array
  2404. $sticky_posts = get_option('sticky_posts');
  2405. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  2406. $num_posts = count($this->posts);
  2407. $sticky_offset = 0;
  2408. // Loop over posts and relocate stickies to the front.
  2409. for ( $i = 0; $i < $num_posts; $i++ ) {
  2410. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2411. $sticky_post = $this->posts[$i];
  2412. // Remove sticky from current position
  2413. array_splice($this->posts, $i, 1);
  2414. // Move to front, after other stickies
  2415. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2416. // Increment the sticky offset. The next sticky will be placed at this offset.
  2417. $sticky_offset++;
  2418. // Remove post from sticky posts array
  2419. $offset = array_search($sticky_post->ID, $sticky_posts);
  2420. unset( $sticky_posts[$offset] );
  2421. }
  2422. }
  2423. // If any posts have been excluded specifically, Ignore those that are sticky.
  2424. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  2425. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2426. // Fetch sticky posts that weren't in the query results
  2427. if ( !empty($sticky_posts) ) {
  2428. $stickies = get_posts( array(
  2429. 'post__in' => $sticky_posts,
  2430. 'post_type' => $post_type,
  2431. 'post_status' => 'publish',
  2432. 'nopaging' => true
  2433. ) );
  2434. foreach ( $stickies as $sticky_post ) {
  2435. array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
  2436. $sticky_offset++;
  2437. }
  2438. }
  2439. }
  2440. if ( !$q['suppress_filters'] )
  2441. $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
  2442. // Ensure that any posts added/modified via one of the filters above are
  2443. // of the type WP_Post and are filtered.
  2444. if ( $this->posts ) {
  2445. $this->post_count = count( $this->posts );
  2446. $this->posts = array_map( 'get_post', $this->posts );
  2447. if ( $q['cache_results'] )
  2448. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2449. $this->post = reset( $this->posts );
  2450. } else {
  2451. $this->post_count = 0;
  2452. $this->posts = array();
  2453. }
  2454. return $this->posts;
  2455. }
  2456. /**
  2457. * Set up the amount of found posts and the number of pages (if limit clause was used)
  2458. * for the current query.
  2459. *
  2460. * @since 3.5.0
  2461. * @access private
  2462. */
  2463. function set_found_posts( $q, $limits ) {
  2464. global $wpdb;
  2465. // Bail if posts is an empty array. Continue if posts is an empty string,
  2466. // null, or false to accommodate caching plugins that fill posts later.
  2467. if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
  2468. return;
  2469. if ( ! empty( $limits ) )
  2470. $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
  2471. else
  2472. $this->found_posts = count( $this->posts );
  2473. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  2474. if ( ! empty( $limits ) )
  2475. $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
  2476. }
  2477. /**
  2478. * Set up the next post and iterate current post index.
  2479. *
  2480. * @since 1.5.0
  2481. * @access public
  2482. *
  2483. * @return WP_Post Next post.
  2484. */
  2485. function next_post() {
  2486. $this->current_post++;
  2487. $this->post = $this->posts[$this->current_post];
  2488. return $this->post;
  2489. }
  2490. /**
  2491. * Sets up the current post.
  2492. *
  2493. * Retrieves the next post, sets up the post, sets the 'in the loop'
  2494. * property to true.
  2495. *
  2496. * @since 1.5.0
  2497. * @access public
  2498. * @uses $post
  2499. * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
  2500. */
  2501. function the_post() {
  2502. global $post;
  2503. $this->in_the_loop = true;
  2504. if ( $this->current_post == -1 ) // loop has just started
  2505. do_action_ref_array('loop_start', array(&$this));
  2506. $post = $this->next_post();
  2507. setup_postdata($post);
  2508. }
  2509. /**
  2510. * Whether there are more posts available in the loop.
  2511. *
  2512. * Calls action 'loop_end', when the loop is complete.
  2513. *
  2514. * @since 1.5.0
  2515. * @access public
  2516. * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  2517. *
  2518. * @return bool True if posts are available, false if end of loop.
  2519. */
  2520. function have_posts() {
  2521. if ( $this->current_post + 1 < $this->post_count ) {
  2522. return true;
  2523. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  2524. do_action_ref_array('loop_end', array(&$this));
  2525. // Do some cleaning up after the loop
  2526. $this->rewind_posts();
  2527. }
  2528. $this->in_the_loop = false;
  2529. return false;
  2530. }
  2531. /**
  2532. * Rewind the posts and reset post index.
  2533. *
  2534. * @since 1.5.0
  2535. * @access public
  2536. */
  2537. function rewind_posts() {
  2538. $this->current_post = -1;
  2539. if ( $this->post_count > 0 ) {
  2540. $this->post = $this->posts[0];
  2541. }
  2542. }
  2543. /**
  2544. * Iterate current comment index and return comment object.
  2545. *
  2546. * @since 2.2.0
  2547. * @access public
  2548. *
  2549. * @return object Comment object.
  2550. */
  2551. function next_comment() {
  2552. $this->current_comment++;
  2553. $this->comment = $this->comments[$this->current_comment];
  2554. return $this->comment;
  2555. }
  2556. /**
  2557. * Sets up the current comment.
  2558. *
  2559. * @since 2.2.0
  2560. * @access public
  2561. * @global object $comment Current comment.
  2562. * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
  2563. */
  2564. function the_comment() {
  2565. global $comment;
  2566. $comment = $this->next_comment();
  2567. if ( $this->current_comment == 0 ) {
  2568. do_action('comment_loop_start');
  2569. }
  2570. }
  2571. /**
  2572. * Whether there are more comments available.
  2573. *
  2574. * Automatically rewinds comments when finished.
  2575. *
  2576. * @since 2.2.0
  2577. * @access public
  2578. *
  2579. * @return bool True, if more comments. False, if no more posts.
  2580. */
  2581. function have_comments() {
  2582. if ( $this->current_comment + 1 < $this->comment_count ) {
  2583. return true;
  2584. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  2585. $this->rewind_comments();
  2586. }
  2587. return false;
  2588. }
  2589. /**
  2590. * Rewind the comments, resets the comment index and comment to first.
  2591. *
  2592. * @since 2.2.0
  2593. * @access public
  2594. */
  2595. function rewind_comments() {
  2596. $this->current_comment = -1;
  2597. if ( $this->comment_count > 0 ) {
  2598. $this->comment = $this->comments[0];
  2599. }
  2600. }
  2601. /**
  2602. * Sets up the WordPress query by parsing query string.
  2603. *
  2604. * @since 1.5.0
  2605. * @access public
  2606. *
  2607. * @param string $query URL query string.
  2608. * @return array List of posts.
  2609. */
  2610. function query( $query ) {
  2611. $this->init();
  2612. $this->query = $this->query_vars = wp_parse_args( $query );
  2613. return $this->get_posts();
  2614. }
  2615. /**
  2616. * Retrieve queried object.
  2617. *
  2618. * If queried object is not set, then the queried object will be set from
  2619. * the category, tag, taxonomy, posts page, single post, page, or author
  2620. * query variable. After it is set up, it will be returned.
  2621. *
  2622. * @since 1.5.0
  2623. * @access public
  2624. *
  2625. * @return object
  2626. */
  2627. function get_queried_object() {
  2628. if ( isset($this->queried_object) )
  2629. return $this->queried_object;
  2630. $this->queried_object = null;
  2631. $this->queried_object_id = 0;
  2632. if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  2633. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  2634. $query = reset( $tax_query_in_and );
  2635. if ( 'term_id' == $query['field'] )
  2636. $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
  2637. elseif ( $query['terms'] )
  2638. $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
  2639. if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
  2640. $this->queried_object = $term;
  2641. $this->queried_object_id = (int) $term->term_id;
  2642. if ( $this->is_category )
  2643. _make_cat_compat( $this->queried_object );
  2644. }
  2645. } elseif ( $this->is_post_type_archive ) {
  2646. $this->queried_object = get_post_type_object( $this->get('post_type') );
  2647. } elseif ( $this->is_posts_page ) {
  2648. $page_for_posts = get_option('page_for_posts');
  2649. $this->queried_object = get_post( $page_for_posts );
  2650. $this->queried_object_id = (int) $this->queried_object->ID;
  2651. } elseif ( $this->is_singular && !is_null($this->post) ) {
  2652. $this->queried_object = $this->post;
  2653. $this->queried_object_id = (int) $this->post->ID;
  2654. } elseif ( $this->is_author ) {
  2655. $this->queried_object_id = (int) $this->get('author');
  2656. $this->queried_object = get_userdata( $this->queried_object_id );
  2657. }
  2658. return $this->queried_object;
  2659. }
  2660. /**
  2661. * Retrieve ID of the current queried object.
  2662. *
  2663. * @since 1.5.0
  2664. * @access public
  2665. *
  2666. * @return int
  2667. */
  2668. function get_queried_object_id() {
  2669. $this->get_queried_object();
  2670. if ( isset($this->queried_object_id) ) {
  2671. return $this->queried_object_id;
  2672. }
  2673. return 0;
  2674. }
  2675. /**
  2676. * Constructor.
  2677. *
  2678. * Sets up the WordPress query, if parameter is not empty.
  2679. *
  2680. * @since 1.5.0
  2681. * @access public
  2682. *
  2683. * @param string $query URL query string.
  2684. * @return WP_Query
  2685. */
  2686. function __construct($query = '') {
  2687. if ( ! empty($query) ) {
  2688. $this->query($query);
  2689. }
  2690. }
  2691. /**
  2692. * Is the query for an existing archive page?
  2693. *
  2694. * Month, Year, Category, Author, Post Type archive...
  2695. *
  2696. * @since 3.1.0
  2697. *
  2698. * @return bool
  2699. */
  2700. function is_archive() {
  2701. return (bool) $this->is_archive;
  2702. }
  2703. /**
  2704. * Is the query for an existing post type archive page?
  2705. *
  2706. * @since 3.1.0
  2707. *
  2708. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  2709. * @return bool
  2710. */
  2711. function is_post_type_archive( $post_types = '' ) {
  2712. if ( empty( $post_types ) || !$this->is_post_type_archive )
  2713. return (bool) $this->is_post_type_archive;
  2714. $post_type_object = $this->get_queried_object();
  2715. return in_array( $post_type_object->name, (array) $post_types );
  2716. }
  2717. /**
  2718. * Is the query for an existing attachment page?
  2719. *
  2720. * @since 3.1.0
  2721. *
  2722. * @return bool
  2723. */
  2724. function is_attachment() {
  2725. return (bool) $this->is_attachment;
  2726. }
  2727. /**
  2728. * Is the query for an existing author archive page?
  2729. *
  2730. * If the $author parameter is specified, this function will additionally
  2731. * check if the query is for one of the authors specified.
  2732. *
  2733. * @since 3.1.0
  2734. *
  2735. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  2736. * @return bool
  2737. */
  2738. function is_author( $author = '' ) {
  2739. if ( !$this->is_author )
  2740. return false;
  2741. if ( empty($author) )
  2742. return true;
  2743. $author_obj = $this->get_queried_object();
  2744. $author = (array) $author;
  2745. if ( in_array( $author_obj->ID, $author ) )
  2746. return true;
  2747. elseif ( in_array( $author_obj->nickname, $author ) )
  2748. return true;
  2749. elseif ( in_array( $author_obj->user_nicename, $author ) )
  2750. return true;
  2751. return false;
  2752. }
  2753. /**
  2754. * Is the query for an existing category archive page?
  2755. *
  2756. * If the $category parameter is specified, this function will additionally
  2757. * check if the query is for one of the categories specified.
  2758. *
  2759. * @since 3.1.0
  2760. *
  2761. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  2762. * @return bool
  2763. */
  2764. function is_category( $category = '' ) {
  2765. if ( !$this->is_category )
  2766. return false;
  2767. if ( empty($category) )
  2768. return true;
  2769. $cat_obj = $this->get_queried_object();
  2770. $category = (array) $category;
  2771. if ( in_array( $cat_obj->term_id, $category ) )
  2772. return true;
  2773. elseif ( in_array( $cat_obj->name, $category ) )
  2774. return true;
  2775. elseif ( in_array( $cat_obj->slug, $category ) )
  2776. return true;
  2777. return false;
  2778. }
  2779. /**
  2780. * Is the query for an existing tag archive page?
  2781. *
  2782. * If the $tag parameter is specified, this function will additionally
  2783. * check if the query is for one of the tags specified.
  2784. *
  2785. * @since 3.1.0
  2786. *
  2787. * @param mixed $slug Optional. Tag slug or array of slugs.
  2788. * @return bool
  2789. */
  2790. function is_tag( $slug = '' ) {
  2791. if ( !$this->is_tag )
  2792. return false;
  2793. if ( empty( $slug ) )
  2794. return true;
  2795. $tag_obj = $this->get_queried_object();
  2796. $slug = (array) $slug;
  2797. if ( in_array( $tag_obj->slug, $slug ) )
  2798. return true;
  2799. return false;
  2800. }
  2801. /**
  2802. * Is the query for an existing taxonomy archive page?
  2803. *
  2804. * If the $taxonomy parameter is specified, this function will additionally
  2805. * check if the query is for that specific $taxonomy.
  2806. *
  2807. * If the $term parameter is specified in addition to the $taxonomy parameter,
  2808. * this function will additionally check if the query is for one of the terms
  2809. * specified.
  2810. *
  2811. * @since 3.1.0
  2812. *
  2813. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  2814. * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  2815. * @return bool
  2816. */
  2817. function is_tax( $taxonomy = '', $term = '' ) {
  2818. global $wp_taxonomies;
  2819. if ( !$this->is_tax )
  2820. return false;
  2821. if ( empty( $taxonomy ) )
  2822. return true;
  2823. $queried_object = $this->get_queried_object();
  2824. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
  2825. $term_array = (array) $term;
  2826. // Check that the taxonomy matches.
  2827. if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
  2828. return false;
  2829. // Only a Taxonomy provided.
  2830. if ( empty( $term ) )
  2831. return true;
  2832. return isset( $queried_object->term_id ) &&
  2833. count( array_intersect(
  2834. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
  2835. $term_array
  2836. ) );
  2837. }
  2838. /**
  2839. * Whether the current URL is within the comments popup window.
  2840. *
  2841. * @since 3.1.0
  2842. *
  2843. * @return bool
  2844. */
  2845. function is_comments_popup() {
  2846. return (bool) $this->is_comments_popup;
  2847. }
  2848. /**
  2849. * Is the query for an existing date archive?
  2850. *
  2851. * @since 3.1.0
  2852. *
  2853. * @return bool
  2854. */
  2855. function is_date() {
  2856. return (bool) $this->is_date;
  2857. }
  2858. /**
  2859. * Is the query for an existing day archive?
  2860. *
  2861. * @since 3.1.0
  2862. *
  2863. * @return bool
  2864. */
  2865. function is_day() {
  2866. return (bool) $this->is_day;
  2867. }
  2868. /**
  2869. * Is the query for a feed?
  2870. *
  2871. * @since 3.1.0
  2872. *
  2873. * @param string|array $feeds Optional feed types to check.
  2874. * @return bool
  2875. */
  2876. function is_feed( $feeds = '' ) {
  2877. if ( empty( $feeds ) || ! $this->is_feed )
  2878. return (bool) $this->is_feed;
  2879. $qv = $this->get( 'feed' );
  2880. if ( 'feed' == $qv )
  2881. $qv = get_default_feed();
  2882. return in_array( $qv, (array) $feeds );
  2883. }
  2884. /**
  2885. * Is the query for a comments feed?
  2886. *
  2887. * @since 3.1.0
  2888. *
  2889. * @return bool
  2890. */
  2891. function is_comment_feed() {
  2892. return (bool) $this->is_comment_feed;
  2893. }
  2894. /**
  2895. * Is the query for the front page of the site?
  2896. *
  2897. * This is for what is displayed at your site's main URL.
  2898. *
  2899. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  2900. *
  2901. * If you set a static page for the front page of your site, this function will return
  2902. * true when viewing that page.
  2903. *
  2904. * Otherwise the same as @see WP_Query::is_home()
  2905. *
  2906. * @since 3.1.0
  2907. * @uses is_home()
  2908. * @uses get_option()
  2909. *
  2910. * @return bool True, if front of site.
  2911. */
  2912. function is_front_page() {
  2913. // most likely case
  2914. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
  2915. return true;
  2916. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
  2917. return true;
  2918. else
  2919. return false;
  2920. }
  2921. /**
  2922. * Is the query for the blog homepage?
  2923. *
  2924. * This is the page which shows the time based blog content of your site.
  2925. *
  2926. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  2927. *
  2928. * If you set a static page for the front page of your site, this function will return
  2929. * true only on the page you set as the "Posts page".
  2930. *
  2931. * @see WP_Query::is_front_page()
  2932. *
  2933. * @since 3.1.0
  2934. *
  2935. * @return bool True if blog view homepage.
  2936. */
  2937. function is_home() {
  2938. return (bool) $this->is_home;
  2939. }
  2940. /**
  2941. * Is the query for an existing month archive?
  2942. *
  2943. * @since 3.1.0
  2944. *
  2945. * @return bool
  2946. */
  2947. function is_month() {
  2948. return (bool) $this->is_month;
  2949. }
  2950. /**
  2951. * Is the query for an existing single page?
  2952. *
  2953. * If the $page parameter is specified, this function will additionally
  2954. * check if the query is for one of the pages specified.
  2955. *
  2956. * @see WP_Query::is_single()
  2957. * @see WP_Query::is_singular()
  2958. *
  2959. * @since 3.1.0
  2960. *
  2961. * @param mixed $page Page ID, title, slug, or array of such.
  2962. * @return bool
  2963. */
  2964. function is_page( $page = '' ) {
  2965. if ( !$this->is_page )
  2966. return false;
  2967. if ( empty( $page ) )
  2968. return true;
  2969. $page_obj = $this->get_queried_object();
  2970. $page = (array) $page;
  2971. if ( in_array( $page_obj->ID, $page ) )
  2972. return true;
  2973. elseif ( in_array( $page_obj->post_title, $page ) )
  2974. return true;
  2975. else if ( in_array( $page_obj->post_name, $page ) )
  2976. return true;
  2977. return false;
  2978. }
  2979. /**
  2980. * Is the query for paged result and not for the first page?
  2981. *
  2982. * @since 3.1.0
  2983. *
  2984. * @return bool
  2985. */
  2986. function is_paged() {
  2987. return (bool) $this->is_paged;
  2988. }
  2989. /**
  2990. * Is the query for a post or page preview?
  2991. *
  2992. * @since 3.1.0
  2993. *
  2994. * @return bool
  2995. */
  2996. function is_preview() {
  2997. return (bool) $this->is_preview;
  2998. }
  2999. /**
  3000. * Is the query for the robots file?
  3001. *
  3002. * @since 3.1.0
  3003. *
  3004. * @return bool
  3005. */
  3006. function is_robots() {
  3007. return (bool) $this->is_robots;
  3008. }
  3009. /**
  3010. * Is the query for a search?
  3011. *
  3012. * @since 3.1.0
  3013. *
  3014. * @return bool
  3015. */
  3016. function is_search() {
  3017. return (bool) $this->is_search;
  3018. }
  3019. /**
  3020. * Is the query for an existing single post?
  3021. *
  3022. * Works for any post type, except attachments and pages
  3023. *
  3024. * If the $post parameter is specified, this function will additionally
  3025. * check if the query is for one of the Posts specified.
  3026. *
  3027. * @see WP_Query::is_page()
  3028. * @see WP_Query::is_singular()
  3029. *
  3030. * @since 3.1.0
  3031. *
  3032. * @param mixed $post Post ID, title, slug, or array of such.
  3033. * @return bool
  3034. */
  3035. function is_single( $post = '' ) {
  3036. if ( !$this->is_single )
  3037. return false;
  3038. if ( empty($post) )
  3039. return true;
  3040. $post_obj = $this->get_queried_object();
  3041. $post = (array) $post;
  3042. if ( in_array( $post_obj->ID, $post ) )
  3043. return true;
  3044. elseif ( in_array( $post_obj->post_title, $post ) )
  3045. return true;
  3046. elseif ( in_array( $post_obj->post_name, $post ) )
  3047. return true;
  3048. return false;
  3049. }
  3050. /**
  3051. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  3052. *
  3053. * If the $post_types parameter is specified, this function will additionally
  3054. * check if the query is for one of the Posts Types specified.
  3055. *
  3056. * @see WP_Query::is_page()
  3057. * @see WP_Query::is_single()
  3058. *
  3059. * @since 3.1.0
  3060. *
  3061. * @param mixed $post_types Optional. Post Type or array of Post Types
  3062. * @return bool
  3063. */
  3064. function is_singular( $post_types = '' ) {
  3065. if ( empty( $post_types ) || !$this->is_singular )
  3066. return (bool) $this->is_singular;
  3067. $post_obj = $this->get_queried_object();
  3068. return in_array( $post_obj->post_type, (array) $post_types );
  3069. }
  3070. /**
  3071. * Is the query for a specific time?
  3072. *
  3073. * @since 3.1.0
  3074. *
  3075. * @return bool
  3076. */
  3077. function is_time() {
  3078. return (bool) $this->is_time;
  3079. }
  3080. /**
  3081. * Is the query for a trackback endpoint call?
  3082. *
  3083. * @since 3.1.0
  3084. *
  3085. * @return bool
  3086. */
  3087. function is_trackback() {
  3088. return (bool) $this->is_trackback;
  3089. }
  3090. /**
  3091. * Is the query for an existing year archive?
  3092. *
  3093. * @since 3.1.0
  3094. *
  3095. * @return bool
  3096. */
  3097. function is_year() {
  3098. return (bool) $this->is_year;
  3099. }
  3100. /**
  3101. * Is the query a 404 (returns no results)?
  3102. *
  3103. * @since 3.1.0
  3104. *
  3105. * @return bool
  3106. */
  3107. function is_404() {
  3108. return (bool) $this->is_404;
  3109. }
  3110. /**
  3111. * Is the query the main query?
  3112. *
  3113. * @since 3.3.0
  3114. *
  3115. * @return bool
  3116. */
  3117. function is_main_query() {
  3118. global $wp_the_query;
  3119. return $wp_the_query === $this;
  3120. }
  3121. }
  3122. /**
  3123. * Redirect old slugs to the correct permalink.
  3124. *
  3125. * Attempts to find the current slug from the past slugs.
  3126. *
  3127. * @since 2.1.0
  3128. * @uses $wp_query
  3129. * @uses $wpdb
  3130. *
  3131. * @return null If no link is found, null is returned.
  3132. */
  3133. function wp_old_slug_redirect() {
  3134. global $wp_query;
  3135. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  3136. global $wpdb;
  3137. // Guess the current post_type based on the query vars.
  3138. if ( get_query_var('post_type') )
  3139. $post_type = get_query_var('post_type');
  3140. elseif ( !empty($wp_query->query_vars['pagename']) )
  3141. $post_type = 'page';
  3142. else
  3143. $post_type = 'post';
  3144. if ( is_array( $post_type ) ) {
  3145. if ( count( $post_type ) > 1 )
  3146. return;
  3147. $post_type = array_shift( $post_type );
  3148. }
  3149. // Do not attempt redirect for hierarchical post types
  3150. if ( is_post_type_hierarchical( $post_type ) )
  3151. return;
  3152. $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']);
  3153. // if year, monthnum, or day have been specified, make our query more precise
  3154. // just in case there are multiple identical _wp_old_slug values
  3155. if ( '' != $wp_query->query_vars['year'] )
  3156. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
  3157. if ( '' != $wp_query->query_vars['monthnum'] )
  3158. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
  3159. if ( '' != $wp_query->query_vars['day'] )
  3160. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
  3161. $id = (int) $wpdb->get_var($query);
  3162. if ( ! $id )
  3163. return;
  3164. $link = get_permalink($id);
  3165. if ( !$link )
  3166. return;
  3167. wp_redirect( $link, 301 ); // Permanent redirect
  3168. exit;
  3169. endif;
  3170. }
  3171. /**
  3172. * Set up global post data.
  3173. *
  3174. * @since 1.5.0
  3175. *
  3176. * @param object $post Post data.
  3177. * @uses do_action_ref_array() Calls 'the_post'
  3178. * @return bool True when finished.
  3179. */
  3180. function setup_postdata( $post ) {
  3181. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  3182. $id = (int) $post->ID;
  3183. $authordata = get_userdata($post->post_author);
  3184. $currentday = mysql2date('d.m.y', $post->post_date, false);
  3185. $currentmonth = mysql2date('m', $post->post_date, false);
  3186. $numpages = 1;
  3187. $multipage = 0;
  3188. $page = get_query_var('page');
  3189. if ( ! $page )
  3190. $page = 1;
  3191. if ( is_single() || is_page() || is_feed() )
  3192. $more = 1;
  3193. $content = $post->post_content;
  3194. if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
  3195. if ( $page > 1 )
  3196. $more = 1;
  3197. $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
  3198. $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
  3199. $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
  3200. // Ignore nextpage at the beginning of the content.
  3201. if ( 0 === strpos( $content, '<!--nextpage-->' ) )
  3202. $content = substr( $content, 15 );
  3203. $pages = explode('<!--nextpage-->', $content);
  3204. $numpages = count($pages);
  3205. if ( $numpages > 1 )
  3206. $multipage = 1;
  3207. } else {
  3208. $pages = array( $post->post_content );
  3209. }
  3210. do_action_ref_array('the_post', array(&$post));
  3211. return true;
  3212. }