PageRenderTime 73ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

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