PageRenderTime 259ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/query.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 2881 lines | 1556 code | 369 blank | 956 comment | 526 complexity | bed0c8ed635ba6e62db2036cc420e2c7 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * WordPress Query API
  4. *
  5. * The query API attempts to get which part of WordPress to the user is on. It
  6. * also provides functionality to getting URL query information.
  7. *
  8. * @link http://codex.wordpress.org/The_Loop More information on The Loop.
  9. *
  10. * @package WordPress
  11. * @subpackage Query
  12. */
  13. /**
  14. * Retrieve variable in the WP_Query class.
  15. *
  16. * @see WP_Query::get()
  17. * @since 1.5.0
  18. * @uses $wp_query
  19. *
  20. * @param string $var The variable key to retrieve.
  21. * @return mixed
  22. */
  23. function get_query_var($var) {
  24. global $wp_query;
  25. return $wp_query->get($var);
  26. }
  27. /**
  28. * Set query variable.
  29. *
  30. * @see WP_Query::set()
  31. * @since 2.2.0
  32. * @uses $wp_query
  33. *
  34. * @param string $var Query variable key.
  35. * @param mixed $value
  36. * @return null
  37. */
  38. function set_query_var($var, $value) {
  39. global $wp_query;
  40. return $wp_query->set($var, $value);
  41. }
  42. /**
  43. * Set up The Loop with query parameters.
  44. *
  45. * This will override the current WordPress Loop and shouldn't be used more than
  46. * once. This must not be used within the WordPress Loop.
  47. *
  48. * @since 1.5.0
  49. * @uses $wp_query
  50. *
  51. * @param string $query
  52. * @return array List of posts
  53. */
  54. function &query_posts($query) {
  55. unset($GLOBALS['wp_query']);
  56. $GLOBALS['wp_query'] =& new WP_Query();
  57. return $GLOBALS['wp_query']->query($query);
  58. }
  59. /**
  60. * Destroy the previous query and set up a new query.
  61. *
  62. * This should be used after {@link query_posts()} and before another {@link
  63. * query_posts()}. This will remove obscure bugs that occur when the previous
  64. * wp_query object is not destroyed properly before another is set up.
  65. *
  66. * @since 2.3.0
  67. * @uses $wp_query
  68. */
  69. function wp_reset_query() {
  70. unset($GLOBALS['wp_query']);
  71. $GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
  72. wp_reset_postdata();
  73. }
  74. /**
  75. * After looping through a separate query, this function restores
  76. * the $post global to the current post in the main query
  77. *
  78. * @since 3.0.0
  79. * @uses $wp_query
  80. */
  81. function wp_reset_postdata() {
  82. global $wp_query;
  83. if ( !empty($wp_query->post) ) {
  84. $GLOBALS['post'] = $wp_query->post;
  85. setup_postdata($wp_query->post);
  86. }
  87. }
  88. /*
  89. * Query type checks.
  90. */
  91. /**
  92. * Is query requesting an archive page.
  93. *
  94. * @since 1.5.0
  95. * @uses $wp_query
  96. *
  97. * @return bool True if page is archive.
  98. */
  99. function is_archive() {
  100. global $wp_query;
  101. return $wp_query->is_archive;
  102. }
  103. /**
  104. * Is query requesting an attachment page.
  105. *
  106. * @since 2.0.0
  107. * @uses $wp_query
  108. *
  109. * @return bool True if page is attachment.
  110. */
  111. function is_attachment() {
  112. global $wp_query;
  113. return $wp_query->is_attachment;
  114. }
  115. /**
  116. * Is query requesting an author page.
  117. *
  118. * If the $author parameter is specified then the check will be expanded to
  119. * include whether the queried author matches the one given in the parameter.
  120. * You can match against integers and against strings.
  121. *
  122. * If matching against an integer, the ID should be used of the author for the
  123. * test. If the $author is an ID and matches the author page user ID, then
  124. * 'true' will be returned.
  125. *
  126. * If matching against strings, then the test will be matched against both the
  127. * nickname and user nicename and will return true on success.
  128. *
  129. * @since 1.5.0
  130. * @uses $wp_query
  131. *
  132. * @param string|int $author Optional. Is current page this author.
  133. * @return bool True if page is author or $author (if set).
  134. */
  135. function is_author($author = '') {
  136. global $wp_query;
  137. if ( !$wp_query->is_author )
  138. return false;
  139. if ( empty($author) )
  140. return true;
  141. $author_obj = $wp_query->get_queried_object();
  142. $author = (array) $author;
  143. if ( in_array( $author_obj->ID, $author ) )
  144. return true;
  145. elseif ( in_array( $author_obj->nickname, $author ) )
  146. return true;
  147. elseif ( in_array( $author_obj->user_nicename, $author ) )
  148. return true;
  149. return false;
  150. }
  151. /**
  152. * Whether current page query contains a category name or given category name.
  153. *
  154. * The category list can contain category IDs, names, or category slugs. If any
  155. * of them are part of the query, then it will return true.
  156. *
  157. * @since 1.5.0
  158. * @uses $wp_query
  159. *
  160. * @param string|array $category Optional.
  161. * @return bool
  162. */
  163. function is_category($category = '') {
  164. global $wp_query;
  165. if ( !$wp_query->is_category )
  166. return false;
  167. if ( empty($category) )
  168. return true;
  169. $cat_obj = $wp_query->get_queried_object();
  170. $category = (array) $category;
  171. if ( in_array( $cat_obj->term_id, $category ) )
  172. return true;
  173. elseif ( in_array( $cat_obj->name, $category ) )
  174. return true;
  175. elseif ( in_array( $cat_obj->slug, $category ) )
  176. return true;
  177. return false;
  178. }
  179. /**
  180. * Whether the current page query has the given tag slug or contains tag.
  181. *
  182. * @since 2.3.0
  183. * @uses $wp_query
  184. *
  185. * @param string|array $slug Optional. Single tag or list of tags to check for.
  186. * @return bool
  187. */
  188. function is_tag( $slug = '' ) {
  189. global $wp_query;
  190. if ( !$wp_query->is_tag )
  191. return false;
  192. if ( empty( $slug ) )
  193. return true;
  194. $tag_obj = $wp_query->get_queried_object();
  195. $slug = (array) $slug;
  196. if ( in_array( $tag_obj->slug, $slug ) )
  197. return true;
  198. return false;
  199. }
  200. /**
  201. * Whether the current query is for the given taxonomy and/or term.
  202. *
  203. * If no taxonomy argument is set, returns true if any taxonomy is queried.
  204. * If the taxonomy argument is passed but no term argument, returns true
  205. * if the taxonomy or taxonomies in the argument are being queried.
  206. * If both taxonomy and term arguments are passed, returns true
  207. * if the current query is for a term contained in the terms argument
  208. * which has a taxonomy contained in the taxonomy argument.
  209. *
  210. * @since 2.5.0
  211. * @uses $wp_query
  212. *
  213. * @param string|array $taxonomy Optional. Taxonomy slug or slugs to check in current query.
  214. * @param int|array|string $term. Optional. A single or array of, The term's ID, Name or Slug
  215. * @return bool
  216. */
  217. function is_tax( $taxonomy = '', $term = '' ) {
  218. global $wp_query, $wp_taxonomies;
  219. $queried_object = $wp_query->get_queried_object();
  220. $tax_array = array_intersect(array_keys($wp_taxonomies), (array) $taxonomy);
  221. $term_array = (array) $term;
  222. if ( !$wp_query->is_tax )
  223. return false;
  224. if ( empty( $taxonomy ) )
  225. return true;
  226. if ( empty( $term ) ) // Only a Taxonomy provided
  227. return isset($queried_object->taxonomy) && count( $tax_array ) && in_array($queried_object->taxonomy, $tax_array);
  228. return isset($queried_object->term_id) &&
  229. count(array_intersect(
  230. array($queried_object->term_id, $queried_object->name, $queried_object->slug),
  231. $term_array
  232. ));
  233. }
  234. /**
  235. * Whether the current URL is within the comments popup window.
  236. *
  237. * @since 1.5.0
  238. * @uses $wp_query
  239. *
  240. * @return bool
  241. */
  242. function is_comments_popup() {
  243. global $wp_query;
  244. return $wp_query->is_comments_popup;
  245. }
  246. /**
  247. * Whether current URL is based on a date.
  248. *
  249. * @since 1.5.0
  250. * @uses $wp_query
  251. *
  252. * @return bool
  253. */
  254. function is_date() {
  255. global $wp_query;
  256. return $wp_query->is_date;
  257. }
  258. /**
  259. * Whether current blog URL contains a day.
  260. *
  261. * @since 1.5.0
  262. * @uses $wp_query
  263. *
  264. * @return bool
  265. */
  266. function is_day() {
  267. global $wp_query;
  268. return $wp_query->is_day;
  269. }
  270. /**
  271. * Whether current page query is feed URL.
  272. *
  273. * @since 1.5.0
  274. * @uses $wp_query
  275. *
  276. * @return bool
  277. */
  278. function is_feed() {
  279. global $wp_query;
  280. return $wp_query->is_feed;
  281. }
  282. /**
  283. * Whether current page query is comment feed URL.
  284. *
  285. * @since 3.0.0
  286. * @uses $wp_query
  287. *
  288. * @return bool
  289. */
  290. function is_comment_feed() {
  291. global $wp_query;
  292. return $wp_query->is_comment_feed;
  293. }
  294. /**
  295. * Whether current page query is the front of the site.
  296. *
  297. * @since 2.5.0
  298. * @uses is_home()
  299. * @uses get_option()
  300. *
  301. * @return bool True, if front of site.
  302. */
  303. function is_front_page() {
  304. // most likely case
  305. if ( 'posts' == get_option('show_on_front') && is_home() )
  306. return true;
  307. elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
  308. return true;
  309. else
  310. return false;
  311. }
  312. /**
  313. * Whether current page view is the blog homepage.
  314. *
  315. * This is the page which is showing the time based blog content of your site
  316. * so if you set a static page for the front page of your site then this will
  317. * only be true on the page which you set as the "Posts page" in Reading Settings.
  318. *
  319. * @since 1.5.0
  320. * @uses $wp_query
  321. *
  322. * @return bool True if blog view homepage.
  323. */
  324. function is_home() {
  325. global $wp_query;
  326. return $wp_query->is_home;
  327. }
  328. /**
  329. * Whether current page query contains a month.
  330. *
  331. * @since 1.5.0
  332. * @uses $wp_query
  333. *
  334. * @return bool
  335. */
  336. function is_month() {
  337. global $wp_query;
  338. return $wp_query->is_month;
  339. }
  340. /**
  341. * Whether query is page or contains given page(s).
  342. *
  343. * Calls the function without any parameters will only test whether the current
  344. * query is of the page type. Either a list or a single item can be tested
  345. * against for whether the query is a page and also is the value or one of the
  346. * values in the page parameter.
  347. *
  348. * The parameter can contain the page ID, page title, or page name. The
  349. * parameter can also be an array of those three values.
  350. *
  351. * @since 1.5.0
  352. * @uses $wp_query
  353. *
  354. * @param mixed $page Either page or list of pages to test against.
  355. * @return bool
  356. */
  357. function is_page($page = '') {
  358. global $wp_query;
  359. if ( !$wp_query->is_page )
  360. return false;
  361. if ( empty($page) )
  362. return true;
  363. $page_obj = $wp_query->get_queried_object();
  364. $page = (array) $page;
  365. if ( in_array( $page_obj->ID, $page ) )
  366. return true;
  367. elseif ( in_array( $page_obj->post_title, $page ) )
  368. return true;
  369. else if ( in_array( $page_obj->post_name, $page ) )
  370. return true;
  371. return false;
  372. }
  373. /**
  374. * Whether query contains multiple pages for the results.
  375. *
  376. * @since 1.5.0
  377. * @uses $wp_query
  378. *
  379. * @return bool
  380. */
  381. function is_paged() {
  382. global $wp_query;
  383. return $wp_query->is_paged;
  384. }
  385. /**
  386. * Whether the current page was created by a plugin.
  387. *
  388. * The plugin can set this by using the global $plugin_page and setting it to
  389. * true.
  390. *
  391. * @since 1.5.0
  392. * @global bool $plugin_page Used by plugins to tell the query that current is a plugin page.
  393. *
  394. * @return bool
  395. */
  396. function is_plugin_page() {
  397. global $plugin_page;
  398. if ( isset($plugin_page) )
  399. return true;
  400. return false;
  401. }
  402. /**
  403. * Whether the current query is preview of post or page.
  404. *
  405. * @since 2.0.0
  406. * @uses $wp_query
  407. *
  408. * @return bool
  409. */
  410. function is_preview() {
  411. global $wp_query;
  412. return $wp_query->is_preview;
  413. }
  414. /**
  415. * Whether the current query post is robots.
  416. *
  417. * @since 2.1.0
  418. * @uses $wp_query
  419. *
  420. * @return bool
  421. */
  422. function is_robots() {
  423. global $wp_query;
  424. return $wp_query->is_robots;
  425. }
  426. /**
  427. * Whether current query is the result of a user search.
  428. *
  429. * @since 1.5.0
  430. * @uses $wp_query
  431. *
  432. * @return bool
  433. */
  434. function is_search() {
  435. global $wp_query;
  436. return $wp_query->is_search;
  437. }
  438. /**
  439. * Whether the current page query is single page.
  440. *
  441. * The parameter can contain the post ID, post title, or post name. The
  442. * parameter can also be an array of those three values.
  443. *
  444. * This applies to other post types, attachments, pages, posts. Just means that
  445. * the current query has only a single object.
  446. *
  447. * @since 1.5.0
  448. * @uses $wp_query
  449. *
  450. * @param mixed $post Either post or list of posts to test against.
  451. * @return bool
  452. */
  453. function is_single($post = '') {
  454. global $wp_query;
  455. if ( !$wp_query->is_single )
  456. return false;
  457. if ( empty($post) )
  458. return true;
  459. $post_obj = $wp_query->get_queried_object();
  460. $post = (array) $post;
  461. if ( in_array( $post_obj->ID, $post ) )
  462. return true;
  463. elseif ( in_array( $post_obj->post_title, $post ) )
  464. return true;
  465. elseif ( in_array( $post_obj->post_name, $post ) )
  466. return true;
  467. return false;
  468. }
  469. /**
  470. * Whether is single post, is a page, or is an attachment.
  471. *
  472. * @since 1.5.0
  473. * @uses $wp_query
  474. *
  475. * @param string|array $post_types Optional. Post type or types to check in current query.
  476. * @return bool
  477. */
  478. function is_singular($post_types = '') {
  479. global $wp_query;
  480. if ( empty($post_types) || !$wp_query->is_singular )
  481. return $wp_query->is_singular;
  482. $post_obj = $wp_query->get_queried_object();
  483. return in_array($post_obj->post_type, (array) $post_types);
  484. }
  485. /**
  486. * Whether the query contains a time.
  487. *
  488. * @since 1.5.0
  489. * @uses $wp_query
  490. *
  491. * @return bool
  492. */
  493. function is_time() {
  494. global $wp_query;
  495. return $wp_query->is_time;
  496. }
  497. /**
  498. * Whether the query is a trackback.
  499. *
  500. * @since 1.5.0
  501. * @uses $wp_query
  502. *
  503. * @return bool
  504. */
  505. function is_trackback() {
  506. global $wp_query;
  507. return $wp_query->is_trackback;
  508. }
  509. /**
  510. * Whether the query contains a year.
  511. *
  512. * @since 1.5.0
  513. * @uses $wp_query
  514. *
  515. * @return bool
  516. */
  517. function is_year() {
  518. global $wp_query;
  519. return $wp_query->is_year;
  520. }
  521. /**
  522. * Whether current page query is a 404 and no results for WordPress query.
  523. *
  524. * @since 1.5.0
  525. * @uses $wp_query
  526. *
  527. * @return bool True, if nothing is found matching WordPress Query.
  528. */
  529. function is_404() {
  530. global $wp_query;
  531. return $wp_query->is_404;
  532. }
  533. /*
  534. * The Loop. Post loop control.
  535. */
  536. /**
  537. * Whether current WordPress query has results to loop over.
  538. *
  539. * @see WP_Query::have_posts()
  540. * @since 1.5.0
  541. * @uses $wp_query
  542. *
  543. * @return bool
  544. */
  545. function have_posts() {
  546. global $wp_query;
  547. return $wp_query->have_posts();
  548. }
  549. /**
  550. * Whether the caller is in the Loop.
  551. *
  552. * @since 2.0.0
  553. * @uses $wp_query
  554. *
  555. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  556. */
  557. function in_the_loop() {
  558. global $wp_query;
  559. return $wp_query->in_the_loop;
  560. }
  561. /**
  562. * Rewind the loop posts.
  563. *
  564. * @see WP_Query::rewind_posts()
  565. * @since 1.5.0
  566. * @uses $wp_query
  567. *
  568. * @return null
  569. */
  570. function rewind_posts() {
  571. global $wp_query;
  572. return $wp_query->rewind_posts();
  573. }
  574. /**
  575. * Iterate the post index in the loop.
  576. *
  577. * @see WP_Query::the_post()
  578. * @since 1.5.0
  579. * @uses $wp_query
  580. */
  581. function the_post() {
  582. global $wp_query;
  583. $wp_query->the_post();
  584. }
  585. /*
  586. * Comments loop.
  587. */
  588. /**
  589. * Whether there are comments to loop over.
  590. *
  591. * @see WP_Query::have_comments()
  592. * @since 2.2.0
  593. * @uses $wp_query
  594. *
  595. * @return bool
  596. */
  597. function have_comments() {
  598. global $wp_query;
  599. return $wp_query->have_comments();
  600. }
  601. /**
  602. * Iterate comment index in the comment loop.
  603. *
  604. * @see WP_Query::the_comment()
  605. * @since 2.2.0
  606. * @uses $wp_query
  607. *
  608. * @return object
  609. */
  610. function the_comment() {
  611. global $wp_query;
  612. return $wp_query->the_comment();
  613. }
  614. /*
  615. * WP_Query
  616. */
  617. /**
  618. * The WordPress Query class.
  619. *
  620. * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  621. *
  622. * @since 1.5.0
  623. */
  624. class WP_Query {
  625. /**
  626. * Query string
  627. *
  628. * @since 1.5.0
  629. * @access public
  630. * @var string
  631. */
  632. var $query;
  633. /**
  634. * Query search variables set by the user.
  635. *
  636. * @since 1.5.0
  637. * @access public
  638. * @var array
  639. */
  640. var $query_vars = array();
  641. /**
  642. * Holds the data for a single object that is queried.
  643. *
  644. * Holds the contents of a post, page, category, attachment.
  645. *
  646. * @since 1.5.0
  647. * @access public
  648. * @var object|array
  649. */
  650. var $queried_object;
  651. /**
  652. * The ID of the queried object.
  653. *
  654. * @since 1.5.0
  655. * @access public
  656. * @var int
  657. */
  658. var $queried_object_id;
  659. /**
  660. * Get post database query.
  661. *
  662. * @since 2.0.1
  663. * @access public
  664. * @var string
  665. */
  666. var $request;
  667. /**
  668. * List of posts.
  669. *
  670. * @since 1.5.0
  671. * @access public
  672. * @var array
  673. */
  674. var $posts;
  675. /**
  676. * The amount of posts for the current query.
  677. *
  678. * @since 1.5.0
  679. * @access public
  680. * @var int
  681. */
  682. var $post_count = 0;
  683. /**
  684. * Index of the current item in the loop.
  685. *
  686. * @since 1.5.0
  687. * @access public
  688. * @var int
  689. */
  690. var $current_post = -1;
  691. /**
  692. * Whether the loop has started and the caller is in the loop.
  693. *
  694. * @since 2.0.0
  695. * @access public
  696. * @var bool
  697. */
  698. var $in_the_loop = false;
  699. /**
  700. * The current post ID.
  701. *
  702. * @since 1.5.0
  703. * @access public
  704. * @var int
  705. */
  706. var $post;
  707. /**
  708. * The list of comments for current post.
  709. *
  710. * @since 2.2.0
  711. * @access public
  712. * @var array
  713. */
  714. var $comments;
  715. /**
  716. * The amount of comments for the posts.
  717. *
  718. * @since 2.2.0
  719. * @access public
  720. * @var int
  721. */
  722. var $comment_count = 0;
  723. /**
  724. * The index of the comment in the comment loop.
  725. *
  726. * @since 2.2.0
  727. * @access public
  728. * @var int
  729. */
  730. var $current_comment = -1;
  731. /**
  732. * Current comment ID.
  733. *
  734. * @since 2.2.0
  735. * @access public
  736. * @var int
  737. */
  738. var $comment;
  739. /**
  740. * Amount of posts if limit clause was not used.
  741. *
  742. * @since 2.1.0
  743. * @access public
  744. * @var int
  745. */
  746. var $found_posts = 0;
  747. /**
  748. * The amount of pages.
  749. *
  750. * @since 2.1.0
  751. * @access public
  752. * @var int
  753. */
  754. var $max_num_pages = 0;
  755. /**
  756. * The amount of comment pages.
  757. *
  758. * @since 2.7.0
  759. * @access public
  760. * @var int
  761. */
  762. var $max_num_comment_pages = 0;
  763. /**
  764. * Set if query is single post.
  765. *
  766. * @since 1.5.0
  767. * @access public
  768. * @var bool
  769. */
  770. var $is_single = false;
  771. /**
  772. * Set if query is preview of blog.
  773. *
  774. * @since 2.0.0
  775. * @access public
  776. * @var bool
  777. */
  778. var $is_preview = false;
  779. /**
  780. * Set if query returns a page.
  781. *
  782. * @since 1.5.0
  783. * @access public
  784. * @var bool
  785. */
  786. var $is_page = false;
  787. /**
  788. * Set if query is an archive list.
  789. *
  790. * @since 1.5.0
  791. * @access public
  792. * @var bool
  793. */
  794. var $is_archive = false;
  795. /**
  796. * Set if query is part of a date.
  797. *
  798. * @since 1.5.0
  799. * @access public
  800. * @var bool
  801. */
  802. var $is_date = false;
  803. /**
  804. * Set if query contains a year.
  805. *
  806. * @since 1.5.0
  807. * @access public
  808. * @var bool
  809. */
  810. var $is_year = false;
  811. /**
  812. * Set if query contains a month.
  813. *
  814. * @since 1.5.0
  815. * @access public
  816. * @var bool
  817. */
  818. var $is_month = false;
  819. /**
  820. * Set if query contains a day.
  821. *
  822. * @since 1.5.0
  823. * @access public
  824. * @var bool
  825. */
  826. var $is_day = false;
  827. /**
  828. * Set if query contains time.
  829. *
  830. * @since 1.5.0
  831. * @access public
  832. * @var bool
  833. */
  834. var $is_time = false;
  835. /**
  836. * Set if query contains an author.
  837. *
  838. * @since 1.5.0
  839. * @access public
  840. * @var bool
  841. */
  842. var $is_author = false;
  843. /**
  844. * Set if query contains category.
  845. *
  846. * @since 1.5.0
  847. * @access public
  848. * @var bool
  849. */
  850. var $is_category = false;
  851. /**
  852. * Set if query contains tag.
  853. *
  854. * @since 2.3.0
  855. * @access public
  856. * @var bool
  857. */
  858. var $is_tag = false;
  859. /**
  860. * Set if query contains taxonomy.
  861. *
  862. * @since 2.5.0
  863. * @access public
  864. * @var bool
  865. */
  866. var $is_tax = false;
  867. /**
  868. * Set if query was part of a search result.
  869. *
  870. * @since 1.5.0
  871. * @access public
  872. * @var bool
  873. */
  874. var $is_search = false;
  875. /**
  876. * Set if query is feed display.
  877. *
  878. * @since 1.5.0
  879. * @access public
  880. * @var bool
  881. */
  882. var $is_feed = false;
  883. /**
  884. * Set if query is comment feed display.
  885. *
  886. * @since 2.2.0
  887. * @access public
  888. * @var bool
  889. */
  890. var $is_comment_feed = false;
  891. /**
  892. * Set if query is trackback.
  893. *
  894. * @since 1.5.0
  895. * @access public
  896. * @var bool
  897. */
  898. var $is_trackback = false;
  899. /**
  900. * Set if query is blog homepage.
  901. *
  902. * @since 1.5.0
  903. * @access public
  904. * @var bool
  905. */
  906. var $is_home = false;
  907. /**
  908. * Set if query couldn't found anything.
  909. *
  910. * @since 1.5.0
  911. * @access public
  912. * @var bool
  913. */
  914. var $is_404 = false;
  915. /**
  916. * Set if query is within comments popup window.
  917. *
  918. * @since 1.5.0
  919. * @access public
  920. * @var bool
  921. */
  922. var $is_comments_popup = false;
  923. /**
  924. * Set if query is part of administration page.
  925. *
  926. * @since 1.5.0
  927. * @access public
  928. * @var bool
  929. */
  930. var $is_admin = false;
  931. /**
  932. * Set if query is an attachment.
  933. *
  934. * @since 2.0.0
  935. * @access public
  936. * @var bool
  937. */
  938. var $is_attachment = false;
  939. /**
  940. * Set if is single, is a page, or is an attachment.
  941. *
  942. * @since 2.1.0
  943. * @access public
  944. * @var bool
  945. */
  946. var $is_singular = false;
  947. /**
  948. * Set if query is for robots.
  949. *
  950. * @since 2.1.0
  951. * @access public
  952. * @var bool
  953. */
  954. var $is_robots = false;
  955. /**
  956. * Set if query contains posts.
  957. *
  958. * Basically, the homepage if the option isn't set for the static homepage.
  959. *
  960. * @since 2.1.0
  961. * @access public
  962. * @var bool
  963. */
  964. var $is_posts_page = false;
  965. /**
  966. * Resets query flags to false.
  967. *
  968. * The query flags are what page info WordPress was able to figure out.
  969. *
  970. * @since 2.0.0
  971. * @access private
  972. */
  973. function init_query_flags() {
  974. $this->is_single = false;
  975. $this->is_page = false;
  976. $this->is_archive = false;
  977. $this->is_date = false;
  978. $this->is_year = false;
  979. $this->is_month = false;
  980. $this->is_day = false;
  981. $this->is_time = false;
  982. $this->is_author = false;
  983. $this->is_category = false;
  984. $this->is_tag = false;
  985. $this->is_tax = false;
  986. $this->is_search = false;
  987. $this->is_feed = false;
  988. $this->is_comment_feed = false;
  989. $this->is_trackback = false;
  990. $this->is_home = false;
  991. $this->is_404 = false;
  992. $this->is_paged = false;
  993. $this->is_admin = false;
  994. $this->is_attachment = false;
  995. $this->is_singular = false;
  996. $this->is_robots = false;
  997. $this->is_posts_page = false;
  998. }
  999. /**
  1000. * Initiates object properties and sets default values.
  1001. *
  1002. * @since 1.5.0
  1003. * @access public
  1004. */
  1005. function init() {
  1006. unset($this->posts);
  1007. unset($this->query);
  1008. $this->query_vars = array();
  1009. unset($this->queried_object);
  1010. unset($this->queried_object_id);
  1011. $this->post_count = 0;
  1012. $this->current_post = -1;
  1013. $this->in_the_loop = false;
  1014. $this->init_query_flags();
  1015. }
  1016. /**
  1017. * Reparse the query vars.
  1018. *
  1019. * @since 1.5.0
  1020. * @access public
  1021. */
  1022. function parse_query_vars() {
  1023. $this->parse_query('');
  1024. }
  1025. /**
  1026. * Fills in the query variables, which do not exist within the parameter.
  1027. *
  1028. * @since 2.1.0
  1029. * @access public
  1030. *
  1031. * @param array $array Defined query variables.
  1032. * @return array Complete query variables with undefined ones filled in empty.
  1033. */
  1034. function fill_query_vars($array) {
  1035. $keys = array(
  1036. 'error'
  1037. , 'm'
  1038. , 'p'
  1039. , 'post_parent'
  1040. , 'subpost'
  1041. , 'subpost_id'
  1042. , 'attachment'
  1043. , 'attachment_id'
  1044. , 'name'
  1045. , 'static'
  1046. , 'pagename'
  1047. , 'page_id'
  1048. , 'second'
  1049. , 'minute'
  1050. , 'hour'
  1051. , 'day'
  1052. , 'monthnum'
  1053. , 'year'
  1054. , 'w'
  1055. , 'category_name'
  1056. , 'tag'
  1057. , 'cat'
  1058. , 'tag_id'
  1059. , 'author_name'
  1060. , 'feed'
  1061. , 'tb'
  1062. , 'paged'
  1063. , 'comments_popup'
  1064. , 'meta_key'
  1065. , 'meta_value'
  1066. , 'preview'
  1067. , 's'
  1068. , 'sentence'
  1069. );
  1070. foreach ( $keys as $key ) {
  1071. if ( !isset($array[$key]))
  1072. $array[$key] = '';
  1073. }
  1074. $array_keys = array('category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1075. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and');
  1076. foreach ( $array_keys as $key ) {
  1077. if ( !isset($array[$key]))
  1078. $array[$key] = array();
  1079. }
  1080. return $array;
  1081. }
  1082. /**
  1083. * Parse a query string and set query type booleans.
  1084. *
  1085. * @since 1.5.0
  1086. * @access public
  1087. *
  1088. * @param string|array $query
  1089. */
  1090. function parse_query($query) {
  1091. if ( !empty($query) || !isset($this->query) ) {
  1092. $this->init();
  1093. if ( is_array($query) )
  1094. $this->query_vars = $query;
  1095. else
  1096. parse_str($query, $this->query_vars);
  1097. $this->query = $query;
  1098. }
  1099. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1100. $qv = &$this->query_vars;
  1101. if ( ! empty($qv['robots']) )
  1102. $this->is_robots = true;
  1103. $qv['p'] = absint($qv['p']);
  1104. $qv['page_id'] = absint($qv['page_id']);
  1105. $qv['year'] = absint($qv['year']);
  1106. $qv['monthnum'] = absint($qv['monthnum']);
  1107. $qv['day'] = absint($qv['day']);
  1108. $qv['w'] = absint($qv['w']);
  1109. $qv['m'] = absint($qv['m']);
  1110. $qv['paged'] = absint($qv['paged']);
  1111. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1112. $qv['pagename'] = trim( $qv['pagename'] );
  1113. $qv['name'] = trim( $qv['name'] );
  1114. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1115. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1116. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1117. // Compat. Map subpost to attachment.
  1118. if ( '' != $qv['subpost'] )
  1119. $qv['attachment'] = $qv['subpost'];
  1120. if ( '' != $qv['subpost_id'] )
  1121. $qv['attachment_id'] = $qv['subpost_id'];
  1122. $qv['attachment_id'] = absint($qv['attachment_id']);
  1123. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1124. $this->is_single = true;
  1125. $this->is_attachment = true;
  1126. } elseif ( '' != $qv['name'] ) {
  1127. $this->is_single = true;
  1128. } elseif ( $qv['p'] ) {
  1129. $this->is_single = true;
  1130. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1131. // If year, month, day, hour, minute, and second are set, a single
  1132. // post is being queried.
  1133. $this->is_single = true;
  1134. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1135. $this->is_page = true;
  1136. $this->is_single = false;
  1137. } elseif ( !empty($qv['s']) ) {
  1138. $this->is_search = true;
  1139. } else {
  1140. // Look for archive queries. Dates, categories, authors.
  1141. if ( '' !== $qv['second'] ) {
  1142. $this->is_time = true;
  1143. $this->is_date = true;
  1144. }
  1145. if ( '' !== $qv['minute'] ) {
  1146. $this->is_time = true;
  1147. $this->is_date = true;
  1148. }
  1149. if ( '' !== $qv['hour'] ) {
  1150. $this->is_time = true;
  1151. $this->is_date = true;
  1152. }
  1153. if ( $qv['day'] ) {
  1154. if ( ! $this->is_date ) {
  1155. $this->is_day = true;
  1156. $this->is_date = true;
  1157. }
  1158. }
  1159. if ( $qv['monthnum'] ) {
  1160. if ( ! $this->is_date ) {
  1161. $this->is_month = true;
  1162. $this->is_date = true;
  1163. }
  1164. }
  1165. if ( $qv['year'] ) {
  1166. if ( ! $this->is_date ) {
  1167. $this->is_year = true;
  1168. $this->is_date = true;
  1169. }
  1170. }
  1171. if ( $qv['m'] ) {
  1172. $this->is_date = true;
  1173. if ( strlen($qv['m']) > 9 ) {
  1174. $this->is_time = true;
  1175. } else if ( strlen($qv['m']) > 7 ) {
  1176. $this->is_day = true;
  1177. } else if ( strlen($qv['m']) > 5 ) {
  1178. $this->is_month = true;
  1179. } else {
  1180. $this->is_year = true;
  1181. }
  1182. }
  1183. if ( '' != $qv['w'] ) {
  1184. $this->is_date = true;
  1185. }
  1186. if ( empty($qv['cat']) || ($qv['cat'] == '0') ) {
  1187. $this->is_category = false;
  1188. } else {
  1189. if ( strpos($qv['cat'], '-') !== false ) {
  1190. $this->is_category = false;
  1191. } else {
  1192. $this->is_category = true;
  1193. }
  1194. }
  1195. if ( '' != $qv['category_name'] ) {
  1196. $this->is_category = true;
  1197. }
  1198. if ( !is_array($qv['category__in']) || empty($qv['category__in']) ) {
  1199. $qv['category__in'] = array();
  1200. } else {
  1201. $qv['category__in'] = array_map('absint', $qv['category__in']);
  1202. $this->is_category = true;
  1203. }
  1204. if ( !is_array($qv['category__not_in']) || empty($qv['category__not_in']) ) {
  1205. $qv['category__not_in'] = array();
  1206. } else {
  1207. $qv['category__not_in'] = array_map('absint', $qv['category__not_in']);
  1208. }
  1209. if ( !is_array($qv['category__and']) || empty($qv['category__and']) ) {
  1210. $qv['category__and'] = array();
  1211. } else {
  1212. $qv['category__and'] = array_map('absint', $qv['category__and']);
  1213. $this->is_category = true;
  1214. }
  1215. if ( '' != $qv['tag'] )
  1216. $this->is_tag = true;
  1217. $qv['tag_id'] = absint($qv['tag_id']);
  1218. if ( !empty($qv['tag_id']) )
  1219. $this->is_tag = true;
  1220. if ( !is_array($qv['tag__in']) || empty($qv['tag__in']) ) {
  1221. $qv['tag__in'] = array();
  1222. } else {
  1223. $qv['tag__in'] = array_map('absint', $qv['tag__in']);
  1224. $this->is_tag = true;
  1225. }
  1226. if ( !is_array($qv['tag__not_in']) || empty($qv['tag__not_in']) ) {
  1227. $qv['tag__not_in'] = array();
  1228. } else {
  1229. $qv['tag__not_in'] = array_map('absint', $qv['tag__not_in']);
  1230. }
  1231. if ( !is_array($qv['tag__and']) || empty($qv['tag__and']) ) {
  1232. $qv['tag__and'] = array();
  1233. } else {
  1234. $qv['tag__and'] = array_map('absint', $qv['tag__and']);
  1235. $this->is_category = true;
  1236. }
  1237. if ( !is_array($qv['tag_slug__in']) || empty($qv['tag_slug__in']) ) {
  1238. $qv['tag_slug__in'] = array();
  1239. } else {
  1240. $qv['tag_slug__in'] = array_map('sanitize_title', $qv['tag_slug__in']);
  1241. $this->is_tag = true;
  1242. }
  1243. if ( !is_array($qv['tag_slug__and']) || empty($qv['tag_slug__and']) ) {
  1244. $qv['tag_slug__and'] = array();
  1245. } else {
  1246. $qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
  1247. $this->is_tag = true;
  1248. }
  1249. if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
  1250. $this->is_tax = false;
  1251. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1252. if ( $t->query_var && isset($qv[$t->query_var]) && '' != $qv[$t->query_var] ) {
  1253. $qv['taxonomy'] = $taxonomy;
  1254. $qv['term'] = $qv[$t->query_var];
  1255. $this->is_tax = true;
  1256. break;
  1257. }
  1258. }
  1259. } else {
  1260. $this->is_tax = true;
  1261. }
  1262. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1263. $this->is_author = false;
  1264. } else {
  1265. $this->is_author = true;
  1266. }
  1267. if ( '' != $qv['author_name'] ) {
  1268. $this->is_author = true;
  1269. }
  1270. if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax) )
  1271. $this->is_archive = true;
  1272. }
  1273. if ( '' != $qv['feed'] )
  1274. $this->is_feed = true;
  1275. if ( '' != $qv['tb'] )
  1276. $this->is_trackback = true;
  1277. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1278. $this->is_paged = true;
  1279. if ( '' != $qv['comments_popup'] )
  1280. $this->is_comments_popup = true;
  1281. // if we're previewing inside the write screen
  1282. if ( '' != $qv['preview'] )
  1283. $this->is_preview = true;
  1284. if ( is_admin() )
  1285. $this->is_admin = true;
  1286. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1287. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1288. $qv['withcomments'] = 1;
  1289. }
  1290. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1291. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1292. $this->is_comment_feed = true;
  1293. 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 ) )
  1294. $this->is_home = true;
  1295. // Correct is_* for page_on_front and page_for_posts
  1296. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1297. $_query = wp_parse_args($query);
  1298. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1299. $this->is_page = true;
  1300. $this->is_home = false;
  1301. $qv['page_id'] = get_option('page_on_front');
  1302. // Correct <!--nextpage--> for page_on_front
  1303. if ( !empty($qv['paged']) ) {
  1304. $qv['page'] = $qv['paged'];
  1305. unset($qv['paged']);
  1306. }
  1307. }
  1308. }
  1309. if ( '' != $qv['pagename'] ) {
  1310. $this->queried_object =& get_page_by_path($qv['pagename']);
  1311. if ( !empty($this->queried_object) )
  1312. $this->queried_object_id = (int) $this->queried_object->ID;
  1313. else
  1314. unset($this->queried_object);
  1315. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1316. $this->is_page = false;
  1317. $this->is_home = true;
  1318. $this->is_posts_page = true;
  1319. }
  1320. }
  1321. if ( $qv['page_id'] ) {
  1322. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1323. $this->is_page = false;
  1324. $this->is_home = true;
  1325. $this->is_posts_page = true;
  1326. }
  1327. }
  1328. if ( !empty($qv['post_type']) ) {
  1329. if ( is_array($qv['post_type']) )
  1330. $qv['post_type'] = array_map('sanitize_user', $qv['post_type'], array(true));
  1331. else
  1332. $qv['post_type'] = sanitize_user($qv['post_type'], true);
  1333. }
  1334. if ( !empty($qv['post_status']) )
  1335. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1336. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1337. $this->is_comment_feed = false;
  1338. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1339. // Done correcting is_* for page_on_front and page_for_posts
  1340. if ( '404' == $qv['error'] )
  1341. $this->set_404();
  1342. if ( !empty($query) )
  1343. do_action_ref_array('parse_query', array(&$this));
  1344. }
  1345. /**
  1346. * Sets the 404 property and saves whether query is feed.
  1347. *
  1348. * @since 2.0.0
  1349. * @access public
  1350. */
  1351. function set_404() {
  1352. $is_feed = $this->is_feed;
  1353. $this->init_query_flags();
  1354. $this->is_404 = true;
  1355. $this->is_feed = $is_feed;
  1356. }
  1357. /**
  1358. * Retrieve query variable.
  1359. *
  1360. * @since 1.5.0
  1361. * @access public
  1362. *
  1363. * @param string $query_var Query variable key.
  1364. * @return mixed
  1365. */
  1366. function get($query_var) {
  1367. if ( isset($this->query_vars[$query_var]) )
  1368. return $this->query_vars[$query_var];
  1369. return '';
  1370. }
  1371. /**
  1372. * Set query variable.
  1373. *
  1374. * @since 1.5.0
  1375. * @access public
  1376. *
  1377. * @param string $query_var Query variable key.
  1378. * @param mixed $value Query variable value.
  1379. */
  1380. function set($query_var, $value) {
  1381. $this->query_vars[$query_var] = $value;
  1382. }
  1383. /**
  1384. * Retrieve the posts based on query variables.
  1385. *
  1386. * There are a few filters and actions that can be used to modify the post
  1387. * database query.
  1388. *
  1389. * @since 1.5.0
  1390. * @access public
  1391. * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  1392. *
  1393. * @return array List of posts.
  1394. */
  1395. function &get_posts() {
  1396. global $wpdb, $user_ID, $_wp_using_ext_object_cache;
  1397. do_action_ref_array('pre_get_posts', array(&$this));
  1398. // Shorthand.
  1399. $q = &$this->query_vars;
  1400. $q = $this->fill_query_vars($q);
  1401. // First let's clear some variables
  1402. $distinct = '';
  1403. $whichcat = '';
  1404. $whichauthor = '';
  1405. $whichmimetype = '';
  1406. $where = '';
  1407. $limits = '';
  1408. $join = '';
  1409. $search = '';
  1410. $groupby = '';
  1411. $fields = "$wpdb->posts.*";
  1412. $post_status_join = false;
  1413. $page = 1;
  1414. if ( !isset($q['caller_get_posts']) )
  1415. $q['caller_get_posts'] = false;
  1416. if ( !isset($q['suppress_filters']) )
  1417. $q['suppress_filters'] = false;
  1418. if ( !isset($q['cache_results']) ) {
  1419. if ( $_wp_using_ext_object_cache )
  1420. $q['cache_results'] = false;
  1421. else
  1422. $q['cache_results'] = true;
  1423. }
  1424. if ( !isset($q['update_post_term_cache']) )
  1425. $q['update_post_term_cache'] = true;
  1426. if ( !isset($q['update_post_meta_cache']) )
  1427. $q['update_post_meta_cache'] = true;
  1428. if ( !isset($q['post_type']) ) {
  1429. if ( $this->is_search )
  1430. $q['post_type'] = 'any';
  1431. else
  1432. $q['post_type'] = '';
  1433. }
  1434. $post_type = $q['post_type'];
  1435. if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  1436. $q['posts_per_page'] = get_option('posts_per_page');
  1437. if ( isset($q['showposts']) && $q['showposts'] ) {
  1438. $q['showposts'] = (int) $q['showposts'];
  1439. $q['posts_per_page'] = $q['showposts'];
  1440. }
  1441. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1442. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1443. if ( !isset($q['nopaging']) ) {
  1444. if ( $q['posts_per_page'] == -1 ) {
  1445. $q['nopaging'] = true;
  1446. } else {
  1447. $q['nopaging'] = false;
  1448. }
  1449. }
  1450. if ( $this->is_feed ) {
  1451. $q['posts_per_page'] = get_option('posts_per_rss');
  1452. $q['nopaging'] = false;
  1453. }
  1454. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1455. if ( $q['posts_per_page'] < -1 )
  1456. $q['posts_per_page'] = abs($q['posts_per_page']);
  1457. else if ( $q['posts_per_page'] == 0 )
  1458. $q['posts_per_page'] = 1;
  1459. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1460. $q['comments_per_page'] = get_option('comments_per_page');
  1461. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1462. $this->is_page = true;
  1463. $this->is_home = false;
  1464. $q['page_id'] = get_option('page_on_front');
  1465. }
  1466. if ( isset($q['page']) ) {
  1467. $q['page'] = trim($q['page'], '/');
  1468. $q['page'] = absint($q['page']);
  1469. }
  1470. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1471. if ( isset($q['no_found_rows']) )
  1472. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1473. else
  1474. $q['no_found_rows'] = false;
  1475. // If a month is specified in the querystring, load that month
  1476. if ( $q['m'] ) {
  1477. $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  1478. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  1479. if ( strlen($q['m']) > 5 )
  1480. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  1481. if ( strlen($q['m']) > 7 )
  1482. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  1483. if ( strlen($q['m']) > 9 )
  1484. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  1485. if ( strlen($q['m']) > 11 )
  1486. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  1487. if ( strlen($q['m']) > 13 )
  1488. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  1489. }
  1490. if ( '' !== $q['hour'] )
  1491. $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
  1492. if ( '' !== $q['minute'] )
  1493. $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
  1494. if ( '' !== $q['second'] )
  1495. $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
  1496. if ( $q['year'] )
  1497. $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
  1498. if ( $q['monthnum'] )
  1499. $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
  1500. if ( $q['day'] )
  1501. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
  1502. // If we've got a post_type AND its not "any" post_type.
  1503. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  1504. foreach ( (array)$q['post_type'] as $_post_type ) {
  1505. $ptype_obj = get_post_type_object($_post_type);
  1506. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  1507. continue;
  1508. if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  1509. // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  1510. $q['name'] = $q[ $ptype_obj->query_var ];
  1511. } else {
  1512. // Hierarchical post_types will operate through the
  1513. $q['pagename'] = $q[ $ptype_obj->query_var ];
  1514. $q['name'] = '';
  1515. }
  1516. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  1517. break;
  1518. } //end foreach
  1519. unset($ptype_obj);
  1520. }
  1521. if ( '' != $q['name'] ) {
  1522. $q['name'] = sanitize_title($q['name']);
  1523. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  1524. } elseif ( '' != $q['pagename'] ) {
  1525. if ( isset($this->queried_object_id) ) {
  1526. $reqpage = $this->queried_object_id;
  1527. } else {
  1528. if ( 'page' != $q['post_type'] ) {
  1529. foreach ( (array)$q['post_type'] as $_post_type ) {
  1530. $ptype_obj = get_post_type_object($_post_type);
  1531. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  1532. continue;
  1533. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  1534. if ( $reqpage )
  1535. break;
  1536. }
  1537. unset($ptype_obj);
  1538. } else {
  1539. $reqpage = get_page_by_path($q['pagename']);
  1540. }
  1541. if ( !empty($reqpage) )
  1542. $reqpage = $reqpage->ID;
  1543. else
  1544. $reqpage = 0;
  1545. }
  1546. $page_for_posts = get_option('page_for_posts');
  1547. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  1548. $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
  1549. $page_paths = '/' . trim($q['pagename'], '/');
  1550. $q['pagename'] = sanitize_title(basename($page_paths));
  1551. $q['name'] = $q['pagename'];
  1552. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  1553. $reqpage_obj = get_page($reqpage);
  1554. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  1555. $this->is_attachment = true;
  1556. $post_type = $q['post_type'] = 'attachment';
  1557. $this->is_page = true;
  1558. $q['attachment_id'] = $reqpage;
  1559. }
  1560. }
  1561. } elseif ( '' != $q['attachment'] ) {
  1562. $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
  1563. $attach_paths = '/' . trim($q['attachment'], '/');
  1564. $q['attachment'] = sanitize_title(basename($attach_paths));
  1565. $q['name'] = $q['attachment'];
  1566. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  1567. }
  1568. if ( $q['w'] )
  1569. $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
  1570. if ( intval($q['comments_popup']) )
  1571. $q['p'] = absint($q['comments_popup']);
  1572. // If an attachment is requested by number, let it supercede any post number.
  1573. if ( $q['attachment_id'] )
  1574. $q['p'] = absint($q['attachment_id']);
  1575. // If a post number is specified, load that post
  1576. if ( $q['p'] ) {
  1577. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  1578. } elseif ( $q['post__in'] ) {
  1579. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  1580. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  1581. } elseif ( $q['post__not_in'] ) {
  1582. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  1583. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  1584. }
  1585. if ( is_numeric($q['post_parent']) )
  1586. $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  1587. if ( $q['page_id'] ) {
  1588. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  1589. $q['p'] = $q['page_id'];
  1590. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  1591. }
  1592. }
  1593. // If a search pattern is specified, load the posts that match
  1594. if ( !empty($q['s']) ) {
  1595. // added slashes screw with quote grouping when done early, so done later
  1596. $q['s'] = stripslashes($q['s']);
  1597. if ( !empty($q['sentence']) ) {
  1598. $q['search_terms'] = array($q['s']);
  1599. } else {
  1600. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
  1601. $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
  1602. }
  1603. $n = !empty($q['exact']) ? '' : '%';
  1604. $searchand = '';
  1605. foreach( (array) $q['search_terms'] as $term ) {
  1606. $term = addslashes_gpc($term);
  1607. $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  1608. $searchand = ' AND ';
  1609. }
  1610. $term = esc_sql($q['s']);
  1611. if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
  1612. $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
  1613. if ( !empty($search) ) {
  1614. $search = " AND ({$search}) ";
  1615. if ( !is_user_logged_in() )
  1616. $search .= " AND ($wpdb->posts.post_password = '') ";
  1617. }
  1618. }
  1619. // Allow plugins to contextually add/remove/modify the search section of the database query
  1620. $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
  1621. // Category stuff
  1622. if ( empty($q['cat']) || ($q['cat'] == '0') ||
  1623. // Bypass cat checks if fetching specific posts
  1624. $this->is_singular ) {
  1625. $whichcat = '';
  1626. } else {
  1627. $q['cat'] = ''.urldecode($q['cat']).'';
  1628. $q['cat'] = addslashes_gpc($q['cat']);
  1629. $cat_array = preg_split('/[,\s]+/', $q['cat']);
  1630. $q['cat'] = '';
  1631. $req_cats = array();
  1632. foreach ( (array) $cat_array as $cat ) {
  1633. $cat = intval($cat);
  1634. $req_cats[] = $cat;
  1635. $in = ($cat > 0);
  1636. $cat = abs($cat);
  1637. if ( $in ) {
  1638. $q['category__in'][] = $cat;
  1639. $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
  1640. } else {
  1641. $q['category__not_in'][] = $cat;
  1642. $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
  1643. }
  1644. }
  1645. $q['cat'] = implode(',', $req_cats);
  1646. }
  1647. if ( !empty($q['category__in']) ) {
  1648. $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  1649. $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
  1650. $include_cats = "'" . implode("', '", $q['category__in']) . "'";
  1651. $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
  1652. }
  1653. if ( !empty($q['category__not_in']) ) {
  1654. $cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
  1655. $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN ($cat_string) )";
  1656. }
  1657. // Category stuff for nice URLs
  1658. if ( '' != $q['category_name'] && !$this->is_singular ) {
  1659. $q['category_name'] = implode('/', array_map('sanitize_title', explode('/', $q['category_name'])));
  1660. $reqcat = get_category_by_path($q['category_name']);
  1661. $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
  1662. $cat_paths = '/' . trim($q['category_name'], '/');
  1663. $q['category_name'] = sanitize_title(basename($cat_paths));
  1664. $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
  1665. $q['category_name'] = sanitize_title(basename($cat_paths));
  1666. $cat_paths = explode('/', $cat_paths);
  1667. $cat_path = '';
  1668. foreach ( (array) $cat_paths as $pathdir )
  1669. $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
  1670. //if we don't match the entire hierarchy fallback on just matching the nicename
  1671. if ( empty($reqcat) )
  1672. $reqcat = get_category_by_path($q['category_name'], false);
  1673. if ( !empty($reqcat) )
  1674. $reqcat = $reqcat->term_id;
  1675. else
  1676. $reqcat = 0;
  1677. $q['cat'] = $reqcat;
  1678. $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  1679. $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
  1680. $in_cats = array($q['cat']);
  1681. $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
  1682. $in_cats = "'" . implode("', '", $in_cats) . "'";
  1683. $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
  1684. $groupby = "{$wpdb->posts}.ID";
  1685. }
  1686. // Tags
  1687. if ( '' != $q['tag'] ) {
  1688. if ( strpos($q['tag'], ',') !== false ) {
  1689. $tags = preg_split('/[,\s]+/', $q['tag']);
  1690. foreach ( (array) $tags as $tag ) {
  1691. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1692. $q['tag_slug__in'][] = $tag;
  1693. }
  1694. } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
  1695. $tags = preg_split('/[+\s]+/', $q['tag']);
  1696. foreach ( (array) $tags as $tag ) {
  1697. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1698. $q['tag_slug__and'][] = $tag;
  1699. }
  1700. } else {
  1701. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1702. $q['tag_slug__in'][] = $q['tag'];
  1703. }
  1704. }
  1705. if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) {
  1706. $groupby = "{$wpdb->posts}.ID";
  1707. }
  1708. if ( !empty($q['tag__in']) && empty($q['cat']) ) {
  1709. $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  1710. $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
  1711. $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
  1712. $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
  1713. $reqtag = term_exists( $q['tag__in'][0], 'post_tag' );
  1714. if ( !empty($reqtag) )
  1715. $q['tag_id'] = $reqtag['term_id'];
  1716. }
  1717. if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {
  1718. $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) ";
  1719. $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
  1720. $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
  1721. $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
  1722. $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
  1723. if ( !empty($reqtag) )
  1724. $q['tag_id'] = $reqtag->term_id;
  1725. }
  1726. if ( !empty($q['tag__not_in']) ) {
  1727. $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'";
  1728. $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'post_tag' AND tt.term_id IN ($tag_string) )";
  1729. }
  1730. // Tag and slug intersections.
  1731. $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
  1732. $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
  1733. foreach ( $intersections as $item => $taxonomy ) {
  1734. if ( empty($q[$item]) ) continue;
  1735. if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
  1736. if ( $item != 'category__and' ) {
  1737. $reqtag = term_exists( $q[$item][0], 'post_tag' );
  1738. if ( !empty($reqtag) )
  1739. $q['tag_id'] = $reqtag['term_id'];
  1740. }
  1741. if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) )
  1742. $taxonomy_field = 'slug';
  1743. else
  1744. $taxonomy_field = 'term_id';
  1745. $q[$item] = array_unique($q[$item]);
  1746. $tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
  1747. $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
  1748. if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
  1749. $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
  1750. }
  1751. $post_ids = $wpdb->get_col($tsql);
  1752. if ( count($post_ids) )
  1753. $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
  1754. else {
  1755. $whichcat = " AND 0 = 1";
  1756. break;
  1757. }
  1758. }
  1759. // Taxonomies
  1760. if ( $this->is_tax ) {
  1761. if ( '' != $q['taxonomy'] ) {
  1762. $taxonomy = $q['taxonomy'];
  1763. $tt[$taxonomy] = $q['term'];
  1764. } else {
  1765. foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  1766. if ( $t->query_var && '' != $q[$t->query_var] ) {
  1767. $tt[$taxonomy] = $q[$t->query_var];
  1768. break;
  1769. }
  1770. }
  1771. }
  1772. $terms = get_terms($taxonomy, array('slug' => $tt[$taxonomy], 'hide_empty' => !is_taxonomy_hierarchical($taxonomy)));
  1773. if ( is_wp_error($terms) || empty($terms) ) {
  1774. $whichcat = " AND 0 ";
  1775. } else {
  1776. foreach ( $terms as $term ) {
  1777. $term_ids[] = $term->term_id;
  1778. if ( is_taxonomy_hierarchical($taxonomy) ) {
  1779. $children = get_term_children($term->term_id, $taxonomy);
  1780. $term_ids = array_merge($term_ids, $children);
  1781. }
  1782. }
  1783. $post_ids = get_objects_in_term($term_ids, $taxonomy);
  1784. if ( !is_wp_error($post_ids) && !empty($post_ids) ) {
  1785. $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
  1786. if ( empty($post_type) ) {
  1787. $post_type = 'any';
  1788. $post_status_join = true;
  1789. } elseif ( in_array('attachment', (array)$post_type) ) {
  1790. $post_status_join = true;
  1791. }
  1792. } else {
  1793. $whichcat = " AND 0 ";
  1794. }
  1795. }
  1796. }
  1797. // Author/user stuff
  1798. if ( empty($q['author']) || ($q['author'] == '0') ) {
  1799. $whichauthor = '';
  1800. } else {
  1801. $q['author'] = (string)urldecode($q['author']);
  1802. $q['author'] = addslashes_gpc($q['author']);
  1803. if ( strpos($q['author'], '-') !== false ) {
  1804. $eq = '!=';
  1805. $andor = 'AND';
  1806. $q['author'] = explode('-', $q['author']);
  1807. $q['author'] = (string)absint($q['author'][1]);
  1808. } else {
  1809. $eq = '=';
  1810. $andor = 'OR';
  1811. }
  1812. $author_array = preg_split('/[,\s]+/', $q['author']);
  1813. $_author_array = array();
  1814. foreach ( $author_array as $key => $_author )
  1815. $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
  1816. $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
  1817. unset($author_array, $_author_array);
  1818. }
  1819. // Author stuff for nice URLs
  1820. if ( '' != $q['author_name'] ) {
  1821. if ( strpos($q['author_name'], '/') !== false ) {
  1822. $q['author_name'] = explode('/', $q['author_name']);
  1823. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  1824. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  1825. } else {
  1826. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailling slash
  1827. }
  1828. }
  1829. $q['author_name'] = sanitize_title($q['author_name']);
  1830. $q['author'] = get_user_by('slug', $q['author_name']);
  1831. if ( $q['author'] )
  1832. $q['author'] = $q['author']->ID;
  1833. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  1834. }
  1835. // MIME-Type stuff for attachment browsing
  1836. if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
  1837. $table_alias = $post_status_join ? $wpdb->posts : '';
  1838. $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
  1839. }
  1840. $where .= $search . $whichcat . $whichauthor . $whichmimetype;
  1841. if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  1842. $q['order'] = 'DESC';
  1843. // Order by
  1844. if ( empty($q['orderby']) ) {
  1845. $q['orderby'] = "$wpdb->posts.post_date " . $q['order'];
  1846. } elseif ( 'none' == $q['orderby'] ) {
  1847. $q['orderby'] = '';
  1848. } else {
  1849. // Used to filter values
  1850. $allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  1851. if ( !empty($q['meta_key']) ) {
  1852. $allowed_keys[] = $q['meta_key'];
  1853. $allowed_keys[] = 'meta_value';
  1854. $allowed_keys[] = 'meta_value_num';
  1855. }
  1856. $q['orderby'] = urldecode($q['orderby']);
  1857. $q['orderby'] = addslashes_gpc($q['orderby']);
  1858. $orderby_array = explode(' ', $q['orderby']);
  1859. $q['orderby'] = '';
  1860. foreach ( $orderby_array as $i => $orderby ) {
  1861. // Only allow certain values for safety
  1862. if ( ! in_array($orderby, $allowed_keys) )
  1863. continue;
  1864. switch ( $orderby ) {
  1865. case 'menu_order':
  1866. break;
  1867. case 'ID':
  1868. $orderby = "$wpdb->posts.ID";
  1869. break;
  1870. case 'rand':
  1871. $orderby = 'RAND()';
  1872. break;
  1873. case $q['meta_key']:
  1874. case 'meta_value':
  1875. $orderby = "$wpdb->postmeta.meta_value";
  1876. break;
  1877. case 'meta_value_num':
  1878. $orderby = "$wpdb->postmeta.meta_value+0";
  1879. break;
  1880. case 'comment_count':
  1881. $orderby = "$wpdb->posts.comment_count";
  1882. break;
  1883. default:
  1884. $orderby = "$wpdb->posts.post_" . $orderby;
  1885. }
  1886. $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
  1887. }
  1888. // append ASC or DESC at the end
  1889. if ( !empty($q['orderby']))
  1890. $q['orderby'] .= " {$q['order']}";
  1891. if ( empty($q['orderby']) )
  1892. $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
  1893. }
  1894. if ( is_array($post_type) ) {
  1895. $post_type_cap = 'multiple_post_type';
  1896. } else {
  1897. $post_type_object = get_post_type_object ( $post_type );
  1898. if ( !empty($post_type_object) )
  1899. $post_type_cap = $post_type_object->capability_type;
  1900. else
  1901. $post_type_cap = $post_type;
  1902. }
  1903. $exclude_post_types = '';
  1904. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  1905. if ( ! empty( $in_search_post_types ) )
  1906. $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
  1907. if ( 'any' == $post_type ) {
  1908. $where .= $exclude_post_types;
  1909. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  1910. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  1911. } elseif ( ! empty( $post_type ) ) {
  1912. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  1913. $post_type_object = get_post_type_object ( $post_type );
  1914. } elseif ( $this->is_attachment ) {
  1915. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  1916. $post_type_object = get_post_type_object ( 'attachment' );
  1917. } elseif ( $this->is_page ) {
  1918. $where .= " AND $wpdb->posts.post_type = 'page'";
  1919. $post_type_object = get_post_type_object ( 'page' );
  1920. } else {
  1921. $where .= " AND $wpdb->posts.post_type = 'post'";
  1922. $post_type_object = get_post_type_object ( 'post' );
  1923. }
  1924. if ( !empty($post_type_object) ) {
  1925. $post_type_cap = $post_type_object->capability_type;
  1926. $edit_cap = $post_type_object->cap->edit_post;
  1927. $read_cap = $post_type_object->cap->read_post;
  1928. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  1929. $read_private_cap = $post_type_object->cap->read_private_posts;
  1930. } else {
  1931. $edit_cap = 'edit_' . $post_type_cap;
  1932. $read_cap = 'read_' . $post_type_cap;
  1933. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  1934. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  1935. }
  1936. if ( isset($q['post_status']) && '' != $q['post_status'] ) {
  1937. $statuswheres = array();
  1938. $q_status = explode(',', $q['post_status']);
  1939. $r_status = array();
  1940. $p_status = array();
  1941. $e_status = array();
  1942. if ( $q['post_status'] == 'any' ) {
  1943. foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  1944. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  1945. } else {
  1946. foreach ( get_post_stati() as $status ) {
  1947. if ( in_array( $status, $q_status ) ) {
  1948. if ( 'private' == $status )
  1949. $p_status[] = "$wpdb->posts.post_status = '$status'";
  1950. else
  1951. $r_status[] = "$wpdb->posts.post_status = '$status'";
  1952. }
  1953. }
  1954. }
  1955. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  1956. $r_status = array_merge($r_status, $p_status);
  1957. unset($p_status);
  1958. }
  1959. if ( !empty($e_status) ) {
  1960. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  1961. }
  1962. if ( !empty($r_status) ) {
  1963. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  1964. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $r_status ) . "))";
  1965. else
  1966. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  1967. }
  1968. if ( !empty($p_status) ) {
  1969. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  1970. $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
  1971. else
  1972. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  1973. }
  1974. if ( $post_status_join ) {
  1975. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  1976. foreach ( $statuswheres as $index => $statuswhere )
  1977. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  1978. }
  1979. foreach ( $statuswheres as $statuswhere )
  1980. $where .= " AND $statuswhere";
  1981. } elseif ( !$this->is_singular ) {
  1982. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  1983. // Add public states.
  1984. $public_states = get_post_stati( array('public' => true) );
  1985. foreach ( (array) $public_states as $state ) {
  1986. if ( 'publish' == $state ) // Publish is hard-coded above.
  1987. continue;
  1988. $where .= " OR $wpdb->posts.post_status = '$state'";
  1989. }
  1990. if ( is_admin() ) {
  1991. // Add protected states that should show in the admin all list.
  1992. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  1993. foreach ( (array) $admin_all_states as $state )
  1994. $where .= " OR $wpdb->posts.post_status = '$state'";
  1995. }
  1996. if ( is_user_logged_in() ) {
  1997. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  1998. $private_states = get_post_stati( array('private' => true) );
  1999. foreach ( (array) $private_states as $state )
  2000. $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'";
  2001. }
  2002. $where .= ')';
  2003. }
  2004. // postmeta queries
  2005. if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
  2006. $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
  2007. if ( ! empty($q['meta_key']) )
  2008. $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
  2009. if ( ! empty($q['meta_value']) ) {
  2010. if ( empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
  2011. $q['meta_compare'] = '=';
  2012. $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
  2013. }
  2014. // Apply filters on where and join prior to paging so that any
  2015. // manipulations to them are reflected in the paging by day queries.
  2016. if ( !$q['suppress_filters'] ) {
  2017. $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
  2018. $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
  2019. }
  2020. // Paging
  2021. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2022. $page = absint($q['paged']);
  2023. if ( empty($page) )
  2024. $page = 1;
  2025. if ( empty($q['offset']) ) {
  2026. $pgstrt = '';
  2027. $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  2028. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2029. } else { // we're ignoring $page and using 'offset'
  2030. $q['offset'] = absint($q['offset']);
  2031. $pgstrt = $q['offset'] . ', ';
  2032. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2033. }
  2034. }
  2035. // Comments feeds
  2036. if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  2037. if ( $this->is_archive || $this->is_search ) {
  2038. $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  2039. $cwhere = "WHERE comment_approved = '1' $where";
  2040. $cgroupby = "$wpdb->comments.comment_id";
  2041. } else { // Other non singular e.g. front
  2042. $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  2043. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2044. $cgroupby = '';
  2045. }
  2046. if ( !$q['suppress_filters'] ) {
  2047. $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
  2048. $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
  2049. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
  2050. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2051. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2052. }
  2053. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2054. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2055. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2056. $this->comment_count = count($this->comments);
  2057. $post_ids = array();
  2058. foreach ( $this->comments as $comment )
  2059. $post_ids[] = (int) $comment->comment_post_ID;
  2060. $post_ids = join(',', $post_ids);
  2061. $join = '';
  2062. if ( $post_ids )
  2063. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2064. else
  2065. $where = "AND 0";
  2066. }
  2067. $orderby = $q['orderby'];
  2068. // Apply post-paging filters on where and join. Only plugins that
  2069. // manipulate paging queries should use these hooks.
  2070. if ( !$q['suppress_filters'] ) {
  2071. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2072. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2073. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2074. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2075. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2076. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2077. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2078. }
  2079. // Announce current selection parameters. For use by caching plugins.
  2080. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2081. // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above.
  2082. if ( !$q['suppress_filters'] ) {
  2083. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2084. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2085. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2086. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2087. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2088. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2089. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2090. }
  2091. if ( ! empty($groupby) )
  2092. $groupby = 'GROUP BY ' . $groupby;
  2093. if ( !empty( $orderby ) )
  2094. $orderby = 'ORDER BY ' . $orderby;
  2095. $found_rows = '';
  2096. if ( !$q['no_found_rows'] && !empty($limits) )
  2097. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2098. $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2099. if ( !$q['suppress_filters'] )
  2100. $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
  2101. $this->posts = $wpdb->get_results($this->request);
  2102. // Raw results filter. Prior to status checks.
  2103. if ( !$q['suppress_filters'] )
  2104. $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  2105. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2106. $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
  2107. $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2108. $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
  2109. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2110. $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2111. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2112. $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2113. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  2114. $this->comments = $wpdb->get_results($comments_request);
  2115. $this->comment_count = count($this->comments);
  2116. }
  2117. if ( !$q['no_found_rows'] && !empty($limits) ) {
  2118. $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
  2119. $this->found_posts = $wpdb->get_var( $found_posts_query );
  2120. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  2121. $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
  2122. }
  2123. // Check post status to determine if post should be displayed.
  2124. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2125. $status = get_post_status($this->posts[0]);
  2126. $post_status_obj = get_post_status_object($status);
  2127. //$type = get_post_type($this->posts[0]);
  2128. if ( !$post_status_obj->public ) {
  2129. if ( ! is_user_logged_in() ) {
  2130. // User must be logged in to view unpublished posts.
  2131. $this->posts = array();
  2132. } else {
  2133. if ( $post_status_obj->protected ) {
  2134. // User must have edit permissions on the draft to preview.
  2135. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2136. $this->posts = array();
  2137. } else {
  2138. $this->is_preview = true;
  2139. if ( 'future' != $status )
  2140. $this->posts[0]->post_date = current_time('mysql');
  2141. }
  2142. } elseif ( $post_status_obj->private ) {
  2143. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2144. $this->posts = array();
  2145. } else {
  2146. $this->posts = array();
  2147. }
  2148. }
  2149. }
  2150. if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) )
  2151. $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
  2152. }
  2153. // Put sticky posts at the top of the posts array
  2154. $sticky_posts = get_option('sticky_posts');
  2155. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['caller_get_posts'] ) {
  2156. $num_posts = count($this->posts);
  2157. $sticky_offset = 0;
  2158. // Loop over posts and relocate stickies to the front.
  2159. for ( $i = 0; $i < $num_posts; $i++ ) {
  2160. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2161. $sticky_post = $this->posts[$i];
  2162. // Remove sticky from current position
  2163. array_splice($this->posts, $i, 1);
  2164. // Move to front, after other stickies
  2165. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2166. // Increment the sticky offset. The next sticky will be placed at this offset.
  2167. $sticky_offset++;
  2168. // Remove post from sticky posts array
  2169. $offset = array_search($sticky_post->ID, $sticky_posts);
  2170. unset( $sticky_posts[$offset] );
  2171. }
  2172. }
  2173. // If any posts have been excluded specifically, Ignore those that are sticky.
  2174. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  2175. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2176. // Fetch sticky posts that weren't in the query results
  2177. if ( !empty($sticky_posts) ) {
  2178. $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
  2179. // honor post type(s) if not set to any
  2180. $stickies_where = '';
  2181. if ( 'any' != $post_type && '' != $post_type ) {
  2182. if ( is_array( $post_type ) ) {
  2183. $post_types = join( "', '", $post_type );
  2184. } else {
  2185. $post_types = $post_type;
  2186. }
  2187. $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
  2188. }
  2189. $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
  2190. foreach ( $stickies as $sticky_post ) {
  2191. // Ignore sticky posts the current user cannot read or are not published.
  2192. if ( 'publish' != $sticky_post->post_status )
  2193. continue;
  2194. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2195. $sticky_offset++;
  2196. }
  2197. }
  2198. }
  2199. if ( !$q['suppress_filters'] )
  2200. $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
  2201. $this->post_count = count($this->posts);
  2202. // Sanitize before caching so it'll only get done once
  2203. for ( $i = 0; $i < $this->post_count; $i++ ) {
  2204. $this->posts[$i] = sanitize_post($this->posts[$i], 'raw');
  2205. }
  2206. if ( $q['cache_results'] )
  2207. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2208. if ( $this->post_count > 0 ) {
  2209. $this->post = $this->posts[0];
  2210. }
  2211. return $this->posts;
  2212. }
  2213. /**
  2214. * Set up the next post and iterate current post index.
  2215. *
  2216. * @since 1.5.0
  2217. * @access public
  2218. *
  2219. * @return object Next post.
  2220. */
  2221. function next_post() {
  2222. $this->current_post++;
  2223. $this->post = $this->posts[$this->current_post];
  2224. return $this->post;
  2225. }
  2226. /**
  2227. * Sets up the current post.
  2228. *
  2229. * Retrieves the next post, sets up the post, sets the 'in the loop'
  2230. * property to true.
  2231. *
  2232. * @since 1.5.0
  2233. * @access public
  2234. * @uses $post
  2235. * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
  2236. */
  2237. function the_post() {
  2238. global $post;
  2239. $this->in_the_loop = true;
  2240. if ( $this->current_post == -1 ) // loop has just started
  2241. do_action_ref_array('loop_start', array(&$this));
  2242. $post = $this->next_post();
  2243. setup_postdata($post);
  2244. }
  2245. /**
  2246. * Whether there are more posts available in the loop.
  2247. *
  2248. * Calls action 'loop_end', when the loop is complete.
  2249. *
  2250. * @since 1.5.0
  2251. * @access public
  2252. * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
  2253. *
  2254. * @return bool True if posts are available, false if end of loop.
  2255. */
  2256. function have_posts() {
  2257. if ( $this->current_post + 1 < $this->post_count ) {
  2258. return true;
  2259. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  2260. do_action_ref_array('loop_end', array(&$this));
  2261. // Do some cleaning up after the loop
  2262. $this->rewind_posts();
  2263. }
  2264. $this->in_the_loop = false;
  2265. return false;
  2266. }
  2267. /**
  2268. * Rewind the posts and reset post index.
  2269. *
  2270. * @since 1.5.0
  2271. * @access public
  2272. */
  2273. function rewind_posts() {
  2274. $this->current_post = -1;
  2275. if ( $this->post_count > 0 ) {
  2276. $this->post = $this->posts[0];
  2277. }
  2278. }
  2279. /**
  2280. * Iterate current comment index and return comment object.
  2281. *
  2282. * @since 2.2.0
  2283. * @access public
  2284. *
  2285. * @return object Comment object.
  2286. */
  2287. function next_comment() {
  2288. $this->current_comment++;
  2289. $this->comment = $this->comments[$this->current_comment];
  2290. return $this->comment;
  2291. }
  2292. /**
  2293. * Sets up the current comment.
  2294. *
  2295. * @since 2.2.0
  2296. * @access public
  2297. * @global object $comment Current comment.
  2298. * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
  2299. */
  2300. function the_comment() {
  2301. global $comment;
  2302. $comment = $this->next_comment();
  2303. if ( $this->current_comment == 0 ) {
  2304. do_action('comment_loop_start');
  2305. }
  2306. }
  2307. /**
  2308. * Whether there are more comments available.
  2309. *
  2310. * Automatically rewinds comments when finished.
  2311. *
  2312. * @since 2.2.0
  2313. * @access public
  2314. *
  2315. * @return bool True, if more comments. False, if no more posts.
  2316. */
  2317. function have_comments() {
  2318. if ( $this->current_comment + 1 < $this->comment_count ) {
  2319. return true;
  2320. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  2321. $this->rewind_comments();
  2322. }
  2323. return false;
  2324. }
  2325. /**
  2326. * Rewind the comments, resets the comment index and comment to first.
  2327. *
  2328. * @since 2.2.0
  2329. * @access public
  2330. */
  2331. function rewind_comments() {
  2332. $this->current_comment = -1;
  2333. if ( $this->comment_count > 0 ) {
  2334. $this->comment = $this->comments[0];
  2335. }
  2336. }
  2337. /**
  2338. * Sets up the WordPress query by parsing query string.
  2339. *
  2340. * @since 1.5.0
  2341. * @access public
  2342. *
  2343. * @param string $query URL query string.
  2344. * @return array List of posts.
  2345. */
  2346. function &query($query) {
  2347. $this->parse_query($query);
  2348. return $this->get_posts();
  2349. }
  2350. /**
  2351. * Retrieve queried object.
  2352. *
  2353. * If queried object is not set, then the queried object will be set from
  2354. * the category, tag, taxonomy, posts page, single post, page, or author
  2355. * query variable. After it is set up, it will be returned.
  2356. *
  2357. * @since 1.5.0
  2358. * @access public
  2359. *
  2360. * @return object
  2361. */
  2362. function get_queried_object() {
  2363. if ( isset($this->queried_object) )
  2364. return $this->queried_object;
  2365. $this->queried_object = NULL;
  2366. $this->queried_object_id = 0;
  2367. if ( $this->is_category ) {
  2368. $cat = $this->get('cat');
  2369. $category = &get_category($cat);
  2370. if ( is_wp_error( $category ) )
  2371. return NULL;
  2372. $this->queried_object = &$category;
  2373. $this->queried_object_id = (int) $cat;
  2374. } elseif ( $this->is_tag ) {
  2375. $tag_id = $this->get('tag_id');
  2376. $tag = &get_term($tag_id, 'post_tag');
  2377. if ( is_wp_error( $tag ) )
  2378. return NULL;
  2379. $this->queried_object = &$tag;
  2380. $this->queried_object_id = (int) $tag_id;
  2381. } elseif ( $this->is_tax ) {
  2382. $tax = $this->get('taxonomy');
  2383. $slug = $this->get('term');
  2384. $term = &get_terms($tax, array( 'slug' => $slug, 'hide_empty' => false ) );
  2385. if ( is_wp_error($term) || empty($term) )
  2386. return NULL;
  2387. $term = $term[0];
  2388. $this->queried_object = $term;
  2389. $this->queried_object_id = $term->term_id;
  2390. } elseif ( $this->is_posts_page ) {
  2391. $page_for_posts = get_option('page_for_posts');
  2392. $this->queried_object = & get_page( $page_for_posts );
  2393. $this->queried_object_id = (int) $this->queried_object->ID;
  2394. } elseif ( $this->is_single && !is_null($this->post) ) {
  2395. $this->queried_object = $this->post;
  2396. $this->queried_object_id = (int) $this->post->ID;
  2397. } elseif ( $this->is_page && !is_null($this->post) ) {
  2398. $this->queried_object = $this->post;
  2399. $this->queried_object_id = (int) $this->post->ID;
  2400. } elseif ( $this->is_author ) {
  2401. $author_id = (int) $this->get('author');
  2402. $author = get_userdata($author_id);
  2403. $this->queried_object = $author;
  2404. $this->queried_object_id = $author_id;
  2405. }
  2406. return $this->queried_object;
  2407. }
  2408. /**
  2409. * Retrieve ID of the current queried object.
  2410. *
  2411. * @since 1.5.0
  2412. * @access public
  2413. *
  2414. * @return int
  2415. */
  2416. function get_queried_object_id() {
  2417. $this->get_queried_object();
  2418. if ( isset($this->queried_object_id) ) {
  2419. return $this->queried_object_id;
  2420. }
  2421. return 0;
  2422. }
  2423. /**
  2424. * PHP4 type constructor.
  2425. *
  2426. * Sets up the WordPress query, if parameter is not empty.
  2427. *
  2428. * @since 1.5.0
  2429. * @access public
  2430. *
  2431. * @param string $query URL query string.
  2432. * @return WP_Query
  2433. */
  2434. function WP_Query($query = '') {
  2435. if ( ! empty($query) ) {
  2436. $this->query($query);
  2437. }
  2438. }
  2439. }
  2440. /**
  2441. * Redirect old slugs to the correct permalink.
  2442. *
  2443. * Attempts to find the current slug from the past slugs.
  2444. *
  2445. * @since 2.1.0
  2446. * @uses $wp_query
  2447. * @uses $wpdb
  2448. *
  2449. * @return null If no link is found, null is returned.
  2450. */
  2451. function wp_old_slug_redirect() {
  2452. global $wp_query;
  2453. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  2454. global $wpdb;
  2455. $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND meta_key = '_wp_old_slug' AND meta_value='" . $wp_query->query_vars['name'] . "'";
  2456. // if year, monthnum, or day have been specified, make our query more precise
  2457. // just in case there are multiple identical _wp_old_slug values
  2458. if ( '' != $wp_query->query_vars['year'] )
  2459. $query .= " AND YEAR(post_date) = '{$wp_query->query_vars['year']}'";
  2460. if ( '' != $wp_query->query_vars['monthnum'] )
  2461. $query .= " AND MONTH(post_date) = '{$wp_query->query_vars['monthnum']}'";
  2462. if ( '' != $wp_query->query_vars['day'] )
  2463. $query .= " AND DAYOFMONTH(post_date) = '{$wp_query->query_vars['day']}'";
  2464. $id = (int) $wpdb->get_var($query);
  2465. if ( !$id )
  2466. return;
  2467. $link = get_permalink($id);
  2468. if ( !$link )
  2469. return;
  2470. wp_redirect($link, '301'); // Permanent redirect
  2471. exit;
  2472. endif;
  2473. }
  2474. /**
  2475. * Set up global post data.
  2476. *
  2477. * @since 1.5.0
  2478. *
  2479. * @param object $post Post data.
  2480. * @uses do_action_ref_array() Calls 'the_post'
  2481. * @return bool True when finished.
  2482. */
  2483. function setup_postdata($post) {
  2484. global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  2485. $id = (int) $post->ID;
  2486. $authordata = get_userdata($post->post_author);
  2487. $day = mysql2date('d.m.y', $post->post_date, false);
  2488. $currentmonth = mysql2date('m', $post->post_date, false);
  2489. $numpages = 1;
  2490. $page = get_query_var('page');
  2491. if ( !$page )
  2492. $page = 1;
  2493. if ( is_single() || is_page() || is_feed() )
  2494. $more = 1;
  2495. $content = $post->post_content;
  2496. if ( strpos( $content, '<!--nextpage-->' ) ) {
  2497. if ( $page > 1 )
  2498. $more = 1;
  2499. $multipage = 1;
  2500. $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
  2501. $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  2502. $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  2503. $pages = explode('<!--nextpage-->', $content);
  2504. $numpages = count($pages);
  2505. } else {
  2506. $pages = array( $post->post_content );
  2507. $multipage = 0;
  2508. }
  2509. do_action_ref_array('the_post', array(&$post));
  2510. return true;
  2511. }
  2512. ?>