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

/wp-includes/query.php

https://github.com/davidmh/WordPress
PHP | 3620 lines | 1817 code | 460 blank | 1343 comment | 529 complexity | 33bdb2278c2acecc02baee9f8fa1b6b5 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * WordPress Query API
  4. *
  5. * The query API attempts to get which part of WordPress to the user is on. It
  6. * also provides functionality to getting URL query information.
  7. *
  8. * @link http://codex.wordpress.org/The_Loop More information on The Loop.
  9. *
  10. * @package WordPress
  11. * @subpackage Query
  12. */
  13. /**
  14. * Retrieve variable in the WP_Query class.
  15. *
  16. * @see WP_Query::get()
  17. * @since 1.5.0
  18. * @uses $wp_query
  19. *
  20. * @param string $var The variable key to retrieve.
  21. * @return mixed
  22. */
  23. function get_query_var($var) {
  24. global $wp_query;
  25. return $wp_query->get($var);
  26. }
  27. /**
  28. * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
  29. *
  30. * @uses WP_Query::get_queried_object
  31. *
  32. * @since 3.1.0
  33. * @access public
  34. *
  35. * @return object
  36. */
  37. function get_queried_object() {
  38. global $wp_query;
  39. return $wp_query->get_queried_object();
  40. }
  41. /**
  42. * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
  43. *
  44. * @uses WP_Query::get_queried_object_id()
  45. *
  46. * @since 3.1.0
  47. * @access public
  48. *
  49. * @return int
  50. */
  51. function get_queried_object_id() {
  52. global $wp_query;
  53. return $wp_query->get_queried_object_id();
  54. }
  55. /**
  56. * Set query variable.
  57. *
  58. * @see WP_Query::set()
  59. * @since 2.2.0
  60. * @uses $wp_query
  61. *
  62. * @param string $var Query variable key.
  63. * @param mixed $value
  64. * @return null
  65. */
  66. function set_query_var($var, $value) {
  67. global $wp_query;
  68. return $wp_query->set($var, $value);
  69. }
  70. /**
  71. * Set up The Loop with query parameters.
  72. *
  73. * This will override the current WordPress Loop and shouldn't be used more than
  74. * once. This must not be used within the WordPress Loop.
  75. *
  76. * @since 1.5.0
  77. * @uses $wp_query
  78. *
  79. * @param string $query
  80. * @return array List of posts
  81. */
  82. function &query_posts($query) {
  83. 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 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 a 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 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 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 a 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 a 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 a 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 a 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 a 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 a 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 a 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 a 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 a 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 a specific year?
  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. * Amount of posts if limit clause was not used.
  863. *
  864. * @since 2.1.0
  865. * @access public
  866. * @var int
  867. */
  868. var $found_posts = 0;
  869. /**
  870. * The amount of pages.
  871. *
  872. * @since 2.1.0
  873. * @access public
  874. * @var int
  875. */
  876. var $max_num_pages = 0;
  877. /**
  878. * The amount of comment pages.
  879. *
  880. * @since 2.7.0
  881. * @access public
  882. * @var int
  883. */
  884. var $max_num_comment_pages = 0;
  885. /**
  886. * Set if query is single post.
  887. *
  888. * @since 1.5.0
  889. * @access public
  890. * @var bool
  891. */
  892. var $is_single = false;
  893. /**
  894. * Set if query is preview of blog.
  895. *
  896. * @since 2.0.0
  897. * @access public
  898. * @var bool
  899. */
  900. var $is_preview = false;
  901. /**
  902. * Set if query returns a page.
  903. *
  904. * @since 1.5.0
  905. * @access public
  906. * @var bool
  907. */
  908. var $is_page = false;
  909. /**
  910. * Set if query is an archive list.
  911. *
  912. * @since 1.5.0
  913. * @access public
  914. * @var bool
  915. */
  916. var $is_archive = false;
  917. /**
  918. * Set if query is part of a date.
  919. *
  920. * @since 1.5.0
  921. * @access public
  922. * @var bool
  923. */
  924. var $is_date = false;
  925. /**
  926. * Set if query contains a year.
  927. *
  928. * @since 1.5.0
  929. * @access public
  930. * @var bool
  931. */
  932. var $is_year = false;
  933. /**
  934. * Set if query contains a month.
  935. *
  936. * @since 1.5.0
  937. * @access public
  938. * @var bool
  939. */
  940. var $is_month = false;
  941. /**
  942. * Set if query contains a day.
  943. *
  944. * @since 1.5.0
  945. * @access public
  946. * @var bool
  947. */
  948. var $is_day = false;
  949. /**
  950. * Set if query contains time.
  951. *
  952. * @since 1.5.0
  953. * @access public
  954. * @var bool
  955. */
  956. var $is_time = false;
  957. /**
  958. * Set if query contains an author.
  959. *
  960. * @since 1.5.0
  961. * @access public
  962. * @var bool
  963. */
  964. var $is_author = false;
  965. /**
  966. * Set if query contains category.
  967. *
  968. * @since 1.5.0
  969. * @access public
  970. * @var bool
  971. */
  972. var $is_category = false;
  973. /**
  974. * Set if query contains tag.
  975. *
  976. * @since 2.3.0
  977. * @access public
  978. * @var bool
  979. */
  980. var $is_tag = false;
  981. /**
  982. * Set if query contains taxonomy.
  983. *
  984. * @since 2.5.0
  985. * @access public
  986. * @var bool
  987. */
  988. var $is_tax = false;
  989. /**
  990. * Set if query was part of a search result.
  991. *
  992. * @since 1.5.0
  993. * @access public
  994. * @var bool
  995. */
  996. var $is_search = false;
  997. /**
  998. * Set if query is feed display.
  999. *
  1000. * @since 1.5.0
  1001. * @access public
  1002. * @var bool
  1003. */
  1004. var $is_feed = false;
  1005. /**
  1006. * Set if query is comment feed display.
  1007. *
  1008. * @since 2.2.0
  1009. * @access public
  1010. * @var bool
  1011. */
  1012. var $is_comment_feed = false;
  1013. /**
  1014. * Set if query is trackback.
  1015. *
  1016. * @since 1.5.0
  1017. * @access public
  1018. * @var bool
  1019. */
  1020. var $is_trackback = false;
  1021. /**
  1022. * Set if query is blog homepage.
  1023. *
  1024. * @since 1.5.0
  1025. * @access public
  1026. * @var bool
  1027. */
  1028. var $is_home = false;
  1029. /**
  1030. * Set if query couldn't found anything.
  1031. *
  1032. * @since 1.5.0
  1033. * @access public
  1034. * @var bool
  1035. */
  1036. var $is_404 = false;
  1037. /**
  1038. * Set if query is within comments popup window.
  1039. *
  1040. * @since 1.5.0
  1041. * @access public
  1042. * @var bool
  1043. */
  1044. var $is_comments_popup = false;
  1045. /**
  1046. * Set if query is paged
  1047. *
  1048. * @since 1.5.0
  1049. * @access public
  1050. * @var bool
  1051. */
  1052. var $is_paged = false;
  1053. /**
  1054. * Set if query is part of administration page.
  1055. *
  1056. * @since 1.5.0
  1057. * @access public
  1058. * @var bool
  1059. */
  1060. var $is_admin = false;
  1061. /**
  1062. * Set if query is an attachment.
  1063. *
  1064. * @since 2.0.0
  1065. * @access public
  1066. * @var bool
  1067. */
  1068. var $is_attachment = false;
  1069. /**
  1070. * Set if is single, is a page, or is an attachment.
  1071. *
  1072. * @since 2.1.0
  1073. * @access public
  1074. * @var bool
  1075. */
  1076. var $is_singular = false;
  1077. /**
  1078. * Set if query is for robots.
  1079. *
  1080. * @since 2.1.0
  1081. * @access public
  1082. * @var bool
  1083. */
  1084. var $is_robots = false;
  1085. /**
  1086. * Set if query contains posts.
  1087. *
  1088. * Basically, the homepage if the option isn't set for the static homepage.
  1089. *
  1090. * @since 2.1.0
  1091. * @access public
  1092. * @var bool
  1093. */
  1094. var $is_posts_page = false;
  1095. /**
  1096. * Set if query is for a post type archive.
  1097. *
  1098. * @since 3.1.0
  1099. * @access public
  1100. * @var bool
  1101. */
  1102. var $is_post_type_archive = false;
  1103. /**
  1104. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1105. * whether we have to re-parse because something has changed
  1106. *
  1107. * @since 3.1.0
  1108. * @access private
  1109. */
  1110. var $query_vars_hash = false;
  1111. /**
  1112. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1113. * via pre_get_posts hooks.
  1114. *
  1115. * @since 3.1.1
  1116. * @access private
  1117. */
  1118. var $query_vars_changed = true;
  1119. /**
  1120. * Set if post thumbnails are cached
  1121. *
  1122. * @since 3.2.0
  1123. * @access public
  1124. * @var bool
  1125. */
  1126. var $thumbnails_cached = false;
  1127. /**
  1128. * Resets query flags to false.
  1129. *
  1130. * The query flags are what page info WordPress was able to figure out.
  1131. *
  1132. * @since 2.0.0
  1133. * @access private
  1134. */
  1135. function init_query_flags() {
  1136. $this->is_single = false;
  1137. $this->is_preview = false;
  1138. $this->is_page = false;
  1139. $this->is_archive = false;
  1140. $this->is_date = false;
  1141. $this->is_year = false;
  1142. $this->is_month = false;
  1143. $this->is_day = false;
  1144. $this->is_time = false;
  1145. $this->is_author = false;
  1146. $this->is_category = false;
  1147. $this->is_tag = false;
  1148. $this->is_tax = false;
  1149. $this->is_search = false;
  1150. $this->is_feed = false;
  1151. $this->is_comment_feed = false;
  1152. $this->is_trackback = false;
  1153. $this->is_home = false;
  1154. $this->is_404 = false;
  1155. $this->is_comments_popup = false;
  1156. $this->is_paged = false;
  1157. $this->is_admin = false;
  1158. $this->is_attachment = false;
  1159. $this->is_singular = false;
  1160. $this->is_robots = false;
  1161. $this->is_posts_page = false;
  1162. $this->is_post_type_archive = false;
  1163. }
  1164. /**
  1165. * Initiates object properties and sets default values.
  1166. *
  1167. * @since 1.5.0
  1168. * @access public
  1169. */
  1170. function init() {
  1171. unset($this->posts);
  1172. unset($this->query);
  1173. $this->query_vars = array();
  1174. unset($this->queried_object);
  1175. unset($this->queried_object_id);
  1176. $this->post_count = 0;
  1177. $this->current_post = -1;
  1178. $this->in_the_loop = false;
  1179. unset( $this->request );
  1180. unset( $this->post );
  1181. unset( $this->comments );
  1182. unset( $this->comment );
  1183. $this->comment_count = 0;
  1184. $this->current_comment = -1;
  1185. $this->found_posts = 0;
  1186. $this->max_num_pages = 0;
  1187. $this->max_num_comment_pages = 0;
  1188. $this->init_query_flags();
  1189. }
  1190. /**
  1191. * Reparse the query vars.
  1192. *
  1193. * @since 1.5.0
  1194. * @access public
  1195. */
  1196. function parse_query_vars() {
  1197. $this->parse_query();
  1198. }
  1199. /**
  1200. * Fills in the query variables, which do not exist within the parameter.
  1201. *
  1202. * @since 2.1.0
  1203. * @access public
  1204. *
  1205. * @param array $array Defined query variables.
  1206. * @return array Complete query variables with undefined ones filled in empty.
  1207. */
  1208. function fill_query_vars($array) {
  1209. $keys = array(
  1210. 'error'
  1211. , 'm'
  1212. , 'p'
  1213. , 'post_parent'
  1214. , 'subpost'
  1215. , 'subpost_id'
  1216. , 'attachment'
  1217. , 'attachment_id'
  1218. , 'name'
  1219. , 'static'
  1220. , 'pagename'
  1221. , 'page_id'
  1222. , 'second'
  1223. , 'minute'
  1224. , 'hour'
  1225. , 'day'
  1226. , 'monthnum'
  1227. , 'year'
  1228. , 'w'
  1229. , 'category_name'
  1230. , 'tag'
  1231. , 'cat'
  1232. , 'tag_id'
  1233. , 'author_name'
  1234. , 'feed'
  1235. , 'tb'
  1236. , 'paged'
  1237. , 'comments_popup'
  1238. , 'meta_key'
  1239. , 'meta_value'
  1240. , 'preview'
  1241. , 's'
  1242. , 'sentence'
  1243. , 'fields'
  1244. );
  1245. foreach ( $keys as $key ) {
  1246. if ( !isset($array[$key]) )
  1247. $array[$key] = '';
  1248. }
  1249. $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1250. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  1251. foreach ( $array_keys as $key ) {
  1252. if ( !isset($array[$key]) )
  1253. $array[$key] = array();
  1254. }
  1255. return $array;
  1256. }
  1257. /**
  1258. * Parse a query string and set query type booleans.
  1259. *
  1260. * @since 1.5.0
  1261. * @access public
  1262. *
  1263. * @param string|array $query Optional query.
  1264. */
  1265. function parse_query( $query = '' ) {
  1266. if ( ! empty( $query ) ) {
  1267. $this->init();
  1268. $this->query = $this->query_vars = wp_parse_args( $query );
  1269. } elseif ( ! isset( $this->query ) ) {
  1270. $this->query = $this->query_vars;
  1271. }
  1272. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1273. $qv = &$this->query_vars;
  1274. $this->query_vars_changed = true;
  1275. if ( ! empty($qv['robots']) )
  1276. $this->is_robots = true;
  1277. $qv['p'] = absint($qv['p']);
  1278. $qv['page_id'] = absint($qv['page_id']);
  1279. $qv['year'] = absint($qv['year']);
  1280. $qv['monthnum'] = absint($qv['monthnum']);
  1281. $qv['day'] = absint($qv['day']);
  1282. $qv['w'] = absint($qv['w']);
  1283. $qv['m'] = absint($qv['m']);
  1284. $qv['paged'] = absint($qv['paged']);
  1285. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1286. $qv['pagename'] = trim( $qv['pagename'] );
  1287. $qv['name'] = trim( $qv['name'] );
  1288. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1289. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1290. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1291. // Compat. Map subpost to attachment.
  1292. if ( '' != $qv['subpost'] )
  1293. $qv['attachment'] = $qv['subpost'];
  1294. if ( '' != $qv['subpost_id'] )
  1295. $qv['attachment_id'] = $qv['subpost_id'];
  1296. $qv['attachment_id'] = absint($qv['attachment_id']);
  1297. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1298. $this->is_single = true;
  1299. $this->is_attachment = true;
  1300. } elseif ( '' != $qv['name'] ) {
  1301. $this->is_single = true;
  1302. } elseif ( $qv['p'] ) {
  1303. $this->is_single = true;
  1304. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1305. // If year, month, day, hour, minute, and second are set, a single
  1306. // post is being queried.
  1307. $this->is_single = true;
  1308. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1309. $this->is_page = true;
  1310. $this->is_single = false;
  1311. } else {
  1312. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1313. if ( !empty($qv['s']) ) {
  1314. $this->is_search = true;
  1315. }
  1316. if ( '' !== $qv['second'] ) {
  1317. $this->is_time = true;
  1318. $this->is_date = true;
  1319. }
  1320. if ( '' !== $qv['minute'] ) {
  1321. $this->is_time = true;
  1322. $this->is_date = true;
  1323. }
  1324. if ( '' !== $qv['hour'] ) {
  1325. $this->is_time = true;
  1326. $this->is_date = true;
  1327. }
  1328. if ( $qv['day'] ) {
  1329. if ( ! $this->is_date ) {
  1330. $this->is_day = true;
  1331. $this->is_date = true;
  1332. }
  1333. }
  1334. if ( $qv['monthnum'] ) {
  1335. if ( ! $this->is_date ) {
  1336. $this->is_month = true;
  1337. $this->is_date = true;
  1338. }
  1339. }
  1340. if ( $qv['year'] ) {
  1341. if ( ! $this->is_date ) {
  1342. $this->is_year = true;
  1343. $this->is_date = true;
  1344. }
  1345. }
  1346. if ( $qv['m'] ) {
  1347. $this->is_date = true;
  1348. if ( strlen($qv['m']) > 9 ) {
  1349. $this->is_time = true;
  1350. } else if ( strlen($qv['m']) > 7 ) {
  1351. $this->is_day = true;
  1352. } else if ( strlen($qv['m']) > 5 ) {
  1353. $this->is_month = true;
  1354. } else {
  1355. $this->is_year = true;
  1356. }
  1357. }
  1358. if ( '' != $qv['w'] ) {
  1359. $this->is_date = true;
  1360. }
  1361. $this->query_vars_hash = false;
  1362. $this->parse_tax_query( $qv );
  1363. foreach ( $this->tax_query->queries as $tax_query ) {
  1364. if ( 'NOT IN' != $tax_query['operator'] ) {
  1365. switch ( $tax_query['taxonomy'] ) {
  1366. case 'category':
  1367. $this->is_category = true;
  1368. break;
  1369. case 'post_tag':
  1370. $this->is_tag = true;
  1371. break;
  1372. default:
  1373. $this->is_tax = true;
  1374. }
  1375. }
  1376. }
  1377. unset( $tax_query );
  1378. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1379. $this->is_author = false;
  1380. } else {
  1381. $this->is_author = true;
  1382. }
  1383. if ( '' != $qv['author_name'] )
  1384. $this->is_author = true;
  1385. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1386. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1387. if ( ! empty( $post_type_obj->has_archive ) )
  1388. $this->is_post_type_archive = true;
  1389. }
  1390. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1391. $this->is_archive = true;
  1392. }
  1393. if ( '' != $qv['feed'] )
  1394. $this->is_feed = true;
  1395. if ( '' != $qv['tb'] )
  1396. $this->is_trackback = true;
  1397. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1398. $this->is_paged = true;
  1399. if ( '' != $qv['comments_popup'] )
  1400. $this->is_comments_popup = true;
  1401. // if we're previewing inside the write screen
  1402. if ( '' != $qv['preview'] )
  1403. $this->is_preview = true;
  1404. if ( is_admin() )
  1405. $this->is_admin = true;
  1406. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1407. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1408. $qv['withcomments'] = 1;
  1409. }
  1410. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1411. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1412. $this->is_comment_feed = true;
  1413. 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 ) )
  1414. $this->is_home = true;
  1415. // Correct is_* for page_on_front and page_for_posts
  1416. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1417. $_query = wp_parse_args($this->query);
  1418. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1419. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1420. unset($_query['pagename']);
  1421. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1422. $this->is_page = true;
  1423. $this->is_home = false;
  1424. $qv['page_id'] = get_option('page_on_front');
  1425. // Correct <!--nextpage--> for page_on_front
  1426. if ( !empty($qv['paged']) ) {
  1427. $qv['page'] = $qv['paged'];
  1428. unset($qv['paged']);
  1429. }
  1430. }
  1431. }
  1432. if ( '' != $qv['pagename'] ) {
  1433. $this->queried_object =& get_page_by_path($qv['pagename']);
  1434. if ( !empty($this->queried_object) )
  1435. $this->queried_object_id = (int) $this->queried_object->ID;
  1436. else
  1437. unset($this->queried_object);
  1438. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1439. $this->is_page = false;
  1440. $this->is_home = true;
  1441. $this->is_posts_page = true;
  1442. }
  1443. }
  1444. if ( $qv['page_id'] ) {
  1445. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1446. $this->is_page = false;
  1447. $this->is_home = true;
  1448. $this->is_posts_page = true;
  1449. }
  1450. }
  1451. if ( !empty($qv['post_type']) ) {
  1452. if ( is_array($qv['post_type']) )
  1453. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1454. else
  1455. $qv['post_type'] = sanitize_key($qv['post_type']);
  1456. }
  1457. if ( ! empty( $qv['post_status'] ) ) {
  1458. if ( is_array( $qv['post_status'] ) )
  1459. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1460. else
  1461. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1462. }
  1463. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1464. $this->is_comment_feed = false;
  1465. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1466. // Done correcting is_* for page_on_front and page_for_posts
  1467. if ( '404' == $qv['error'] )
  1468. $this->set_404();
  1469. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1470. $this->query_vars_changed = false;
  1471. do_action_ref_array('parse_query', array(&$this));
  1472. }
  1473. /*
  1474. * Parses various taxonomy related query vars.
  1475. *
  1476. * @access protected
  1477. * @since 3.1.0
  1478. *
  1479. * @param array &$q The query variables
  1480. */
  1481. function parse_tax_query( &$q ) {
  1482. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1483. $tax_query = $q['tax_query'];
  1484. } else {
  1485. $tax_query = array();
  1486. }
  1487. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1488. $tax_query[] = array(
  1489. 'taxonomy' => $q['taxonomy'],
  1490. 'terms' => array( $q['term'] ),
  1491. 'field' => 'slug',
  1492. );
  1493. }
  1494. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1495. if ( 'post_tag' == $taxonomy )
  1496. continue; // Handled further down in the $q['tag'] block
  1497. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1498. $tax_query_defaults = array(
  1499. 'taxonomy' => $taxonomy,
  1500. 'field' => 'slug',
  1501. );
  1502. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1503. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1504. }
  1505. $term = $q[$t->query_var];
  1506. if ( strpos($term, '+') !== false ) {
  1507. $terms = preg_split( '/[+]+/', $term );
  1508. foreach ( $terms as $term ) {
  1509. $tax_query[] = array_merge( $tax_query_defaults, array(
  1510. 'terms' => array( $term )
  1511. ) );
  1512. }
  1513. } else {
  1514. $tax_query[] = array_merge( $tax_query_defaults, array(
  1515. 'terms' => preg_split( '/[,]+/', $term )
  1516. ) );
  1517. }
  1518. }
  1519. }
  1520. // Category stuff
  1521. if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
  1522. $q['cat'] = ''.urldecode($q['cat']).'';
  1523. $q['cat'] = addslashes_gpc($q['cat']);
  1524. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  1525. $q['cat'] = '';
  1526. $req_cats = array();
  1527. foreach ( (array) $cat_array as $cat ) {
  1528. $cat = intval($cat);
  1529. $req_cats[] = $cat;
  1530. $in = ($cat > 0);
  1531. $cat = abs($cat);
  1532. if ( $in ) {
  1533. $q['category__in'][] = $cat;
  1534. $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
  1535. } else {
  1536. $q['category__not_in'][] = $cat;
  1537. $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
  1538. }
  1539. }
  1540. $q['cat'] = implode(',', $req_cats);
  1541. }
  1542. if ( !empty($q['category__in']) ) {
  1543. $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
  1544. $tax_query[] = array(
  1545. 'taxonomy' => 'category',
  1546. 'terms' => $q['category__in'],
  1547. 'field' => 'term_id',
  1548. 'include_children' => false
  1549. );
  1550. }
  1551. if ( !empty($q['category__not_in']) ) {
  1552. $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
  1553. $tax_query[] = array(
  1554. 'taxonomy' => 'category',
  1555. 'terms' => $q['category__not_in'],
  1556. 'operator' => 'NOT IN',
  1557. 'include_children' => false
  1558. );
  1559. }
  1560. if ( !empty($q['category__and']) ) {
  1561. $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
  1562. $tax_query[] = array(
  1563. 'taxonomy' => 'category',
  1564. 'terms' => $q['category__and'],
  1565. 'field' => 'term_id',
  1566. 'operator' => 'AND',
  1567. 'include_children' => false
  1568. );
  1569. }
  1570. // Tag stuff
  1571. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1572. if ( strpos($q['tag'], ',') !== false ) {
  1573. $tags = preg_split('/[,\s]+/', $q['tag']);
  1574. foreach ( (array) $tags as $tag ) {
  1575. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1576. $q['tag_slug__in'][] = $tag;
  1577. }
  1578. } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
  1579. $tags = preg_split('/[+\s]+/', $q['tag']);
  1580. foreach ( (array) $tags as $tag ) {
  1581. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1582. $q['tag_slug__and'][] = $tag;
  1583. }
  1584. } else {
  1585. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1586. $q['tag_slug__in'][] = $q['tag'];
  1587. }
  1588. }
  1589. if ( !empty($q['tag_id']) ) {
  1590. $q['tag_id'] = absint( $q['tag_id'] );
  1591. $tax_query[] = array(
  1592. 'taxonomy' => 'post_tag',
  1593. 'terms' => $q['tag_id']
  1594. );
  1595. }
  1596. if ( !empty($q['tag__in']) ) {
  1597. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1598. $tax_query[] = array(
  1599. 'taxonomy' => 'post_tag',
  1600. 'terms' => $q['tag__in']
  1601. );
  1602. }
  1603. if ( !empty($q['tag__not_in']) ) {
  1604. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1605. $tax_query[] = array(
  1606. 'taxonomy' => 'post_tag',
  1607. 'terms' => $q['tag__not_in'],
  1608. 'operator' => 'NOT IN'
  1609. );
  1610. }
  1611. if ( !empty($q['tag__and']) ) {
  1612. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1613. $tax_query[] = array(
  1614. 'taxonomy' => 'post_tag',
  1615. 'terms' => $q['tag__and'],
  1616. 'operator' => 'AND'
  1617. );
  1618. }
  1619. if ( !empty($q['tag_slug__in']) ) {
  1620. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
  1621. $tax_query[] = array(
  1622. 'taxonomy' => 'post_tag',
  1623. 'terms' => $q['tag_slug__in'],
  1624. 'field' => 'slug'
  1625. );
  1626. }
  1627. if ( !empty($q['tag_slug__and']) ) {
  1628. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
  1629. $tax_query[] = array(
  1630. 'taxonomy' => 'post_tag',
  1631. 'terms' => $q['tag_slug__and'],
  1632. 'field' => 'slug',
  1633. 'operator' => 'AND'
  1634. );
  1635. }
  1636. $this->tax_query = new WP_Tax_Query( $tax_query );
  1637. }
  1638. /**
  1639. * Sets the 404 property and saves whether query is feed.
  1640. *
  1641. * @since 2.0.0
  1642. * @access public
  1643. */
  1644. function set_404() {
  1645. $is_feed = $this->is_feed;
  1646. $this->init_query_flags();
  1647. $this->is_404 = true;
  1648. $this->is_feed = $is_feed;
  1649. }
  1650. /**
  1651. * Retrieve query variable.
  1652. *
  1653. * @since 1.5.0
  1654. * @access public
  1655. *
  1656. * @param string $query_var Query variable key.
  1657. * @return mixed
  1658. */
  1659. function get($query_var) {
  1660. if ( isset($this->query_vars[$query_var]) )
  1661. return $this->query_vars[$query_var];
  1662. return '';
  1663. }
  1664. /**
  1665. * Set query variable.
  1666. *
  1667. * @since 1.5.0
  1668. * @access public
  1669. *
  1670. * @param string $query_var Query variable key.
  1671. * @param mixed $value Query variable value.
  1672. */
  1673. function set($query_var, $value) {
  1674. $this->query_vars[$query_var] = $value;
  1675. }
  1676. /**
  1677. * Retrieve the posts based on query variables.
  1678. *
  1679. * There are a few filters and actions that can be used to modify the post
  1680. * database query.
  1681. *
  1682. * @since 1.5.0
  1683. * @access public
  1684. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1685. *
  1686. * @return array List of posts.
  1687. */
  1688. function &get_posts() {
  1689. global $wpdb, $user_ID, $_wp_using_ext_object_cache;
  1690. $this->parse_query();
  1691. do_action_ref_array('pre_get_posts', array(&$this));
  1692. // Shorthand.
  1693. $q = &$this->query_vars;
  1694. // Fill again in case pre_get_posts unset some vars.
  1695. $q = $this->fill_query_vars($q);
  1696. // Parse meta query
  1697. $this->meta_query = new WP_Meta_Query();
  1698. $this->meta_query->parse_query_vars( $q );
  1699. // Set a flag if a pre_get_posts hook changed the query vars.
  1700. $hash = md5( serialize( $this->query_vars ) );
  1701. if ( $hash != $this->query_vars_hash ) {
  1702. $this->query_vars_changed = true;
  1703. $this->query_vars_hash = $hash;
  1704. }
  1705. unset($hash);
  1706. // First let's clear some variables
  1707. $distinct = '';
  1708. $whichauthor = '';
  1709. $whichmimetype = '';
  1710. $where = '';
  1711. $limits = '';
  1712. $join = '';
  1713. $search = '';
  1714. $groupby = '';
  1715. $fields = '';
  1716. $post_status_join = false;
  1717. $page = 1;
  1718. if ( isset( $q['caller_get_posts'] ) ) {
  1719. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1720. if ( !isset( $q['ignore_sticky_posts'] ) )
  1721. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1722. }
  1723. if ( !isset( $q['ignore_sticky_posts'] ) )
  1724. $q['ignore_sticky_posts'] = false;
  1725. if ( !isset($q['suppress_filters']) )
  1726. $q['suppress_filters'] = false;
  1727. if ( !isset($q['cache_results']) ) {
  1728. if ( $_wp_using_ext_object_cache )
  1729. $q['cache_results'] = false;
  1730. else
  1731. $q['cache_results'] = true;
  1732. }
  1733. if ( !isset($q['update_post_term_cache']) )
  1734. $q['update_post_term_cache'] = true;
  1735. if ( !isset($q['update_post_meta_cache']) )
  1736. $q['update_post_meta_cache'] = true;
  1737. if ( !isset($q['post_type']) ) {
  1738. if ( $this->is_search )
  1739. $q['post_type'] = 'any';
  1740. else
  1741. $q['post_type'] = '';
  1742. }
  1743. $post_type = $q['post_type'];
  1744. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  1745. $q['posts_per_page'] = get_option('posts_per_page');
  1746. if ( isset($q['showposts']) && $q['showposts'] ) {
  1747. $q['showposts'] = (int) $q['showposts'];
  1748. $q['posts_per_page'] = $q['showposts'];
  1749. }
  1750. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1751. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1752. if ( !isset($q['nopaging']) ) {
  1753. if ( $q['posts_per_page'] == -1 ) {
  1754. $q['nopaging'] = true;
  1755. } else {
  1756. $q['nopaging'] = false;
  1757. }
  1758. }
  1759. if ( $this->is_feed ) {
  1760. $q['posts_per_page'] = get_option('posts_per_rss');
  1761. $q['nopaging'] = false;
  1762. }
  1763. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1764. if ( $q['posts_per_page'] < -1 )
  1765. $q['posts_per_page'] = abs($q['posts_per_page']);
  1766. else if ( $q['posts_per_page'] == 0 )
  1767. $q['posts_per_page'] = 1;
  1768. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1769. $q['comments_per_page'] = get_option('comments_per_page');
  1770. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1771. $this->is_page = true;
  1772. $this->is_home = false;
  1773. $q['page_id'] = get_option('page_on_front');
  1774. }
  1775. if ( isset($q['page']) ) {
  1776. $q['page'] = trim($q['page'], '/');
  1777. $q['page'] = absint($q['page']);
  1778. }
  1779. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1780. if ( isset($q['no_found_rows']) )
  1781. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1782. else
  1783. $q['no_found_rows'] = false;
  1784. switch ( $q['fields'] ) {
  1785. case 'ids':
  1786. $fields = "$wpdb->posts.ID";
  1787. break;
  1788. case 'id=>parent':
  1789. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  1790. break;
  1791. default:
  1792. $fields = "$wpdb->posts.*";
  1793. }
  1794. // If a month is specified in the querystring, load that month
  1795. if ( $q['m'] ) {
  1796. $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  1797. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  1798. if ( strlen($q['m']) > 5 )
  1799. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  1800. if ( strlen($q['m']) > 7 )
  1801. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  1802. if ( strlen($q['m']) > 9 )
  1803. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  1804. if ( strlen($q['m']) > 11 )
  1805. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  1806. if ( strlen($q['m']) > 13 )
  1807. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  1808. }
  1809. if ( '' !== $q['hour'] )
  1810. $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
  1811. if ( '' !== $q['minute'] )
  1812. $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
  1813. if ( '' !== $q['second'] )
  1814. $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
  1815. if ( $q['year'] )
  1816. $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
  1817. if ( $q['monthnum'] )
  1818. $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
  1819. if ( $q['day'] )
  1820. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
  1821. // If we've got a post_type AND its not "any" post_type.
  1822. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  1823. foreach ( (array)$q['post_type'] as $_post_type ) {
  1824. $ptype_obj = get_post_type_object($_post_type);
  1825. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  1826. continue;
  1827. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  1828. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  1829. $q['name'] = $q[ $ptype_obj->query_var ];
  1830. } else {
  1831. // Hierarchical post_types will operate through the
  1832. $q['pagename'] = $q[ $ptype_obj->query_var ];
  1833. $q['name'] = '';
  1834. }
  1835. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  1836. break;
  1837. } //end foreach
  1838. unset($ptype_obj);
  1839. }
  1840. if ( '' != $q['name'] ) {
  1841. $q['name'] = sanitize_title_for_query( $q['name'] );
  1842. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  1843. } elseif ( '' != $q['pagename'] ) {
  1844. if ( isset($this->queried_object_id) ) {
  1845. $reqpage = $this->queried_object_id;
  1846. } else {
  1847. if ( 'page' != $q['post_type'] ) {
  1848. foreach ( (array)$q['post_type'] as $_post_type ) {
  1849. $ptype_obj = get_post_type_object($_post_type);
  1850. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  1851. continue;
  1852. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  1853. if ( $reqpage )
  1854. break;
  1855. }
  1856. unset($ptype_obj);
  1857. } else {
  1858. $reqpage = get_page_by_path($q['pagename']);
  1859. }
  1860. if ( !empty($reqpage) )
  1861. $reqpage = $reqpage->ID;
  1862. else
  1863. $reqpage = 0;
  1864. }
  1865. $page_for_posts = get_option('page_for_posts');
  1866. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  1867. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  1868. $q['name'] = $q['pagename'];
  1869. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  1870. $reqpage_obj = get_page($reqpage);
  1871. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  1872. $this->is_attachment = true;
  1873. $post_type = $q['post_type'] = 'attachment';
  1874. $this->is_page = true;
  1875. $q['attachment_id'] = $reqpage;
  1876. }
  1877. }
  1878. } elseif ( '' != $q['attachment'] ) {
  1879. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  1880. $q['name'] = $q['attachment'];
  1881. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  1882. }
  1883. if ( $q['w'] )
  1884. $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
  1885. if ( intval($q['comments_popup']) )
  1886. $q['p'] = absint($q['comments_popup']);
  1887. // If an attachment is requested by number, let it supersede any post number.
  1888. if ( $q['attachment_id'] )
  1889. $q['p'] = absint($q['attachment_id']);
  1890. // If a post number is specified, load that post
  1891. if ( $q['p'] ) {
  1892. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  1893. } elseif ( $q['post__in'] ) {
  1894. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  1895. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  1896. } elseif ( $q['post__not_in'] ) {
  1897. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  1898. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  1899. }
  1900. if ( is_numeric($q['post_parent']) )
  1901. $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  1902. if ( $q['page_id'] ) {
  1903. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  1904. $q['p'] = $q['page_id'];
  1905. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  1906. }
  1907. }
  1908. // If a search pattern is specified, load the posts that match
  1909. if ( !empty($q['s']) ) {
  1910. // added slashes screw with quote grouping when done early, so done later
  1911. $q['s'] = stripslashes($q['s']);
  1912. if ( !empty($q['sentence']) ) {
  1913. $q['search_terms'] = array($q['s']);
  1914. } else {
  1915. preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
  1916. $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
  1917. }
  1918. $n = !empty($q['exact']) ? '' : '%';
  1919. $searchand = '';
  1920. foreach( (array) $q['search_terms'] as $term ) {
  1921. $term = esc_sql( like_escape( $term ) );
  1922. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  1923. $searchand = ' AND ';
  1924. }
  1925. if ( !empty($search) ) {
  1926. $search = " AND ({$search}) ";
  1927. if ( !is_user_logged_in() )
  1928. $search .= " AND ($wpdb->posts.post_password = '') ";
  1929. }
  1930. }
  1931. // Allow plugins to contextually add/remove/modify the search section of the database query
  1932. $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
  1933. // Taxonomies
  1934. if ( !$this->is_singular ) {
  1935. $this->parse_tax_query( $q );
  1936. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
  1937. $join .= $clauses['join'];
  1938. $where .= $clauses['where'];
  1939. }
  1940. if ( $this->is_tax ) {
  1941. if ( empty($post_type) ) {
  1942. $post_type = 'any';
  1943. $post_status_join = true;
  1944. } elseif ( in_array('attachment', (array) $post_type) ) {
  1945. $post_status_join = true;
  1946. }
  1947. }
  1948. // Back-compat
  1949. if ( !empty($this->tax_query->queries) ) {
  1950. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  1951. if ( !empty( $tax_query_in_and ) ) {
  1952. if ( !isset( $q['taxonomy'] ) ) {
  1953. foreach ( $tax_query_in_and as $a_tax_query ) {
  1954. if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
  1955. $q['taxonomy'] = $a_tax_query['taxonomy'];
  1956. if ( 'slug' == $a_tax_query['field'] )
  1957. $q['term'] = $a_tax_query['terms'][0];
  1958. else
  1959. $q['term_id'] = $a_tax_query['terms'][0];
  1960. break;
  1961. }
  1962. }
  1963. }
  1964. $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
  1965. if ( !empty( $cat_query ) ) {
  1966. $cat_query = reset( $cat_query );
  1967. $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
  1968. if ( $the_cat ) {
  1969. $this->set( 'cat', $the_cat->term_id );
  1970. $this->set( 'category_name', $the_cat->slug );
  1971. }
  1972. unset( $the_cat );
  1973. }
  1974. unset( $cat_query );
  1975. $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
  1976. if ( !empty( $tag_query ) ) {
  1977. $tag_query = reset( $tag_query );
  1978. $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
  1979. if ( $the_tag ) {
  1980. $this->set( 'tag_id', $the_tag->term_id );
  1981. }
  1982. unset( $the_tag );
  1983. }
  1984. unset( $tag_query );
  1985. }
  1986. }
  1987. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
  1988. $groupby = "{$wpdb->posts}.ID";
  1989. }
  1990. // Author/user stuff
  1991. if ( empty($q['author']) || ($q['author'] == '0') ) {
  1992. $whichauthor = '';
  1993. } else {
  1994. $q['author'] = (string)urldecode($q['author']);
  1995. $q['author'] = addslashes_gpc($q['author']);
  1996. if ( strpos($q['author'], '-') !== false ) {
  1997. $eq = '!=';
  1998. $andor = 'AND';
  1999. $q['author'] = explode('-', $q['author']);
  2000. $q['author'] = (string)absint($q['author'][1]);
  2001. } else {
  2002. $eq = '=';
  2003. $andor = 'OR';
  2004. }
  2005. $author_array = preg_split('/[,\s]+/', $q['author']);
  2006. $_author_array = array();
  2007. foreach ( $author_array as $key => $_author )
  2008. $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
  2009. $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
  2010. unset($author_array, $_author_array);
  2011. }
  2012. // Author stuff for nice URLs
  2013. if ( '' != $q['author_name'] ) {
  2014. if ( strpos($q['author_name'], '/') !== false ) {
  2015. $q['author_name'] = explode('/', $q['author_name']);
  2016. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  2017. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  2018. } else {
  2019. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
  2020. }
  2021. }
  2022. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  2023. $q['author'] = get_user_by('slug', $q['author_name']);
  2024. if ( $q['author'] )
  2025. $q['author'] = $q['author']->ID;
  2026. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  2027. }
  2028. // MIME-Type stuff for attachment browsing
  2029. if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
  2030. $table_alias = $post_status_join ? $wpdb->posts : '';
  2031. $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
  2032. }
  2033. $where .= $search . $whichauthor . $whichmimetype;
  2034. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  2035. $q['order'] = 'DESC';
  2036. // Order by
  2037. if ( empty($q['orderby']) ) {
  2038. $orderby = "$wpdb->posts.post_date " . $q['order'];
  2039. } elseif ( 'none' == $q['orderby'] ) {
  2040. $orderby = '';
  2041. } else {
  2042. // Used to filter values
  2043. $allowed_keys = array('name', 'author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2044. if ( !empty($q['meta_key']) ) {
  2045. $allowed_keys[] = $q['meta_key'];
  2046. $allowed_keys[] = 'meta_value';
  2047. $allowed_keys[] = 'meta_value_num';
  2048. }
  2049. $q['orderby'] = urldecode($q['orderby']);
  2050. $q['orderby'] = addslashes_gpc($q['orderby']);
  2051. $orderby_array = array();
  2052. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  2053. // Only allow certain values for safety
  2054. if ( ! in_array($orderby, $allowed_keys) )
  2055. continue;
  2056. switch ( $orderby ) {
  2057. case 'menu_order':
  2058. break;
  2059. case 'ID':
  2060. $orderby = "$wpdb->posts.ID";
  2061. break;
  2062. case 'rand':
  2063. $orderby = 'RAND()';
  2064. break;
  2065. case $q['meta_key']:
  2066. case 'meta_value':
  2067. $orderby = "$wpdb->postmeta.meta_value";
  2068. break;
  2069. case 'meta_value_num':
  2070. $orderby = "$wpdb->postmeta.meta_value+0";
  2071. break;
  2072. case 'comment_count':
  2073. $orderby = "$wpdb->posts.comment_count";
  2074. break;
  2075. default:
  2076. $orderby = "$wpdb->posts.post_" . $orderby;
  2077. }
  2078. $orderby_array[] = $orderby;
  2079. }
  2080. $orderby = implode( ',', $orderby_array );
  2081. if ( empty( $orderby ) )
  2082. $orderby = "$wpdb->posts.post_date ".$q['order'];
  2083. else
  2084. $orderby .= " {$q['order']}";
  2085. }
  2086. if ( is_array( $post_type ) ) {
  2087. $post_type_cap = 'multiple_post_type';
  2088. } else {
  2089. $post_type_object = get_post_type_object( $post_type );
  2090. if ( empty( $post_type_object ) )
  2091. $post_type_cap = $post_type;
  2092. }
  2093. if ( 'any' == $post_type ) {
  2094. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  2095. if ( ! empty( $in_search_post_types ) )
  2096. $where .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
  2097. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2098. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2099. } elseif ( ! empty( $post_type ) ) {
  2100. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  2101. $post_type_object = get_post_type_object ( $post_type );
  2102. } elseif ( $this->is_attachment ) {
  2103. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  2104. $post_type_object = get_post_type_object ( 'attachment' );
  2105. } elseif ( $this->is_page ) {
  2106. $where .= " AND $wpdb->posts.post_type = 'page'";
  2107. $post_type_object = get_post_type_object ( 'page' );
  2108. } else {
  2109. $where .= " AND $wpdb->posts.post_type = 'post'";
  2110. $post_type_object = get_post_type_object ( 'post' );
  2111. }
  2112. if ( ! empty( $post_type_object ) ) {
  2113. $edit_cap = $post_type_object->cap->edit_post;
  2114. $read_cap = $post_type_object->cap->read_post;
  2115. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  2116. $read_private_cap = $post_type_object->cap->read_private_posts;
  2117. } else {
  2118. $edit_cap = 'edit_' . $post_type_cap;
  2119. $read_cap = 'read_' . $post_type_cap;
  2120. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  2121. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  2122. }
  2123. if ( ! empty( $q['post_status'] ) ) {
  2124. $statuswheres = array();
  2125. $q_status = $q['post_status'];
  2126. if ( ! is_array( $q_status ) )
  2127. $q_status = explode(',', $q_status);
  2128. $r_status = array();
  2129. $p_status = array();
  2130. $e_status = array();
  2131. if ( in_array('any', $q_status) ) {
  2132. foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  2133. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  2134. } else {
  2135. foreach ( get_post_stati() as $status ) {
  2136. if ( in_array( $status, $q_status ) ) {
  2137. if ( 'private' == $status )
  2138. $p_status[] = "$wpdb->posts.post_status = '$status'";
  2139. else
  2140. $r_status[] = "$wpdb->posts.post_status = '$status'";
  2141. }
  2142. }
  2143. }
  2144. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2145. $r_status = array_merge($r_status, $p_status);
  2146. unset($p_status);
  2147. }
  2148. if ( !empty($e_status) ) {
  2149. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2150. }
  2151. if ( !empty($r_status) ) {
  2152. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2153. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
  2154. else
  2155. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2156. }
  2157. if ( !empty($p_status) ) {
  2158. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2159. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
  2160. else
  2161. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2162. }
  2163. if ( $post_status_join ) {
  2164. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2165. foreach ( $statuswheres as $index => $statuswhere )
  2166. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  2167. }
  2168. foreach ( $statuswheres as $statuswhere )
  2169. $where .= " AND $statuswhere";
  2170. } elseif ( !$this->is_singular ) {
  2171. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  2172. // Add public states.
  2173. $public_states = get_post_stati( array('public' => true) );
  2174. foreach ( (array) $public_states as $state ) {
  2175. if ( 'publish' == $state ) // Publish is hard-coded above.
  2176. continue;
  2177. $where .= " OR $wpdb->posts.post_status = '$state'";
  2178. }
  2179. if ( $this->is_admin ) {
  2180. // Add protected states that should show in the admin all list.
  2181. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2182. foreach ( (array) $admin_all_states as $state )
  2183. $where .= " OR $wpdb->posts.post_status = '$state'";
  2184. }
  2185. if ( is_user_logged_in() ) {
  2186. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2187. $private_states = get_post_stati( array('private' => true) );
  2188. foreach ( (array) $private_states as $state )
  2189. $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'";
  2190. }
  2191. $where .= ')';
  2192. }
  2193. if ( !empty( $this->meta_query->queries ) ) {
  2194. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  2195. $join .= $clauses['join'];
  2196. $where .= $clauses['where'];
  2197. }
  2198. // Apply filters on where and join prior to paging so that any
  2199. // manipulations to them are reflected in the paging by day queries.
  2200. if ( !$q['suppress_filters'] ) {
  2201. $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
  2202. $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
  2203. }
  2204. // Paging
  2205. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2206. $page = absint($q['paged']);
  2207. if ( !$page )
  2208. $page = 1;
  2209. if ( empty($q['offset']) ) {
  2210. $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2211. } else { // we're ignoring $page and using 'offset'
  2212. $q['offset'] = absint($q['offset']);
  2213. $pgstrt = $q['offset'] . ', ';
  2214. }
  2215. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2216. }
  2217. // Comments feeds
  2218. if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2219. if ( $this->is_archive || $this->is_search ) {
  2220. $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  2221. $cwhere = "WHERE comment_approved = '1' $where";
  2222. $cgroupby = "$wpdb->comments.comment_id";
  2223. } else { // Other non singular e.g. front
  2224. $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  2225. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2226. $cgroupby = '';
  2227. }
  2228. if ( !$q['suppress_filters'] ) {
  2229. $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
  2230. $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
  2231. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
  2232. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2233. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2234. }
  2235. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2236. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2237. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2238. $this->comment_count = count($this->comments);
  2239. $post_ids = array();
  2240. foreach ( $this->comments as $comment )
  2241. $post_ids[] = (int) $comment->comment_post_ID;
  2242. $post_ids = join(',', $post_ids);
  2243. $join = '';
  2244. if ( $post_ids )
  2245. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2246. else
  2247. $where = "AND 0";
  2248. }
  2249. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2250. // Apply post-paging filters on where and join. Only plugins that
  2251. // manipulate paging queries should use these hooks.
  2252. if ( !$q['suppress_filters'] ) {
  2253. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2254. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2255. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2256. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2257. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2258. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2259. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2260. // Filter all clauses at once, for convenience
  2261. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2262. foreach ( $pieces as $piece )
  2263. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2264. }
  2265. // Announce current selection parameters. For use by caching plugins.
  2266. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2267. // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
  2268. if ( !$q['suppress_filters'] ) {
  2269. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2270. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2271. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2272. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2273. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2274. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2275. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2276. // Filter all clauses at once, for convenience
  2277. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
  2278. foreach ( $pieces as $piece )
  2279. $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
  2280. }
  2281. if ( ! empty($groupby) )
  2282. $groupby = 'GROUP BY ' . $groupby;
  2283. if ( !empty( $orderby ) )
  2284. $orderby = 'ORDER BY ' . $orderby;
  2285. $found_rows = '';
  2286. if ( !$q['no_found_rows'] && !empty($limits) )
  2287. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2288. $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2289. if ( !$q['suppress_filters'] ) {
  2290. $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
  2291. }
  2292. if ( 'ids' == $q['fields'] ) {
  2293. $this->posts = $wpdb->get_col($this->request);
  2294. return $this->posts;
  2295. }
  2296. if ( 'id=>parent' == $q['fields'] ) {
  2297. $this->posts = $wpdb->get_results($this->request);
  2298. $r = array();
  2299. foreach ( $this->posts as $post )
  2300. $r[ $post->ID ] = $post->post_parent;
  2301. return $r;
  2302. }
  2303. if ( $old_request == $this->request && "$wpdb->posts.*" == $fields ) {
  2304. // First get the IDs and then fill in the objects
  2305. $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2306. $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
  2307. $ids = $wpdb->get_col( $this->request );
  2308. if ( $ids ) {
  2309. $this->set_found_posts( $q, $limits );
  2310. _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
  2311. $this->posts = array_map( 'get_post', $ids );
  2312. } else {
  2313. $this->found_posts = $this->max_num_pages = 0;
  2314. $this->posts = array();
  2315. }
  2316. } else {
  2317. $this->posts = $wpdb->get_results( $this->request );
  2318. $this->set_found_posts( $q, $limits );
  2319. }
  2320. // Raw results filter. Prior to status checks.
  2321. if ( !$q['suppress_filters'] )
  2322. $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  2323. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2324. $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
  2325. $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2326. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
  2327. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2328. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2329. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2330. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2331. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2332. $this->comments = $wpdb->get_results($comments_request);
  2333. $this->comment_count = count($this->comments);
  2334. }
  2335. // Check post status to determine if post should be displayed.
  2336. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2337. $status = get_post_status($this->posts[0]);
  2338. $post_status_obj = get_post_status_object($status);
  2339. //$type = get_post_type($this->posts[0]);
  2340. if ( !$post_status_obj->public ) {
  2341. if ( ! is_user_logged_in() ) {
  2342. // User must be logged in to view unpublished posts.
  2343. $this->posts = array();
  2344. } else {
  2345. if ( $post_status_obj->protected ) {
  2346. // User must have edit permissions on the draft to preview.
  2347. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2348. $this->posts = array();
  2349. } else {
  2350. $this->is_preview = true;
  2351. if ( 'future' != $status )
  2352. $this->posts[0]->post_date = current_time('mysql');
  2353. }
  2354. } elseif ( $post_status_obj->private ) {
  2355. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2356. $this->posts = array();
  2357. } else {
  2358. $this->posts = array();
  2359. }
  2360. }
  2361. }
  2362. if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
  2363. $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
  2364. }
  2365. // Put sticky posts at the top of the posts array
  2366. $sticky_posts = get_option('sticky_posts');
  2367. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  2368. $num_posts = count($this->posts);
  2369. $sticky_offset = 0;
  2370. // Loop over posts and relocate stickies to the front.
  2371. for ( $i = 0; $i < $num_posts; $i++ ) {
  2372. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2373. $sticky_post = $this->posts[$i];
  2374. // Remove sticky from current position
  2375. array_splice($this->posts, $i, 1);
  2376. // Move to front, after other stickies
  2377. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2378. // Increment the sticky offset. The next sticky will be placed at this offset.
  2379. $sticky_offset++;
  2380. // Remove post from sticky posts array
  2381. $offset = array_search($sticky_post->ID, $sticky_posts);
  2382. unset( $sticky_posts[$offset] );
  2383. }
  2384. }
  2385. // If any posts have been excluded specifically, Ignore those that are sticky.
  2386. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  2387. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2388. // Fetch sticky posts that weren't in the query results
  2389. if ( !empty($sticky_posts) ) {
  2390. $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
  2391. // honor post type(s) if not set to any
  2392. $stickies_where = '';
  2393. if ( 'any' != $post_type && '' != $post_type ) {
  2394. if ( is_array( $post_type ) ) {
  2395. $post_types = join( "', '", $post_type );
  2396. } else {
  2397. $post_types = $post_type;
  2398. }
  2399. $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
  2400. }
  2401. $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
  2402. foreach ( $stickies as $sticky_post ) {
  2403. // Ignore sticky posts the current user cannot read or are not published.
  2404. if ( 'publish' != $sticky_post->post_status )
  2405. continue;
  2406. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2407. $sticky_offset++;
  2408. }
  2409. }
  2410. }
  2411. if ( !$q['suppress_filters'] )
  2412. $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
  2413. $this->post_count = count($this->posts);
  2414. // Always sanitize
  2415. foreach ( $this->posts as $i => $post ) {
  2416. $this->posts[$i] = sanitize_post( $post, 'raw' );
  2417. }
  2418. if ( $q['cache_results'] )
  2419. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2420. if ( $this->post_count > 0 ) {
  2421. $this->post = $this->posts[0];
  2422. }
  2423. return $this->posts;
  2424. }
  2425. function set_found_posts( $q, $limits ) {
  2426. global $wpdb;
  2427. if ( $q['no_found_rows'] || empty( $limits ) )
  2428. return;
  2429. $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
  2430. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  2431. $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
  2432. }
  2433. /**
  2434. * Set up the next post and iterate current post index.
  2435. *
  2436. * @since 1.5.0
  2437. * @access public
  2438. *
  2439. * @return object Next post.
  2440. */
  2441. function next_post() {
  2442. $this->current_post++;
  2443. $this->post = $this->posts[$this->current_post];
  2444. return $this->post;
  2445. }
  2446. /**
  2447. * Sets up the current post.
  2448. *
  2449. * Retrieves the next post, sets up the post, sets the 'in the loop'
  2450. * property to true.
  2451. *
  2452. * @since 1.5.0
  2453. * @access public
  2454. * @uses $post
  2455. * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
  2456. */
  2457. function the_post() {
  2458. global $post;
  2459. $this->in_the_loop = true;
  2460. if ( $this->current_post == -1 ) // loop has just started
  2461. do_action_ref_array('loop_start', array(&$this));
  2462. $post = $this->next_post();
  2463. setup_postdata($post);
  2464. }
  2465. /**
  2466. * Whether there are more posts available in the loop.
  2467. *
  2468. * Calls action 'loop_end', when the loop is complete.
  2469. *
  2470. * @since 1.5.0
  2471. * @access public
  2472. * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  2473. *
  2474. * @return bool True if posts are available, false if end of loop.
  2475. */
  2476. function have_posts() {
  2477. if ( $this->current_post + 1 < $this->post_count ) {
  2478. return true;
  2479. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  2480. do_action_ref_array('loop_end', array(&$this));
  2481. // Do some cleaning up after the loop
  2482. $this->rewind_posts();
  2483. }
  2484. $this->in_the_loop = false;
  2485. return false;
  2486. }
  2487. /**
  2488. * Rewind the posts and reset post index.
  2489. *
  2490. * @since 1.5.0
  2491. * @access public
  2492. */
  2493. function rewind_posts() {
  2494. $this->current_post = -1;
  2495. if ( $this->post_count > 0 ) {
  2496. $this->post = $this->posts[0];
  2497. }
  2498. }
  2499. /**
  2500. * Iterate current comment index and return comment object.
  2501. *
  2502. * @since 2.2.0
  2503. * @access public
  2504. *
  2505. * @return object Comment object.
  2506. */
  2507. function next_comment() {
  2508. $this->current_comment++;
  2509. $this->comment = $this->comments[$this->current_comment];
  2510. return $this->comment;
  2511. }
  2512. /**
  2513. * Sets up the current comment.
  2514. *
  2515. * @since 2.2.0
  2516. * @access public
  2517. * @global object $comment Current comment.
  2518. * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
  2519. */
  2520. function the_comment() {
  2521. global $comment;
  2522. $comment = $this->next_comment();
  2523. if ( $this->current_comment == 0 ) {
  2524. do_action('comment_loop_start');
  2525. }
  2526. }
  2527. /**
  2528. * Whether there are more comments available.
  2529. *
  2530. * Automatically rewinds comments when finished.
  2531. *
  2532. * @since 2.2.0
  2533. * @access public
  2534. *
  2535. * @return bool True, if more comments. False, if no more posts.
  2536. */
  2537. function have_comments() {
  2538. if ( $this->current_comment + 1 < $this->comment_count ) {
  2539. return true;
  2540. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  2541. $this->rewind_comments();
  2542. }
  2543. return false;
  2544. }
  2545. /**
  2546. * Rewind the comments, resets the comment index and comment to first.
  2547. *
  2548. * @since 2.2.0
  2549. * @access public
  2550. */
  2551. function rewind_comments() {
  2552. $this->current_comment = -1;
  2553. if ( $this->comment_count > 0 ) {
  2554. $this->comment = $this->comments[0];
  2555. }
  2556. }
  2557. /**
  2558. * Sets up the WordPress query by parsing query string.
  2559. *
  2560. * @since 1.5.0
  2561. * @access public
  2562. *
  2563. * @param string $query URL query string.
  2564. * @return array List of posts.
  2565. */
  2566. function &query( $query ) {
  2567. $this->init();
  2568. $this->query = $this->query_vars = wp_parse_args( $query );
  2569. return $this->get_posts();
  2570. }
  2571. /**
  2572. * Retrieve queried object.
  2573. *
  2574. * If queried object is not set, then the queried object will be set from
  2575. * the category, tag, taxonomy, posts page, single post, page, or author
  2576. * query variable. After it is set up, it will be returned.
  2577. *
  2578. * @since 1.5.0
  2579. * @access public
  2580. *
  2581. * @return object
  2582. */
  2583. function get_queried_object() {
  2584. if ( isset($this->queried_object) )
  2585. return $this->queried_object;
  2586. $this->queried_object = null;
  2587. $this->queried_object_id = 0;
  2588. if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  2589. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  2590. $query = reset( $tax_query_in_and );
  2591. if ( 'term_id' == $query['field'] )
  2592. $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
  2593. else
  2594. $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
  2595. if ( $term && ! is_wp_error($term) ) {
  2596. $this->queried_object = $term;
  2597. $this->queried_object_id = (int) $term->term_id;
  2598. if ( $this->is_category )
  2599. _make_cat_compat( $this->queried_object );
  2600. }
  2601. } elseif ( $this->is_post_type_archive ) {
  2602. $this->queried_object = get_post_type_object( $this->get('post_type') );
  2603. } elseif ( $this->is_posts_page ) {
  2604. $page_for_posts = get_option('page_for_posts');
  2605. $this->queried_object = & get_page( $page_for_posts );
  2606. $this->queried_object_id = (int) $this->queried_object->ID;
  2607. } elseif ( $this->is_singular && !is_null($this->post) ) {
  2608. $this->queried_object = $this->post;
  2609. $this->queried_object_id = (int) $this->post->ID;
  2610. } elseif ( $this->is_author ) {
  2611. $this->queried_object_id = (int) $this->get('author');
  2612. $this->queried_object = get_userdata( $this->queried_object_id );
  2613. }
  2614. return $this->queried_object;
  2615. }
  2616. /**
  2617. * Retrieve ID of the current queried object.
  2618. *
  2619. * @since 1.5.0
  2620. * @access public
  2621. *
  2622. * @return int
  2623. */
  2624. function get_queried_object_id() {
  2625. $this->get_queried_object();
  2626. if ( isset($this->queried_object_id) ) {
  2627. return $this->queried_object_id;
  2628. }
  2629. return 0;
  2630. }
  2631. /**
  2632. * Constructor.
  2633. *
  2634. * Sets up the WordPress query, if parameter is not empty.
  2635. *
  2636. * @since 1.5.0
  2637. * @access public
  2638. *
  2639. * @param string $query URL query string.
  2640. * @return WP_Query
  2641. */
  2642. function __construct($query = '') {
  2643. if ( ! empty($query) ) {
  2644. $this->query($query);
  2645. }
  2646. }
  2647. /**
  2648. * Is the query for an archive page?
  2649. *
  2650. * Month, Year, Category, Author, Post Type archive...
  2651. *
  2652. * @since 3.1.0
  2653. *
  2654. * @return bool
  2655. */
  2656. function is_archive() {
  2657. return (bool) $this->is_archive;
  2658. }
  2659. /**
  2660. * Is the query for a post type archive page?
  2661. *
  2662. * @since 3.1.0
  2663. *
  2664. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  2665. * @return bool
  2666. */
  2667. function is_post_type_archive( $post_types = '' ) {
  2668. if ( empty( $post_types ) || !$this->is_post_type_archive )
  2669. return (bool) $this->is_post_type_archive;
  2670. $post_type_object = $this->get_queried_object();
  2671. return in_array( $post_type_object->name, (array) $post_types );
  2672. }
  2673. /**
  2674. * Is the query for an attachment page?
  2675. *
  2676. * @since 3.1.0
  2677. *
  2678. * @return bool
  2679. */
  2680. function is_attachment() {
  2681. return (bool) $this->is_attachment;
  2682. }
  2683. /**
  2684. * Is the query for an author archive page?
  2685. *
  2686. * If the $author parameter is specified, this function will additionally
  2687. * check if the query is for one of the authors specified.
  2688. *
  2689. * @since 3.1.0
  2690. *
  2691. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  2692. * @return bool
  2693. */
  2694. function is_author( $author = '' ) {
  2695. if ( !$this->is_author )
  2696. return false;
  2697. if ( empty($author) )
  2698. return true;
  2699. $author_obj = $this->get_queried_object();
  2700. $author = (array) $author;
  2701. if ( in_array( $author_obj->ID, $author ) )
  2702. return true;
  2703. elseif ( in_array( $author_obj->nickname, $author ) )
  2704. return true;
  2705. elseif ( in_array( $author_obj->user_nicename, $author ) )
  2706. return true;
  2707. return false;
  2708. }
  2709. /**
  2710. * Is the query for a category archive page?
  2711. *
  2712. * If the $category parameter is specified, this function will additionally
  2713. * check if the query is for one of the categories specified.
  2714. *
  2715. * @since 3.1.0
  2716. *
  2717. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  2718. * @return bool
  2719. */
  2720. function is_category( $category = '' ) {
  2721. if ( !$this->is_category )
  2722. return false;
  2723. if ( empty($category) )
  2724. return true;
  2725. $cat_obj = $this->get_queried_object();
  2726. $category = (array) $category;
  2727. if ( in_array( $cat_obj->term_id, $category ) )
  2728. return true;
  2729. elseif ( in_array( $cat_obj->name, $category ) )
  2730. return true;
  2731. elseif ( in_array( $cat_obj->slug, $category ) )
  2732. return true;
  2733. return false;
  2734. }
  2735. /**
  2736. * Is the query for a tag archive page?
  2737. *
  2738. * If the $tag parameter is specified, this function will additionally
  2739. * check if the query is for one of the tags specified.
  2740. *
  2741. * @since 3.1.0
  2742. *
  2743. * @param mixed $slug Optional. Tag slug or array of slugs.
  2744. * @return bool
  2745. */
  2746. function is_tag( $slug = '' ) {
  2747. if ( !$this->is_tag )
  2748. return false;
  2749. if ( empty( $slug ) )
  2750. return true;
  2751. $tag_obj = $this->get_queried_object();
  2752. $slug = (array) $slug;
  2753. if ( in_array( $tag_obj->slug, $slug ) )
  2754. return true;
  2755. return false;
  2756. }
  2757. /**
  2758. * Is the query for a taxonomy archive page?
  2759. *
  2760. * If the $taxonomy parameter is specified, this function will additionally
  2761. * check if the query is for that specific $taxonomy.
  2762. *
  2763. * If the $term parameter is specified in addition to the $taxonomy parameter,
  2764. * this function will additionally check if the query is for one of the terms
  2765. * specified.
  2766. *
  2767. * @since 3.1.0
  2768. *
  2769. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  2770. * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  2771. * @return bool
  2772. */
  2773. function is_tax( $taxonomy = '', $term = '' ) {
  2774. global $wp_taxonomies;
  2775. if ( !$this->is_tax )
  2776. return false;
  2777. if ( empty( $taxonomy ) )
  2778. return true;
  2779. $queried_object = $this->get_queried_object();
  2780. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
  2781. $term_array = (array) $term;
  2782. if ( empty( $term ) ) // Only a Taxonomy provided
  2783. return isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array );
  2784. return isset( $queried_object->term_id ) &&
  2785. count( array_intersect(
  2786. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
  2787. $term_array
  2788. ) );
  2789. }
  2790. /**
  2791. * Whether the current URL is within the comments popup window.
  2792. *
  2793. * @since 3.1.0
  2794. *
  2795. * @return bool
  2796. */
  2797. function is_comments_popup() {
  2798. return (bool) $this->is_comments_popup;
  2799. }
  2800. /**
  2801. * Is the query for a date archive?
  2802. *
  2803. * @since 3.1.0
  2804. *
  2805. * @return bool
  2806. */
  2807. function is_date() {
  2808. return (bool) $this->is_date;
  2809. }
  2810. /**
  2811. * Is the query for a day archive?
  2812. *
  2813. * @since 3.1.0
  2814. *
  2815. * @return bool
  2816. */
  2817. function is_day() {
  2818. return (bool) $this->is_day;
  2819. }
  2820. /**
  2821. * Is the query for a feed?
  2822. *
  2823. * @since 3.1.0
  2824. *
  2825. * @param string|array $feeds Optional feed types to check.
  2826. * @return bool
  2827. */
  2828. function is_feed( $feeds = '' ) {
  2829. if ( empty( $feeds ) || ! $this->is_feed )
  2830. return (bool) $this->is_feed;
  2831. $qv = $this->get( 'feed' );
  2832. if ( 'feed' == $qv )
  2833. $qv = get_default_feed();
  2834. return in_array( $qv, (array) $feeds );
  2835. }
  2836. /**
  2837. * Is the query for a comments feed?
  2838. *
  2839. * @since 3.1.0
  2840. *
  2841. * @return bool
  2842. */
  2843. function is_comment_feed() {
  2844. return (bool) $this->is_comment_feed;
  2845. }
  2846. /**
  2847. * Is the query for the front page of the site?
  2848. *
  2849. * This is for what is displayed at your site's main URL.
  2850. *
  2851. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  2852. *
  2853. * If you set a static page for the front page of your site, this function will return
  2854. * true when viewing that page.
  2855. *
  2856. * Otherwise the same as @see WP_Query::is_home()
  2857. *
  2858. * @since 3.1.0
  2859. * @uses is_home()
  2860. * @uses get_option()
  2861. *
  2862. * @return bool True, if front of site.
  2863. */
  2864. function is_front_page() {
  2865. // most likely case
  2866. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
  2867. return true;
  2868. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
  2869. return true;
  2870. else
  2871. return false;
  2872. }
  2873. /**
  2874. * Is the query for the blog homepage?
  2875. *
  2876. * This is the page which shows the time based blog content of your site.
  2877. *
  2878. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  2879. *
  2880. * If you set a static page for the front page of your site, this function will return
  2881. * true only on the page you set as the "Posts page".
  2882. *
  2883. * @see WP_Query::is_front_page()
  2884. *
  2885. * @since 3.1.0
  2886. *
  2887. * @return bool True if blog view homepage.
  2888. */
  2889. function is_home() {
  2890. return (bool) $this->is_home;
  2891. }
  2892. /**
  2893. * Is the query for a month archive?
  2894. *
  2895. * @since 3.1.0
  2896. *
  2897. * @return bool
  2898. */
  2899. function is_month() {
  2900. return (bool) $this->is_month;
  2901. }
  2902. /**
  2903. * Is the query for a single page?
  2904. *
  2905. * If the $page parameter is specified, this function will additionally
  2906. * check if the query is for one of the pages specified.
  2907. *
  2908. * @see WP_Query::is_single()
  2909. * @see WP_Query::is_singular()
  2910. *
  2911. * @since 3.1.0
  2912. *
  2913. * @param mixed $page Page ID, title, slug, or array of such.
  2914. * @return bool
  2915. */
  2916. function is_page( $page = '' ) {
  2917. if ( !$this->is_page )
  2918. return false;
  2919. if ( empty( $page ) )
  2920. return true;
  2921. $page_obj = $this->get_queried_object();
  2922. $page = (array) $page;
  2923. if ( in_array( $page_obj->ID, $page ) )
  2924. return true;
  2925. elseif ( in_array( $page_obj->post_title, $page ) )
  2926. return true;
  2927. else if ( in_array( $page_obj->post_name, $page ) )
  2928. return true;
  2929. return false;
  2930. }
  2931. /**
  2932. * Is the query for paged result and not for the first page?
  2933. *
  2934. * @since 3.1.0
  2935. *
  2936. * @return bool
  2937. */
  2938. function is_paged() {
  2939. return (bool) $this->is_paged;
  2940. }
  2941. /**
  2942. * Is the query for a post or page preview?
  2943. *
  2944. * @since 3.1.0
  2945. *
  2946. * @return bool
  2947. */
  2948. function is_preview() {
  2949. return (bool) $this->is_preview;
  2950. }
  2951. /**
  2952. * Is the query for the robots file?
  2953. *
  2954. * @since 3.1.0
  2955. *
  2956. * @return bool
  2957. */
  2958. function is_robots() {
  2959. return (bool) $this->is_robots;
  2960. }
  2961. /**
  2962. * Is the query for a search?
  2963. *
  2964. * @since 3.1.0
  2965. *
  2966. * @return bool
  2967. */
  2968. function is_search() {
  2969. return (bool) $this->is_search;
  2970. }
  2971. /**
  2972. * Is the query for a single post?
  2973. *
  2974. * Works for any post type, except attachments and pages
  2975. *
  2976. * If the $post parameter is specified, this function will additionally
  2977. * check if the query is for one of the Posts specified.
  2978. *
  2979. * @see WP_Query::is_page()
  2980. * @see WP_Query::is_singular()
  2981. *
  2982. * @since 3.1.0
  2983. *
  2984. * @param mixed $post Post ID, title, slug, or array of such.
  2985. * @return bool
  2986. */
  2987. function is_single( $post = '' ) {
  2988. if ( !$this->is_single )
  2989. return false;
  2990. if ( empty($post) )
  2991. return true;
  2992. $post_obj = $this->get_queried_object();
  2993. $post = (array) $post;
  2994. if ( in_array( $post_obj->ID, $post ) )
  2995. return true;
  2996. elseif ( in_array( $post_obj->post_title, $post ) )
  2997. return true;
  2998. elseif ( in_array( $post_obj->post_name, $post ) )
  2999. return true;
  3000. return false;
  3001. }
  3002. /**
  3003. * Is the query for a single post of any post type (post, attachment, page, ... )?
  3004. *
  3005. * If the $post_types parameter is specified, this function will additionally
  3006. * check if the query is for one of the Posts Types specified.
  3007. *
  3008. * @see WP_Query::is_page()
  3009. * @see WP_Query::is_single()
  3010. *
  3011. * @since 3.1.0
  3012. *
  3013. * @param mixed $post_types Optional. Post Type or array of Post Types
  3014. * @return bool
  3015. */
  3016. function is_singular( $post_types = '' ) {
  3017. if ( empty( $post_types ) || !$this->is_singular )
  3018. return (bool) $this->is_singular;
  3019. $post_obj = $this->get_queried_object();
  3020. return in_array( $post_obj->post_type, (array) $post_types );
  3021. }
  3022. /**
  3023. * Is the query for a specific time?
  3024. *
  3025. * @since 3.1.0
  3026. *
  3027. * @return bool
  3028. */
  3029. function is_time() {
  3030. return (bool) $this->is_time;
  3031. }
  3032. /**
  3033. * Is the query for a trackback endpoint call?
  3034. *
  3035. * @since 3.1.0
  3036. *
  3037. * @return bool
  3038. */
  3039. function is_trackback() {
  3040. return (bool) $this->is_trackback;
  3041. }
  3042. /**
  3043. * Is the query for a specific year?
  3044. *
  3045. * @since 3.1.0
  3046. *
  3047. * @return bool
  3048. */
  3049. function is_year() {
  3050. return (bool) $this->is_year;
  3051. }
  3052. /**
  3053. * Is the query a 404 (returns no results)?
  3054. *
  3055. * @since 3.1.0
  3056. *
  3057. * @return bool
  3058. */
  3059. function is_404() {
  3060. return (bool) $this->is_404;
  3061. }
  3062. /**
  3063. * Is the query the main query?
  3064. *
  3065. * @since 3.3.0
  3066. *
  3067. * @return bool
  3068. */
  3069. function is_main_query() {
  3070. global $wp_the_query;
  3071. return $wp_the_query === $this;
  3072. }
  3073. }
  3074. /**
  3075. * Redirect old slugs to the correct permalink.
  3076. *
  3077. * Attempts to find the current slug from the past slugs.
  3078. *
  3079. * @since 2.1.0
  3080. * @uses $wp_query
  3081. * @uses $wpdb
  3082. *
  3083. * @return null If no link is found, null is returned.
  3084. */
  3085. function wp_old_slug_redirect() {
  3086. global $wp_query;
  3087. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  3088. global $wpdb;
  3089. // Guess the current post_type based on the query vars.
  3090. if ( get_query_var('post_type') )
  3091. $post_type = get_query_var('post_type');
  3092. elseif ( !empty($wp_query->query_vars['pagename']) )
  3093. $post_type = 'page';
  3094. else
  3095. $post_type = 'post';
  3096. if ( is_array( $post_type ) ) {
  3097. if ( count( $post_type ) > 1 )
  3098. return;
  3099. $post_type = array_shift( $post_type );
  3100. }
  3101. // Do not attempt redirect for hierarchical post types
  3102. if ( is_post_type_hierarchical( $post_type ) )
  3103. return;
  3104. $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']);
  3105. // if year, monthnum, or day have been specified, make our query more precise
  3106. // just in case there are multiple identical _wp_old_slug values
  3107. if ( '' != $wp_query->query_vars['year'] )
  3108. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
  3109. if ( '' != $wp_query->query_vars['monthnum'] )
  3110. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
  3111. if ( '' != $wp_query->query_vars['day'] )
  3112. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
  3113. $id = (int) $wpdb->get_var($query);
  3114. if ( ! $id )
  3115. return;
  3116. $link = get_permalink($id);
  3117. if ( !$link )
  3118. return;
  3119. wp_redirect( $link, 301 ); // Permanent redirect
  3120. exit;
  3121. endif;
  3122. }
  3123. /**
  3124. * Set up global post data.
  3125. *
  3126. * @since 1.5.0
  3127. *
  3128. * @param object $post Post data.
  3129. * @uses do_action_ref_array() Calls 'the_post'
  3130. * @return bool True when finished.
  3131. */
  3132. function setup_postdata($post) {
  3133. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  3134. $id = (int) $post->ID;
  3135. $authordata = get_userdata($post->post_author);
  3136. $currentday = mysql2date('d.m.y', $post->post_date, false);
  3137. $currentmonth = mysql2date('m', $post->post_date, false);
  3138. $numpages = 1;
  3139. $page = get_query_var('page');
  3140. if ( !$page )
  3141. $page = 1;
  3142. if ( is_single() || is_page() || is_feed() )
  3143. $more = 1;
  3144. $content = $post->post_content;
  3145. if ( strpos( $content, '<!--nextpage-->' ) ) {
  3146. if ( $page > 1 )
  3147. $more = 1;
  3148. $multipage = 1;
  3149. $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
  3150. $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  3151. $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  3152. $pages = explode('<!--nextpage-->', $content);
  3153. $numpages = count($pages);
  3154. } else {
  3155. $pages = array( $post->post_content );
  3156. $multipage = 0;
  3157. }
  3158. do_action_ref_array('the_post', array(&$post));
  3159. return true;
  3160. }