PageRenderTime 69ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/wordpress/wp-includes/query.php

http://ownerpress.googlecode.com/
PHP | 2485 lines | 1273 code | 305 blank | 907 comment | 384 complexity | 912300712bd0622ca1c804de65d591fb MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.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. * The Loop. Post loop control.
  630. */
  631. /**
  632. * Whether current WordPress query has results to loop over.
  633. *
  634. * @see WP_Query::have_posts()
  635. * @since 1.5.0
  636. * @uses $wp_query
  637. *
  638. * @return bool
  639. */
  640. function have_posts() {
  641. global $wp_query;
  642. return $wp_query->have_posts();
  643. }
  644. /**
  645. * Whether the caller is in the Loop.
  646. *
  647. * @since 2.0.0
  648. * @uses $wp_query
  649. *
  650. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  651. */
  652. function in_the_loop() {
  653. global $wp_query;
  654. return $wp_query->in_the_loop;
  655. }
  656. /**
  657. * Rewind the loop posts.
  658. *
  659. * @see WP_Query::rewind_posts()
  660. * @since 1.5.0
  661. * @uses $wp_query
  662. *
  663. * @return null
  664. */
  665. function rewind_posts() {
  666. global $wp_query;
  667. return $wp_query->rewind_posts();
  668. }
  669. /**
  670. * Iterate the post index in the loop.
  671. *
  672. * @see WP_Query::the_post()
  673. * @since 1.5.0
  674. * @uses $wp_query
  675. */
  676. function the_post() {
  677. global $wp_query;
  678. $wp_query->the_post();
  679. }
  680. /*
  681. * Comments loop.
  682. */
  683. /**
  684. * Whether there are comments to loop over.
  685. *
  686. * @see WP_Query::have_comments()
  687. * @since 2.2.0
  688. * @uses $wp_query
  689. *
  690. * @return bool
  691. */
  692. function have_comments() {
  693. global $wp_query;
  694. return $wp_query->have_comments();
  695. }
  696. /**
  697. * Iterate comment index in the comment loop.
  698. *
  699. * @see WP_Query::the_comment()
  700. * @since 2.2.0
  701. * @uses $wp_query
  702. *
  703. * @return object
  704. */
  705. function the_comment() {
  706. global $wp_query;
  707. return $wp_query->the_comment();
  708. }
  709. /*
  710. * WP_Query
  711. */
  712. /**
  713. * The WordPress Query class.
  714. *
  715. * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  716. *
  717. * @since 1.5.0
  718. */
  719. class WP_Query {
  720. /**
  721. * Query vars set by the user
  722. *
  723. * @since 1.5.0
  724. * @access public
  725. * @var array
  726. */
  727. var $query;
  728. /**
  729. * Query vars, after parsing
  730. *
  731. * @since 1.5.0
  732. * @access public
  733. * @var array
  734. */
  735. var $query_vars = array();
  736. /**
  737. * Taxonomy query, as passed to get_tax_sql()
  738. *
  739. * @since 3.1.0
  740. * @access public
  741. * @var object WP_Tax_Query
  742. */
  743. var $tax_query;
  744. /**
  745. * Holds the data for a single object that is queried.
  746. *
  747. * Holds the contents of a post, page, category, attachment.
  748. *
  749. * @since 1.5.0
  750. * @access public
  751. * @var object|array
  752. */
  753. var $queried_object;
  754. /**
  755. * The ID of the queried object.
  756. *
  757. * @since 1.5.0
  758. * @access public
  759. * @var int
  760. */
  761. var $queried_object_id;
  762. /**
  763. * Get post database query.
  764. *
  765. * @since 2.0.1
  766. * @access public
  767. * @var string
  768. */
  769. var $request;
  770. /**
  771. * List of posts.
  772. *
  773. * @since 1.5.0
  774. * @access public
  775. * @var array
  776. */
  777. var $posts;
  778. /**
  779. * The amount of posts for the current query.
  780. *
  781. * @since 1.5.0
  782. * @access public
  783. * @var int
  784. */
  785. var $post_count = 0;
  786. /**
  787. * Index of the current item in the loop.
  788. *
  789. * @since 1.5.0
  790. * @access public
  791. * @var int
  792. */
  793. var $current_post = -1;
  794. /**
  795. * Whether the loop has started and the caller is in the loop.
  796. *
  797. * @since 2.0.0
  798. * @access public
  799. * @var bool
  800. */
  801. var $in_the_loop = false;
  802. /**
  803. * The current post ID.
  804. *
  805. * @since 1.5.0
  806. * @access public
  807. * @var object
  808. */
  809. var $post;
  810. /**
  811. * The list of comments for current post.
  812. *
  813. * @since 2.2.0
  814. * @access public
  815. * @var array
  816. */
  817. var $comments;
  818. /**
  819. * The amount of comments for the posts.
  820. *
  821. * @since 2.2.0
  822. * @access public
  823. * @var int
  824. */
  825. var $comment_count = 0;
  826. /**
  827. * The index of the comment in the comment loop.
  828. *
  829. * @since 2.2.0
  830. * @access public
  831. * @var int
  832. */
  833. var $current_comment = -1;
  834. /**
  835. * Current comment ID.
  836. *
  837. * @since 2.2.0
  838. * @access public
  839. * @var int
  840. */
  841. var $comment;
  842. /**
  843. * Amount of posts if limit clause was not used.
  844. *
  845. * @since 2.1.0
  846. * @access public
  847. * @var int
  848. */
  849. var $found_posts = 0;
  850. /**
  851. * The amount of pages.
  852. *
  853. * @since 2.1.0
  854. * @access public
  855. * @var int
  856. */
  857. var $max_num_pages = 0;
  858. /**
  859. * The amount of comment pages.
  860. *
  861. * @since 2.7.0
  862. * @access public
  863. * @var int
  864. */
  865. var $max_num_comment_pages = 0;
  866. /**
  867. * Set if query is single post.
  868. *
  869. * @since 1.5.0
  870. * @access public
  871. * @var bool
  872. */
  873. var $is_single = false;
  874. /**
  875. * Set if query is preview of blog.
  876. *
  877. * @since 2.0.0
  878. * @access public
  879. * @var bool
  880. */
  881. var $is_preview = false;
  882. /**
  883. * Set if query returns a page.
  884. *
  885. * @since 1.5.0
  886. * @access public
  887. * @var bool
  888. */
  889. var $is_page = false;
  890. /**
  891. * Set if query is an archive list.
  892. *
  893. * @since 1.5.0
  894. * @access public
  895. * @var bool
  896. */
  897. var $is_archive = false;
  898. /**
  899. * Set if query is part of a date.
  900. *
  901. * @since 1.5.0
  902. * @access public
  903. * @var bool
  904. */
  905. var $is_date = false;
  906. /**
  907. * Set if query contains a year.
  908. *
  909. * @since 1.5.0
  910. * @access public
  911. * @var bool
  912. */
  913. var $is_year = false;
  914. /**
  915. * Set if query contains a month.
  916. *
  917. * @since 1.5.0
  918. * @access public
  919. * @var bool
  920. */
  921. var $is_month = false;
  922. /**
  923. * Set if query contains a day.
  924. *
  925. * @since 1.5.0
  926. * @access public
  927. * @var bool
  928. */
  929. var $is_day = false;
  930. /**
  931. * Set if query contains time.
  932. *
  933. * @since 1.5.0
  934. * @access public
  935. * @var bool
  936. */
  937. var $is_time = false;
  938. /**
  939. * Set if query contains an author.
  940. *
  941. * @since 1.5.0
  942. * @access public
  943. * @var bool
  944. */
  945. var $is_author = false;
  946. /**
  947. * Set if query contains category.
  948. *
  949. * @since 1.5.0
  950. * @access public
  951. * @var bool
  952. */
  953. var $is_category = false;
  954. /**
  955. * Set if query contains tag.
  956. *
  957. * @since 2.3.0
  958. * @access public
  959. * @var bool
  960. */
  961. var $is_tag = false;
  962. /**
  963. * Set if query contains taxonomy.
  964. *
  965. * @since 2.5.0
  966. * @access public
  967. * @var bool
  968. */
  969. var $is_tax = false;
  970. /**
  971. * Set if query was part of a search result.
  972. *
  973. * @since 1.5.0
  974. * @access public
  975. * @var bool
  976. */
  977. var $is_search = false;
  978. /**
  979. * Set if query is feed display.
  980. *
  981. * @since 1.5.0
  982. * @access public
  983. * @var bool
  984. */
  985. var $is_feed = false;
  986. /**
  987. * Set if query is comment feed display.
  988. *
  989. * @since 2.2.0
  990. * @access public
  991. * @var bool
  992. */
  993. var $is_comment_feed = false;
  994. /**
  995. * Set if query is trackback.
  996. *
  997. * @since 1.5.0
  998. * @access public
  999. * @var bool
  1000. */
  1001. var $is_trackback = false;
  1002. /**
  1003. * Set if query is blog homepage.
  1004. *
  1005. * @since 1.5.0
  1006. * @access public
  1007. * @var bool
  1008. */
  1009. var $is_home = false;
  1010. /**
  1011. * Set if query couldn't found anything.
  1012. *
  1013. * @since 1.5.0
  1014. * @access public
  1015. * @var bool
  1016. */
  1017. var $is_404 = false;
  1018. /**
  1019. * Set if query is within comments popup window.
  1020. *
  1021. * @since 1.5.0
  1022. * @access public
  1023. * @var bool
  1024. */
  1025. var $is_comments_popup = false;
  1026. /**
  1027. * Set if query is paged
  1028. *
  1029. * @since 1.5.0
  1030. * @access public
  1031. * @var bool
  1032. */
  1033. var $is_paged = false;
  1034. /**
  1035. * Set if query is part of administration page.
  1036. *
  1037. * @since 1.5.0
  1038. * @access public
  1039. * @var bool
  1040. */
  1041. var $is_admin = false;
  1042. /**
  1043. * Set if query is an attachment.
  1044. *
  1045. * @since 2.0.0
  1046. * @access public
  1047. * @var bool
  1048. */
  1049. var $is_attachment = false;
  1050. /**
  1051. * Set if is single, is a page, or is an attachment.
  1052. *
  1053. * @since 2.1.0
  1054. * @access public
  1055. * @var bool
  1056. */
  1057. var $is_singular = false;
  1058. /**
  1059. * Set if query is for robots.
  1060. *
  1061. * @since 2.1.0
  1062. * @access public
  1063. * @var bool
  1064. */
  1065. var $is_robots = false;
  1066. /**
  1067. * Set if query contains posts.
  1068. *
  1069. * Basically, the homepage if the option isn't set for the static homepage.
  1070. *
  1071. * @since 2.1.0
  1072. * @access public
  1073. * @var bool
  1074. */
  1075. var $is_posts_page = false;
  1076. /**
  1077. * Set if query is for a post type archive.
  1078. *
  1079. * @since 3.1.0
  1080. * @access public
  1081. * @var bool
  1082. */
  1083. var $is_post_type_archive = false;
  1084. /**
  1085. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1086. * whether we have to re-parse because something has changed
  1087. *
  1088. * @since 3.1.0
  1089. * @access private
  1090. */
  1091. var $query_vars_hash = false;
  1092. /**
  1093. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1094. * via pre_get_posts hooks.
  1095. *
  1096. * @since 3.1.1
  1097. * @access private
  1098. */
  1099. var $query_vars_changed = true;
  1100. /**
  1101. * Resets query flags to false.
  1102. *
  1103. * The query flags are what page info WordPress was able to figure out.
  1104. *
  1105. * @since 2.0.0
  1106. * @access private
  1107. */
  1108. function init_query_flags() {
  1109. $this->is_single = false;
  1110. $this->is_preview = false;
  1111. $this->is_page = false;
  1112. $this->is_archive = false;
  1113. $this->is_date = false;
  1114. $this->is_year = false;
  1115. $this->is_month = false;
  1116. $this->is_day = false;
  1117. $this->is_time = false;
  1118. $this->is_author = false;
  1119. $this->is_category = false;
  1120. $this->is_tag = false;
  1121. $this->is_tax = false;
  1122. $this->is_search = false;
  1123. $this->is_feed = false;
  1124. $this->is_comment_feed = false;
  1125. $this->is_trackback = false;
  1126. $this->is_home = false;
  1127. $this->is_404 = false;
  1128. $this->is_comments_popup = false;
  1129. $this->is_paged = false;
  1130. $this->is_admin = false;
  1131. $this->is_attachment = false;
  1132. $this->is_singular = false;
  1133. $this->is_robots = false;
  1134. $this->is_posts_page = false;
  1135. $this->is_post_type_archive = false;
  1136. }
  1137. /**
  1138. * Initiates object properties and sets default values.
  1139. *
  1140. * @since 1.5.0
  1141. * @access public
  1142. */
  1143. function init() {
  1144. unset($this->posts);
  1145. unset($this->query);
  1146. $this->query_vars = array();
  1147. unset($this->queried_object);
  1148. unset($this->queried_object_id);
  1149. $this->post_count = 0;
  1150. $this->current_post = -1;
  1151. $this->in_the_loop = false;
  1152. unset( $this->request );
  1153. unset( $this->post );
  1154. unset( $this->comments );
  1155. unset( $this->comment );
  1156. $this->comment_count = 0;
  1157. $this->current_comment = -1;
  1158. $this->found_posts = 0;
  1159. $this->max_num_pages = 0;
  1160. $this->max_num_comment_pages = 0;
  1161. $this->init_query_flags();
  1162. }
  1163. /**
  1164. * Reparse the query vars.
  1165. *
  1166. * @since 1.5.0
  1167. * @access public
  1168. */
  1169. function parse_query_vars() {
  1170. $this->parse_query();
  1171. }
  1172. /**
  1173. * Fills in the query variables, which do not exist within the parameter.
  1174. *
  1175. * @since 2.1.0
  1176. * @access public
  1177. *
  1178. * @param array $array Defined query variables.
  1179. * @return array Complete query variables with undefined ones filled in empty.
  1180. */
  1181. function fill_query_vars($array) {
  1182. $keys = array(
  1183. 'error'
  1184. , 'm'
  1185. , 'p'
  1186. , 'post_parent'
  1187. , 'subpost'
  1188. , 'subpost_id'
  1189. , 'attachment'
  1190. , 'attachment_id'
  1191. , 'name'
  1192. , 'static'
  1193. , 'pagename'
  1194. , 'page_id'
  1195. , 'second'
  1196. , 'minute'
  1197. , 'hour'
  1198. , 'day'
  1199. , 'monthnum'
  1200. , 'year'
  1201. , 'w'
  1202. , 'category_name'
  1203. , 'tag'
  1204. , 'cat'
  1205. , 'tag_id'
  1206. , 'author_name'
  1207. , 'feed'
  1208. , 'tb'
  1209. , 'paged'
  1210. , 'comments_popup'
  1211. , 'meta_key'
  1212. , 'meta_value'
  1213. , 'preview'
  1214. , 's'
  1215. , 'sentence'
  1216. , 'fields'
  1217. );
  1218. foreach ( $keys as $key ) {
  1219. if ( !isset($array[$key]) )
  1220. $array[$key] = '';
  1221. }
  1222. $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1223. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  1224. foreach ( $array_keys as $key ) {
  1225. if ( !isset($array[$key]) )
  1226. $array[$key] = array();
  1227. }
  1228. return $array;
  1229. }
  1230. /**
  1231. * Parse a query string and set query type booleans.
  1232. *
  1233. * @since 1.5.0
  1234. * @access public
  1235. *
  1236. * @param string|array $query Optional query.
  1237. */
  1238. function parse_query( $query = '' ) {
  1239. if ( ! empty( $query ) ) {
  1240. $this->init();
  1241. $this->query = $this->query_vars = wp_parse_args( $query );
  1242. } elseif ( ! isset( $this->query ) ) {
  1243. $this->query = $this->query_vars;
  1244. }
  1245. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1246. $qv = &$this->query_vars;
  1247. $this->query_vars_changed = true;
  1248. if ( ! empty($qv['robots']) )
  1249. $this->is_robots = true;
  1250. $qv['p'] = absint($qv['p']);
  1251. $qv['page_id'] = absint($qv['page_id']);
  1252. $qv['year'] = absint($qv['year']);
  1253. $qv['monthnum'] = absint($qv['monthnum']);
  1254. $qv['day'] = absint($qv['day']);
  1255. $qv['w'] = absint($qv['w']);
  1256. $qv['m'] = absint($qv['m']);
  1257. $qv['paged'] = absint($qv['paged']);
  1258. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1259. $qv['pagename'] = trim( $qv['pagename'] );
  1260. $qv['name'] = trim( $qv['name'] );
  1261. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1262. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1263. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1264. // Compat. Map subpost to attachment.
  1265. if ( '' != $qv['subpost'] )
  1266. $qv['attachment'] = $qv['subpost'];
  1267. if ( '' != $qv['subpost_id'] )
  1268. $qv['attachment_id'] = $qv['subpost_id'];
  1269. $qv['attachment_id'] = absint($qv['attachment_id']);
  1270. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1271. $this->is_single = true;
  1272. $this->is_attachment = true;
  1273. } elseif ( '' != $qv['name'] ) {
  1274. $this->is_single = true;
  1275. } elseif ( $qv['p'] ) {
  1276. $this->is_single = true;
  1277. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1278. // If year, month, day, hour, minute, and second are set, a single
  1279. // post is being queried.
  1280. $this->is_single = true;
  1281. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1282. $this->is_page = true;
  1283. $this->is_single = false;
  1284. } else {
  1285. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1286. if ( !empty($qv['s']) ) {
  1287. $this->is_search = true;
  1288. }
  1289. if ( '' !== $qv['second'] ) {
  1290. $this->is_time = true;
  1291. $this->is_date = true;
  1292. }
  1293. if ( '' !== $qv['minute'] ) {
  1294. $this->is_time = true;
  1295. $this->is_date = true;
  1296. }
  1297. if ( '' !== $qv['hour'] ) {
  1298. $this->is_time = true;
  1299. $this->is_date = true;
  1300. }
  1301. if ( $qv['day'] ) {
  1302. if ( ! $this->is_date ) {
  1303. $this->is_day = true;
  1304. $this->is_date = true;
  1305. }
  1306. }
  1307. if ( $qv['monthnum'] ) {
  1308. if ( ! $this->is_date ) {
  1309. $this->is_month = true;
  1310. $this->is_date = true;
  1311. }
  1312. }
  1313. if ( $qv['year'] ) {
  1314. if ( ! $this->is_date ) {
  1315. $this->is_year = true;
  1316. $this->is_date = true;
  1317. }
  1318. }
  1319. if ( $qv['m'] ) {
  1320. $this->is_date = true;
  1321. if ( strlen($qv['m']) > 9 ) {
  1322. $this->is_time = true;
  1323. } else if ( strlen($qv['m']) > 7 ) {
  1324. $this->is_day = true;
  1325. } else if ( strlen($qv['m']) > 5 ) {
  1326. $this->is_month = true;
  1327. } else {
  1328. $this->is_year = true;
  1329. }
  1330. }
  1331. if ( '' != $qv['w'] ) {
  1332. $this->is_date = true;
  1333. }
  1334. $this->query_vars_hash = false;
  1335. $this->parse_tax_query( $qv );
  1336. foreach ( $this->tax_query->queries as $tax_query ) {
  1337. if ( 'IN' == $tax_query['operator'] ) {
  1338. switch ( $tax_query['taxonomy'] ) {
  1339. case 'category':
  1340. $this->is_category = true;
  1341. break;
  1342. case 'post_tag':
  1343. $this->is_tag = true;
  1344. break;
  1345. default:
  1346. $this->is_tax = true;
  1347. }
  1348. }
  1349. }
  1350. unset( $tax_query );
  1351. _parse_meta_query( $qv );
  1352. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1353. $this->is_author = false;
  1354. } else {
  1355. $this->is_author = true;
  1356. }
  1357. if ( '' != $qv['author_name'] )
  1358. $this->is_author = true;
  1359. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1360. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1361. if ( ! empty( $post_type_obj->has_archive ) )
  1362. $this->is_post_type_archive = true;
  1363. }
  1364. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1365. $this->is_archive = true;
  1366. }
  1367. if ( '' != $qv['feed'] )
  1368. $this->is_feed = true;
  1369. if ( '' != $qv['tb'] )
  1370. $this->is_trackback = true;
  1371. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1372. $this->is_paged = true;
  1373. if ( '' != $qv['comments_popup'] )
  1374. $this->is_comments_popup = true;
  1375. // if we're previewing inside the write screen
  1376. if ( '' != $qv['preview'] )
  1377. $this->is_preview = true;
  1378. if ( is_admin() )
  1379. $this->is_admin = true;
  1380. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1381. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1382. $qv['withcomments'] = 1;
  1383. }
  1384. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1385. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1386. $this->is_comment_feed = true;
  1387. 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 ) )
  1388. $this->is_home = true;
  1389. // Correct is_* for page_on_front and page_for_posts
  1390. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1391. $_query = wp_parse_args($this->query);
  1392. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1393. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1394. unset($_query['pagename']);
  1395. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1396. $this->is_page = true;
  1397. $this->is_home = false;
  1398. $qv['page_id'] = get_option('page_on_front');
  1399. // Correct <!--nextpage--> for page_on_front
  1400. if ( !empty($qv['paged']) ) {
  1401. $qv['page'] = $qv['paged'];
  1402. unset($qv['paged']);
  1403. }
  1404. }
  1405. }
  1406. if ( '' != $qv['pagename'] ) {
  1407. $this->queried_object =& get_page_by_path($qv['pagename']);
  1408. if ( !empty($this->queried_object) )
  1409. $this->queried_object_id = (int) $this->queried_object->ID;
  1410. else
  1411. unset($this->queried_object);
  1412. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1413. $this->is_page = false;
  1414. $this->is_home = true;
  1415. $this->is_posts_page = true;
  1416. }
  1417. }
  1418. if ( $qv['page_id'] ) {
  1419. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1420. $this->is_page = false;
  1421. $this->is_home = true;
  1422. $this->is_posts_page = true;
  1423. }
  1424. }
  1425. if ( !empty($qv['post_type']) ) {
  1426. if ( is_array($qv['post_type']) )
  1427. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1428. else
  1429. $qv['post_type'] = sanitize_key($qv['post_type']);
  1430. }
  1431. if ( !empty($qv['post_status']) )
  1432. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1433. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1434. $this->is_comment_feed = false;
  1435. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1436. // Done correcting is_* for page_on_front and page_for_posts
  1437. if ( '404' == $qv['error'] )
  1438. $this->set_404();
  1439. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1440. $this->query_vars_changed = false;
  1441. do_action_ref_array('parse_query', array(&$this));
  1442. }
  1443. /*
  1444. * Parses various taxonomy related query vars.
  1445. *
  1446. * @access protected
  1447. * @since 3.1.0
  1448. *
  1449. * @param array &$q The query variables
  1450. */
  1451. function parse_tax_query( &$q ) {
  1452. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1453. $tax_query = $q['tax_query'];
  1454. } else {
  1455. $tax_query = array();
  1456. }
  1457. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1458. $tax_query[] = array(
  1459. 'taxonomy' => $q['taxonomy'],
  1460. 'terms' => array( $q['term'] ),
  1461. 'field' => 'slug',
  1462. );
  1463. }
  1464. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1465. if ( 'post_tag' == $taxonomy )
  1466. continue; // Handled further down in the $q['tag'] block
  1467. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1468. $tax_query_defaults = array(
  1469. 'taxonomy' => $taxonomy,
  1470. 'field' => 'slug',
  1471. );
  1472. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1473. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1474. }
  1475. $term = $q[$t->query_var];
  1476. if ( strpos($term, '+') !== false ) {
  1477. $terms = preg_split( '/[+]+/', $term );
  1478. foreach ( $terms as $term ) {
  1479. $tax_query[] = array_merge( $tax_query_defaults, array(
  1480. 'terms' => array( $term )
  1481. ) );
  1482. }
  1483. } else {
  1484. $tax_query[] = array_merge( $tax_query_defaults, array(
  1485. 'terms' => preg_split( '/[,]+/', $term )
  1486. ) );
  1487. }
  1488. }
  1489. }
  1490. // Category stuff
  1491. if ( !empty($q['cat']) && '0' != $q['cat'] && !$this->is_singular && $this->query_vars_changed ) {
  1492. $q['cat'] = ''.urldecode($q['cat']).'';
  1493. $q['cat'] = addslashes_gpc($q['cat']);
  1494. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  1495. $q['cat'] = '';
  1496. $req_cats = array();
  1497. foreach ( (array) $cat_array as $cat ) {
  1498. $cat = intval($cat);
  1499. $req_cats[] = $cat;
  1500. $in = ($cat > 0);
  1501. $cat = abs($cat);
  1502. if ( $in ) {
  1503. $q['category__in'][] = $cat;
  1504. $q['category__in'] = array_merge( $q['category__in'], get_term_children($cat, 'category') );
  1505. } else {
  1506. $q['category__not_in'][] = $cat;
  1507. $q['category__not_in'] = array_merge( $q['category__not_in'], get_term_children($cat, 'category') );
  1508. }
  1509. }
  1510. $q['cat'] = implode(',', $req_cats);
  1511. }
  1512. if ( !empty($q['category__in']) ) {
  1513. $q['category__in'] = array_map('absint', array_unique( (array) $q['category__in'] ) );
  1514. $tax_query[] = array(
  1515. 'taxonomy' => 'category',
  1516. 'terms' => $q['category__in'],
  1517. 'field' => 'term_id',
  1518. 'include_children' => false
  1519. );
  1520. }
  1521. if ( !empty($q['category__not_in']) ) {
  1522. $q['category__not_in'] = array_map('absint', array_unique( (array) $q['category__not_in'] ) );
  1523. $tax_query[] = array(
  1524. 'taxonomy' => 'category',
  1525. 'terms' => $q['category__not_in'],
  1526. 'operator' => 'NOT IN',
  1527. 'include_children' => false
  1528. );
  1529. }
  1530. if ( !empty($q['category__and']) ) {
  1531. $q['category__and'] = array_map('absint', array_unique( (array) $q['category__and'] ) );
  1532. $tax_query[] = array(
  1533. 'taxonomy' => 'category',
  1534. 'terms' => $q['category__and'],
  1535. 'field' => 'term_id',
  1536. 'operator' => 'AND',
  1537. 'include_children' => false
  1538. );
  1539. }
  1540. // Tag stuff
  1541. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1542. if ( strpos($q['tag'], ',') !== false ) {
  1543. $tags = preg_split('/[,\s]+/', $q['tag']);
  1544. foreach ( (array) $tags as $tag ) {
  1545. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1546. $q['tag_slug__in'][] = $tag;
  1547. }
  1548. } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
  1549. $tags = preg_split('/[+\s]+/', $q['tag']);
  1550. foreach ( (array) $tags as $tag ) {
  1551. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1552. $q['tag_slug__and'][] = $tag;
  1553. }
  1554. } else {
  1555. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1556. $q['tag_slug__in'][] = $q['tag'];
  1557. }
  1558. }
  1559. if ( !empty($q['tag_id']) ) {
  1560. $q['tag_id'] = absint( $q['tag_id'] );
  1561. $tax_query[] = array(
  1562. 'taxonomy' => 'post_tag',
  1563. 'terms' => $q['tag_id']
  1564. );
  1565. }
  1566. if ( !empty($q['tag__in']) ) {
  1567. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1568. $tax_query[] = array(
  1569. 'taxonomy' => 'post_tag',
  1570. 'terms' => $q['tag__in']
  1571. );
  1572. }
  1573. if ( !empty($q['tag__not_in']) ) {
  1574. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1575. $tax_query[] = array(
  1576. 'taxonomy' => 'post_tag',
  1577. 'terms' => $q['tag__not_in'],
  1578. 'operator' => 'NOT IN'
  1579. );
  1580. }
  1581. if ( !empty($q['tag__and']) ) {
  1582. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1583. $tax_query[] = array(
  1584. 'taxonomy' => 'post_tag',
  1585. 'terms' => $q['tag__and'],
  1586. 'operator' => 'AND'
  1587. );
  1588. }
  1589. if ( !empty($q['tag_slug__in']) ) {
  1590. $q['tag_slug__in'] = array_map('sanitize_title', array_unique( (array) $q['tag_slug__in'] ) );
  1591. $tax_query[] = array(
  1592. 'taxonomy' => 'post_tag',
  1593. 'terms' => $q['tag_slug__in'],
  1594. 'field' => 'slug'
  1595. );
  1596. }
  1597. if ( !empty($q['tag_slug__and']) ) {
  1598. $q['tag_slug__and'] = array_map('sanitize_title', array_unique( (array) $q['tag_slug__and'] ) );
  1599. $tax_query[] = array(
  1600. 'taxonomy' => 'post_tag',
  1601. 'terms' => $q['tag_slug__and'],
  1602. 'field' => 'slug',
  1603. 'operator' => 'AND'
  1604. );
  1605. }
  1606. $this->tax_query = new WP_Tax_Query( $tax_query );
  1607. }
  1608. /**
  1609. * Sets the 404 property and saves whether query is feed.
  1610. *
  1611. * @since 2.0.0
  1612. * @access public
  1613. */
  1614. function set_404() {
  1615. $is_feed = $this->is_feed;
  1616. $this->init_query_flags();
  1617. $this->is_404 = true;
  1618. $this->is_feed = $is_feed;
  1619. }
  1620. /**
  1621. * Retrieve query variable.
  1622. *
  1623. * @since 1.5.0
  1624. * @access public
  1625. *
  1626. * @param string $query_var Query variable key.
  1627. * @return mixed
  1628. */
  1629. function get($query_var) {
  1630. if ( isset($this->query_vars[$query_var]) )
  1631. return $this->query_vars[$query_var];
  1632. return '';
  1633. }
  1634. /**
  1635. * Set query variable.
  1636. *
  1637. * @since 1.5.0
  1638. * @access public
  1639. *
  1640. * @param string $query_var Query variable key.
  1641. * @param mixed $value Query variable value.
  1642. */
  1643. function set($query_var, $value) {
  1644. $this->query_vars[$query_var] = $value;
  1645. }
  1646. /**
  1647. * Retrieve the posts based on query variables.
  1648. *
  1649. * There are a few filters and actions that can be used to modify the post
  1650. * database query.
  1651. *
  1652. * @since 1.5.0
  1653. * @access public
  1654. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1655. *
  1656. * @return array List of posts.
  1657. */
  1658. function &get_posts() {
  1659. global $wpdb, $user_ID, $_wp_using_ext_object_cache;
  1660. $this->parse_query();
  1661. do_action_ref_array('pre_get_posts', array(&$this));
  1662. // Shorthand.
  1663. $q = &$this->query_vars;
  1664. // Fill again in case pre_get_posts unset some vars.
  1665. $q = $this->fill_query_vars($q);
  1666. // Set a flag if a pre_get_posts hook changed the query vars.
  1667. $hash = md5( serialize( $this->query_vars ) );
  1668. if ( $hash != $this->query_vars_hash ) {
  1669. $this->query_vars_changed = true;
  1670. $this->query_vars_hash = $hash;
  1671. }
  1672. unset($hash);
  1673. // First let's clear some variables
  1674. $distinct = '';
  1675. $whichauthor = '';
  1676. $whichmimetype = '';
  1677. $where = '';
  1678. $limits = '';
  1679. $join = '';
  1680. $search = '';
  1681. $groupby = '';
  1682. $fields = '';
  1683. $post_status_join = false;
  1684. $page = 1;
  1685. if ( isset( $q['caller_get_posts'] ) ) {
  1686. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  1687. if ( !isset( $q['ignore_sticky_posts'] ) )
  1688. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1689. }
  1690. if ( !isset( $q['ignore_sticky_posts'] ) )
  1691. $q['ignore_sticky_posts'] = false;
  1692. if ( !isset($q['suppress_filters']) )
  1693. $q['suppress_filters'] = false;
  1694. if ( !isset($q['cache_results']) ) {
  1695. if ( $_wp_using_ext_object_cache )
  1696. $q['cache_results'] = false;
  1697. else
  1698. $q['cache_results'] = true;
  1699. }
  1700. if ( !isset($q['update_post_term_cache']) )
  1701. $q['update_post_term_cache'] = true;
  1702. if ( !isset($q['update_post_meta_cache']) )
  1703. $q['update_post_meta_cache'] = true;
  1704. if ( !isset($q['post_type']) ) {
  1705. if ( $this->is_search )
  1706. $q['post_type'] = 'any';
  1707. else
  1708. $q['post_type'] = '';
  1709. }
  1710. $post_type = $q['post_type'];
  1711. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  1712. $q['posts_per_page'] = get_option('posts_per_page');
  1713. if ( isset($q['showposts']) && $q['showposts'] ) {
  1714. $q['showposts'] = (int) $q['showposts'];
  1715. $q['posts_per_page'] = $q['showposts'];
  1716. }
  1717. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1718. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1719. if ( !isset($q['nopaging']) ) {
  1720. if ( $q['posts_per_page'] == -1 ) {
  1721. $q['nopaging'] = true;
  1722. } else {
  1723. $q['nopaging'] = false;
  1724. }
  1725. }
  1726. if ( $this->is_feed ) {
  1727. $q['posts_per_page'] = get_option('posts_per_rss');
  1728. $q['nopaging'] = false;
  1729. }
  1730. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1731. if ( $q['posts_per_page'] < -1 )
  1732. $q['posts_per_page'] = abs($q['posts_per_page']);
  1733. else if ( $q['posts_per_page'] == 0 )
  1734. $q['posts_per_page'] = 1;
  1735. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1736. $q['comments_per_page'] = get_option('comments_per_page');
  1737. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1738. $this->is_page = true;
  1739. $this->is_home = false;
  1740. $q['page_id'] = get_option('page_on_front');
  1741. }
  1742. if ( isset($q['page']) ) {
  1743. $q['page'] = trim($q['page'], '/');
  1744. $q['page'] = absint($q['page']);
  1745. }
  1746. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1747. if ( isset($q['no_found_rows']) )
  1748. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1749. else
  1750. $q['no_found_rows'] = false;
  1751. switch ( $q['fields'] ) {
  1752. case 'ids':
  1753. $fields = "$wpdb->posts.ID";
  1754. break;
  1755. case 'id=>parent':
  1756. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  1757. break;
  1758. default:
  1759. $fields = "$wpdb->posts.*";
  1760. }
  1761. // If a month is specified in the querystring, load that month
  1762. if ( $q['m'] ) {
  1763. $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  1764. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  1765. if ( strlen($q['m']) > 5 )
  1766. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  1767. if ( strlen($q['m']) > 7 )
  1768. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  1769. if ( strlen($q['m']) > 9 )
  1770. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  1771. if ( strlen($q['m']) > 11 )
  1772. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  1773. if ( strlen($q['m']) > 13 )
  1774. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  1775. }
  1776. if ( '' !== $q['hour'] )
  1777. $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
  1778. if ( '' !== $q['minute'] )
  1779. $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
  1780. if ( '' !== $q['second'] )
  1781. $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
  1782. if ( $q['year'] )
  1783. $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
  1784. if ( $q['monthnum'] )
  1785. $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
  1786. if ( $q['day'] )
  1787. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
  1788. // If we've got a post_type AND its not "any" post_type.
  1789. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  1790. foreach ( (array)$q['post_type'] as $_post_type ) {
  1791. $ptype_obj = get_post_type_object($_post_type);
  1792. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  1793. continue;
  1794. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  1795. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  1796. $q['name'] = $q[ $ptype_obj->query_var ];
  1797. } else {
  1798. // Hierarchical post_types will operate through the
  1799. $q['pagename'] = $q[ $ptype_obj->query_var ];
  1800. $q['name'] = '';
  1801. }
  1802. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  1803. break;
  1804. } //end foreach
  1805. unset($ptype_obj);
  1806. }
  1807. if ( '' != $q['name'] ) {
  1808. $q['name'] = sanitize_title_for_query( $q['name'] );
  1809. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  1810. } elseif ( '' != $q['pagename'] ) {
  1811. if ( isset($this->queried_object_id) ) {
  1812. $reqpage = $this->queried_object_id;
  1813. } else {
  1814. if ( 'page' != $q['post_type'] ) {
  1815. foreach ( (array)$q['post_type'] as $_post_type ) {
  1816. $ptype_obj = get_post_type_object($_post_type);
  1817. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  1818. continue;
  1819. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  1820. if ( $reqpage )
  1821. break;
  1822. }
  1823. unset($ptype_obj);
  1824. } else {
  1825. $reqpage = get_page_by_path($q['pagename']);
  1826. }
  1827. if ( !empty($reqpage) )
  1828. $reqpage = $reqpage->ID;
  1829. else
  1830. $reqpage = 0;
  1831. }
  1832. $page_for_posts = get_option('page_for_posts');
  1833. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  1834. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  1835. $q['name'] = $q['pagename'];
  1836. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  1837. $reqpage_obj = get_page($reqpage);
  1838. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  1839. $this->is_attachment = true;
  1840. $post_type = $q['post_type'] = 'attachment';
  1841. $this->is_page = true;
  1842. $q['attachment_id'] = $reqpage;
  1843. }
  1844. }
  1845. } elseif ( '' != $q['attachment'] ) {
  1846. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  1847. $q['name'] = $q['attachment'];
  1848. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  1849. }
  1850. if ( $q['w'] )
  1851. $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
  1852. if ( intval($q['comments_popup']) )
  1853. $q['p'] = absint($q['comments_popup']);
  1854. // If an attachment is requested by number, let it supercede any post number.
  1855. if ( $q['attachment_id'] )
  1856. $q['p'] = absint($q['attachment_id']);
  1857. // If a post number is specified, load that post
  1858. if ( $q['p'] ) {
  1859. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  1860. } elseif ( $q['post__in'] ) {
  1861. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  1862. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  1863. } elseif ( $q['post__not_in'] ) {
  1864. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  1865. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  1866. }
  1867. if ( is_numeric($q['post_parent']) )
  1868. $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  1869. if ( $q['page_id'] ) {
  1870. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  1871. $q['p'] = $q['page_id'];
  1872. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  1873. }
  1874. }
  1875. // If a search pattern is specified, load the posts that match
  1876. if ( !empty($q['s']) ) {
  1877. // added slashes screw with quote grouping when done early, so done later
  1878. $q['s'] = stripslashes($q['s']);
  1879. if ( !empty($q['sentence']) ) {
  1880. $q['search_terms'] = array($q['s']);
  1881. } else {
  1882. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
  1883. $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
  1884. }
  1885. $n = !empty($q['exact']) ? '' : '%';
  1886. $searchand = '';
  1887. foreach( (array) $q['search_terms'] as $term ) {
  1888. $term = esc_sql( like_escape( $term ) );
  1889. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  1890. $searchand = ' AND ';
  1891. }
  1892. $term = esc_sql( like_escape( $q['s'] ) );
  1893. if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
  1894. $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
  1895. if ( !empty($search) ) {
  1896. $search = " AND ({$search}) ";
  1897. if ( !is_user_logged_in() )
  1898. $search .= " AND ($wpdb->posts.post_password = '') ";
  1899. }
  1900. }
  1901. // Allow plugins to contextually add/remove/modify the search section of the database query
  1902. $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
  1903. // Taxonomies
  1904. if ( !$this->is_singular ) {
  1905. $this->parse_tax_query( $q );
  1906. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
  1907. $join .= $clauses['join'];
  1908. $where .= $clauses['where'];
  1909. }
  1910. if ( $this->is_tax ) {
  1911. if ( empty($post_type) ) {
  1912. $post_type = 'any';
  1913. $post_status_join = true;
  1914. } elseif ( in_array('attachment', (array) $post_type) ) {
  1915. $post_status_join = true;
  1916. }
  1917. }
  1918. // Back-compat
  1919. if ( !empty($this->tax_query->queries) ) {
  1920. $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  1921. if ( !empty( $tax_query_in_and ) ) {
  1922. if ( !isset( $q['taxonomy'] ) ) {
  1923. foreach ( $tax_query_in_and as $a_tax_query ) {
  1924. if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
  1925. $q['taxonomy'] = $a_tax_query['taxonomy'];
  1926. if ( 'slug' == $a_tax_query['field'] )
  1927. $q['term'] = $a_tax_query['terms'][0];
  1928. else
  1929. $q['term_id'] = $a_tax_query['terms'][0];
  1930. break;
  1931. }
  1932. }
  1933. }
  1934. $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
  1935. if ( !empty( $cat_query ) ) {
  1936. $cat_query = reset( $cat_query );
  1937. $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
  1938. if ( $the_cat ) {
  1939. $this->set( 'cat', $the_cat->term_id );
  1940. $this->set( 'category_name', $the_cat->slug );
  1941. }
  1942. unset( $the_cat );
  1943. }
  1944. unset( $cat_query );
  1945. $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
  1946. if ( !empty( $tag_query ) ) {
  1947. $tag_query = reset( $tag_query );
  1948. $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
  1949. if ( $the_tag ) {
  1950. $this->set( 'tag_id', $the_tag->term_id );
  1951. }
  1952. unset( $the_tag );
  1953. }
  1954. unset( $tag_query );
  1955. }
  1956. }
  1957. if ( !empty( $this->tax_query->queries ) || !empty( $q['meta_key'] ) ) {
  1958. $groupby = "{$wpdb->posts}.ID";
  1959. }
  1960. // Author/user stuff
  1961. if ( empty($q['author']) || ($q['author'] == '0') ) {
  1962. $whichauthor = '';
  1963. } else {
  1964. $q['author'] = (string)urldecode($q['author']);
  1965. $q['author'] = addslashes_gpc($q['author']);
  1966. if ( strpos($q['author'], '-') !== false ) {
  1967. $eq = '!=';
  1968. $andor = 'AND';
  1969. $q['author'] = explode('-', $q['author']);
  1970. $q['author'] = (string)absint($q['author'][1]);
  1971. } else {
  1972. $eq = '=';
  1973. $andor = 'OR';
  1974. }
  1975. $author_array = preg_split('/[,\s]+/', $q['author']);
  1976. $_author_array = array();
  1977. foreach ( $author_array as $key => $_author )
  1978. $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
  1979. $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
  1980. unset($author_array, $_author_array);
  1981. }
  1982. // Author stuff for nice URLs
  1983. if ( '' != $q['author_name'] ) {
  1984. if ( strpos($q['author_name'], '/') !== false ) {
  1985. $q['author_name'] = explode('/', $q['author_name']);
  1986. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  1987. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  1988. } else {
  1989. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailling slash
  1990. }
  1991. }
  1992. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  1993. $q['author'] = get_user_by('slug', $q['author_name']);
  1994. if ( $q['author'] )
  1995. $q['author'] = $q['author']->ID;
  1996. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  1997. }
  1998. // MIME-Type stuff for attachment browsing
  1999. if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
  2000. $table_alias = $post_status_join ? $wpdb->posts : '';
  2001. $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
  2002. }
  2003. $where .= $search . $whichauthor . $whichmimetype;
  2004. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  2005. $q['order'] = 'DESC';
  2006. // Order by
  2007. if ( empty($q['orderby']) ) {
  2008. $q['orderby'] = "$wpdb->posts.post_date " . $q['order'];
  2009. } elseif ( 'none' == $q['orderby'] ) {
  2010. $q['orderby'] = '';
  2011. } else {
  2012. // Used to filter values
  2013. $allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  2014. if ( !empty($q['meta_key']) ) {
  2015. $allowed_keys[] = $q['meta_key'];
  2016. $allowed_keys[] = 'meta_value';
  2017. $allowed_keys[] = 'meta_value_num';
  2018. }
  2019. $q['orderby'] = urldecode($q['orderby']);
  2020. $q['orderby'] = addslashes_gpc($q['orderby']);
  2021. $orderby_array = explode(' ', $q['orderby']);
  2022. $q['orderby'] = '';
  2023. foreach ( $orderby_array as $i => $orderby ) {
  2024. // Only allow certain values for safety
  2025. if ( ! in_array($orderby, $allowed_keys) )
  2026. continue;
  2027. switch ( $orderby ) {
  2028. case 'menu_order':
  2029. break;
  2030. case 'ID':
  2031. $orderby = "$wpdb->posts.ID";
  2032. break;
  2033. case 'rand':
  2034. $orderby = 'RAND()';
  2035. break;
  2036. case $q['meta_key']:
  2037. case 'meta_value':
  2038. $orderby = "$wpdb->postmeta.meta_value";
  2039. break;
  2040. case 'meta_value_num':
  2041. $orderby = "$wpdb->postmeta.meta_value+0";
  2042. break;
  2043. case 'comment_count':
  2044. $orderby = "$wpdb->posts.comment_count";
  2045. break;
  2046. default:
  2047. $orderby = "$wpdb->posts.post_" . $orderby;
  2048. }
  2049. $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
  2050. }
  2051. // append ASC or DESC at the end
  2052. if ( !empty($q['orderby']))
  2053. $q['orderby'] .= " {$q['order']}";
  2054. if ( empty($q['orderby']) )
  2055. $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
  2056. }
  2057. if ( is_array( $post_type ) ) {
  2058. $post_type_cap = 'multiple_post_type';
  2059. } else {
  2060. $post_type_object = get_post_type_object( $post_type );
  2061. if ( empty( $post_type_object ) )
  2062. $post_type_cap = $post_type;
  2063. }
  2064. $exclude_post_types = '';
  2065. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  2066. if ( ! empty( $in_search_post_types ) )
  2067. $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
  2068. if ( 'any' == $post_type ) {
  2069. $where .= $exclude_post_types;
  2070. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2071. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2072. } elseif ( ! empty( $post_type ) ) {
  2073. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  2074. $post_type_object = get_post_type_object ( $post_type );
  2075. } elseif ( $this->is_attachment ) {
  2076. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  2077. $post_type_object = get_post_type_object ( 'attachment' );
  2078. } elseif ( $this->is_page ) {
  2079. $where .= " AND $wpdb->posts.post_type = 'page'";
  2080. $post_type_object = get_post_type_object ( 'page' );
  2081. } else {
  2082. $where .= " AND $wpdb->posts.post_type = 'post'";
  2083. $post_type_object = get_post_type_object ( 'post' );
  2084. }
  2085. if ( ! empty( $post_type_object ) ) {
  2086. $edit_cap = $post_type_object->cap->edit_post;
  2087. $read_cap = $post_type_object->cap->read_post;
  2088. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  2089. $read_private_cap = $post_type_object->cap->read_private_posts;
  2090. } else {
  2091. $edit_cap = 'edit_' . $post_type_cap;
  2092. $read_cap = 'read_' . $post_type_cap;
  2093. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  2094. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  2095. }
  2096. if ( isset($q['post_status']) && '' != $q['post_status'] ) {
  2097. $statuswheres = array();
  2098. $q_status = explode(',', $q['post_status']);
  2099. $r_status = array();
  2100. $p_status = array();
  2101. $e_status = array();
  2102. if ( $q['post_status'] == 'any' ) {
  2103. foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  2104. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  2105. } else {
  2106. foreach ( get_post_stati() as $status ) {
  2107. if ( in_array( $status, $q_status ) ) {
  2108. if ( 'private' == $status )
  2109. $p_status[] = "$wpdb->posts.post_status = '$status'";
  2110. else
  2111. $r_status[] = "$wpdb->posts.post_status = '$status'";
  2112. }
  2113. }
  2114. }
  2115. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2116. $r_status = array_merge($r_status, $p_status);
  2117. unset($p_status);
  2118. }
  2119. if ( !empty($e_status) ) {
  2120. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2121. }
  2122. if ( !empty($r_status) ) {
  2123. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2124. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
  2125. else
  2126. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2127. }
  2128. if ( !empty($p_status) ) {
  2129. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2130. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
  2131. else
  2132. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2133. }
  2134. if ( $post_status_join ) {
  2135. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2136. foreach ( $statuswheres as $index => $statuswhere )
  2137. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  2138. }
  2139. foreach ( $statuswheres as $statuswhere )
  2140. $where .= " AND $statuswhere";
  2141. } elseif ( !$this->is_singular ) {
  2142. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  2143. // Add public states.
  2144. $public_states = get_post_stati( array('public' => true) );
  2145. foreach ( (array) $public_states as $state ) {
  2146. if ( 'publish' == $state ) // Publish is hard-coded above.
  2147. continue;
  2148. $where .= " OR $wpdb->posts.post_status = '$state'";
  2149. }
  2150. if ( is_admin() ) {
  2151. // Add protected states that should show in the admin all list.
  2152. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2153. foreach ( (array) $admin_all_states as $state )
  2154. $where .= " OR $wpdb->posts.post_status = '$state'";
  2155. }
  2156. if ( is_user_logged_in() ) {
  2157. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2158. $private_states = get_post_stati( array('private' => true) );
  2159. foreach ( (array) $private_states as $state )
  2160. $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'";
  2161. }
  2162. $where .= ')';
  2163. }
  2164. // Parse the meta query again if query vars have changed.
  2165. if ( $this->query_vars_changed ) {
  2166. $meta_query_hash = md5( serialize( $q['meta_query'] ) );
  2167. $_meta_query = $q['meta_query'];
  2168. unset( $q['meta_query'] );
  2169. _parse_meta_query( $q );
  2170. if ( md5( serialize( $q['meta_query'] ) ) != $meta_query_hash && is_array( $_meta_query ) )
  2171. $q['meta_query'] = array_merge( $_meta_query, $q['meta_query'] );
  2172. }
  2173. if ( !empty( $q['meta_query'] ) ) {
  2174. $clauses = call_user_func_array( '_get_meta_sql', array( $q['meta_query'], 'post', $wpdb->posts, 'ID', &$this) );
  2175. $join .= $clauses['join'];
  2176. $where .= $clauses['where'];
  2177. }
  2178. // Apply filters on where and join prior to paging so that any
  2179. // manipulations to them are reflected in the paging by day queries.
  2180. if ( !$q['suppress_filt