PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/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

Large files files are truncated, but you can click here to view the full file

  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] =

Large files files are truncated, but you can click here to view the full file