PageRenderTime 75ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 4701 lines | 2308 code | 528 blank | 1865 comment | 601 complexity | 14b4c47ebc867b0918a2893eaccfb8ca MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * WordPress Query API
  4. *
  5. * The query API attempts to get which part of WordPress the user is on. It
  6. * also provides functionality for 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. * @param mixed $default Value to return if the query variable is not set. Default ''.
  22. * @return mixed
  23. */
  24. function get_query_var( $var, $default = '' ) {
  25. global $wp_query;
  26. return $wp_query->get( $var, $default );
  27. }
  28. /**
  29. * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
  30. *
  31. * @uses WP_Query::get_queried_object
  32. *
  33. * @since 3.1.0
  34. * @access public
  35. *
  36. * @return object
  37. */
  38. function get_queried_object() {
  39. global $wp_query;
  40. return $wp_query->get_queried_object();
  41. }
  42. /**
  43. * Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
  44. *
  45. * @uses WP_Query::get_queried_object_id()
  46. *
  47. * @since 3.1.0
  48. * @access public
  49. *
  50. * @return int
  51. */
  52. function get_queried_object_id() {
  53. global $wp_query;
  54. return $wp_query->get_queried_object_id();
  55. }
  56. /**
  57. * Set query variable.
  58. *
  59. * @see WP_Query::set()
  60. * @since 2.2.0
  61. * @uses $wp_query
  62. *
  63. * @param string $var Query variable key.
  64. * @param mixed $value
  65. * @return null
  66. */
  67. function set_query_var($var, $value) {
  68. global $wp_query;
  69. return $wp_query->set($var, $value);
  70. }
  71. /**
  72. * Set up The Loop with query parameters.
  73. *
  74. * This will override the current WordPress Loop and shouldn't be used more than
  75. * once. This must not be used within the WordPress Loop.
  76. *
  77. * @since 1.5.0
  78. * @uses $wp_query
  79. *
  80. * @param string $query
  81. * @return array List of posts
  82. */
  83. function query_posts($query) {
  84. $GLOBALS['wp_query'] = new WP_Query();
  85. return $GLOBALS['wp_query']->query($query);
  86. }
  87. /**
  88. * Destroy the previous query and set up a new query.
  89. *
  90. * This should be used after {@link query_posts()} and before another {@link
  91. * query_posts()}. This will remove obscure bugs that occur when the previous
  92. * wp_query object is not destroyed properly before another is set up.
  93. *
  94. * @since 2.3.0
  95. * @uses $wp_query
  96. */
  97. function wp_reset_query() {
  98. $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
  99. wp_reset_postdata();
  100. }
  101. /**
  102. * After looping through a separate query, this function restores
  103. * the $post global to the current post in the main query.
  104. *
  105. * @since 3.0.0
  106. * @uses $wp_query
  107. */
  108. function wp_reset_postdata() {
  109. global $wp_query;
  110. if ( isset( $wp_query ) ) {
  111. $wp_query->reset_postdata();
  112. }
  113. }
  114. /*
  115. * Query type checks.
  116. */
  117. /**
  118. * Is the query for an existing archive page?
  119. *
  120. * Month, Year, Category, Author, Post Type archive...
  121. *
  122. * @see WP_Query::is_archive()
  123. * @since 1.5.0
  124. * @uses $wp_query
  125. *
  126. * @return bool
  127. */
  128. function is_archive() {
  129. global $wp_query;
  130. if ( ! isset( $wp_query ) ) {
  131. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  132. return false;
  133. }
  134. return $wp_query->is_archive();
  135. }
  136. /**
  137. * Is the query for an existing post type archive page?
  138. *
  139. * @see WP_Query::is_post_type_archive()
  140. * @since 3.1.0
  141. * @uses $wp_query
  142. *
  143. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  144. * @return bool
  145. */
  146. function is_post_type_archive( $post_types = '' ) {
  147. global $wp_query;
  148. if ( ! isset( $wp_query ) ) {
  149. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  150. return false;
  151. }
  152. return $wp_query->is_post_type_archive( $post_types );
  153. }
  154. /**
  155. * Is the query for an existing attachment page?
  156. *
  157. * @see WP_Query::is_attachment()
  158. * @since 2.0.0
  159. * @uses $wp_query
  160. *
  161. * @param int|string|array|object $attachment Attachment ID, title, slug, or array of such.
  162. * @return bool
  163. */
  164. function is_attachment( $attachment = '' ) {
  165. global $wp_query;
  166. if ( ! isset( $wp_query ) ) {
  167. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  168. return false;
  169. }
  170. return $wp_query->is_attachment( $attachment );
  171. }
  172. /**
  173. * Is the query for an existing author archive page?
  174. *
  175. * If the $author parameter is specified, this function will additionally
  176. * check if the query is for one of the authors specified.
  177. *
  178. * @see WP_Query::is_author()
  179. * @since 1.5.0
  180. * @uses $wp_query
  181. *
  182. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  183. * @return bool
  184. */
  185. function is_author( $author = '' ) {
  186. global $wp_query;
  187. if ( ! isset( $wp_query ) ) {
  188. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  189. return false;
  190. }
  191. return $wp_query->is_author( $author );
  192. }
  193. /**
  194. * Is the query for an existing category archive page?
  195. *
  196. * If the $category parameter is specified, this function will additionally
  197. * check if the query is for one of the categories specified.
  198. *
  199. * @see WP_Query::is_category()
  200. * @since 1.5.0
  201. * @uses $wp_query
  202. *
  203. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  204. * @return bool
  205. */
  206. function is_category( $category = '' ) {
  207. global $wp_query;
  208. if ( ! isset( $wp_query ) ) {
  209. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  210. return false;
  211. }
  212. return $wp_query->is_category( $category );
  213. }
  214. /**
  215. * Is the query for an existing tag archive page?
  216. *
  217. * If the $tag parameter is specified, this function will additionally
  218. * check if the query is for one of the tags specified.
  219. *
  220. * @see WP_Query::is_tag()
  221. * @since 2.3.0
  222. * @uses $wp_query
  223. *
  224. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  225. * @return bool
  226. */
  227. function is_tag( $tag = '' ) {
  228. global $wp_query;
  229. if ( ! isset( $wp_query ) ) {
  230. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  231. return false;
  232. }
  233. return $wp_query->is_tag( $tag );
  234. }
  235. /**
  236. * Is the query for an existing taxonomy archive page?
  237. *
  238. * If the $taxonomy parameter is specified, this function will additionally
  239. * check if the query is for that specific $taxonomy.
  240. *
  241. * If the $term parameter is specified in addition to the $taxonomy parameter,
  242. * this function will additionally check if the query is for one of the terms
  243. * specified.
  244. *
  245. * @see WP_Query::is_tax()
  246. * @since 2.5.0
  247. * @uses $wp_query
  248. *
  249. * @param string|array $taxonomy Optional. Taxonomy slug or slugs.
  250. * @param int|string|array $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  251. * @return bool
  252. */
  253. function is_tax( $taxonomy = '', $term = '' ) {
  254. global $wp_query;
  255. if ( ! isset( $wp_query ) ) {
  256. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  257. return false;
  258. }
  259. return $wp_query->is_tax( $taxonomy, $term );
  260. }
  261. /**
  262. * Whether the current URL is within the comments popup window.
  263. *
  264. * @see WP_Query::is_comments_popup()
  265. * @since 1.5.0
  266. * @uses $wp_query
  267. *
  268. * @return bool
  269. */
  270. function is_comments_popup() {
  271. global $wp_query;
  272. if ( ! isset( $wp_query ) ) {
  273. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  274. return false;
  275. }
  276. return $wp_query->is_comments_popup();
  277. }
  278. /**
  279. * Is the query for an existing date archive?
  280. *
  281. * @see WP_Query::is_date()
  282. * @since 1.5.0
  283. * @uses $wp_query
  284. *
  285. * @return bool
  286. */
  287. function is_date() {
  288. global $wp_query;
  289. if ( ! isset( $wp_query ) ) {
  290. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  291. return false;
  292. }
  293. return $wp_query->is_date();
  294. }
  295. /**
  296. * Is the query for an existing day archive?
  297. *
  298. * @see WP_Query::is_day()
  299. * @since 1.5.0
  300. * @uses $wp_query
  301. *
  302. * @return bool
  303. */
  304. function is_day() {
  305. global $wp_query;
  306. if ( ! isset( $wp_query ) ) {
  307. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  308. return false;
  309. }
  310. return $wp_query->is_day();
  311. }
  312. /**
  313. * Is the query for a feed?
  314. *
  315. * @see WP_Query::is_feed()
  316. * @since 1.5.0
  317. * @uses $wp_query
  318. *
  319. * @param string|array $feeds Optional feed types to check.
  320. * @return bool
  321. */
  322. function is_feed( $feeds = '' ) {
  323. global $wp_query;
  324. if ( ! isset( $wp_query ) ) {
  325. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  326. return false;
  327. }
  328. return $wp_query->is_feed( $feeds );
  329. }
  330. /**
  331. * Is the query for a comments feed?
  332. *
  333. * @see WP_Query::is_comments_feed()
  334. * @since 3.0.0
  335. * @uses $wp_query
  336. *
  337. * @return bool
  338. */
  339. function is_comment_feed() {
  340. global $wp_query;
  341. if ( ! isset( $wp_query ) ) {
  342. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  343. return false;
  344. }
  345. return $wp_query->is_comment_feed();
  346. }
  347. /**
  348. * Is the query for the front page of the site?
  349. *
  350. * This is for what is displayed at your site's main URL.
  351. *
  352. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  353. *
  354. * If you set a static page for the front page of your site, this function will return
  355. * true when viewing that page.
  356. *
  357. * Otherwise the same as @see is_home()
  358. *
  359. * @see WP_Query::is_front_page()
  360. * @since 2.5.0
  361. *
  362. * @return bool True, if front of site.
  363. */
  364. function is_front_page() {
  365. global $wp_query;
  366. if ( ! isset( $wp_query ) ) {
  367. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  368. return false;
  369. }
  370. return $wp_query->is_front_page();
  371. }
  372. /**
  373. * Is the query for the blog homepage?
  374. *
  375. * This is the page which shows the time based blog content of your site.
  376. *
  377. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  378. *
  379. * If you set a static page for the front page of your site, this function will return
  380. * true only on the page you set as the "Posts page".
  381. *
  382. * @see is_front_page()
  383. *
  384. * @see WP_Query::is_home()
  385. * @since 1.5.0
  386. * @uses $wp_query
  387. *
  388. * @return bool True if blog view homepage.
  389. */
  390. function is_home() {
  391. global $wp_query;
  392. if ( ! isset( $wp_query ) ) {
  393. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  394. return false;
  395. }
  396. return $wp_query->is_home();
  397. }
  398. /**
  399. * Is the query for an existing month archive?
  400. *
  401. * @see WP_Query::is_month()
  402. * @since 1.5.0
  403. * @uses $wp_query
  404. *
  405. * @return bool
  406. */
  407. function is_month() {
  408. global $wp_query;
  409. if ( ! isset( $wp_query ) ) {
  410. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  411. return false;
  412. }
  413. return $wp_query->is_month();
  414. }
  415. /**
  416. * Is the query for an existing single page?
  417. *
  418. * If the $page parameter is specified, this function will additionally
  419. * check if the query is for one of the pages specified.
  420. *
  421. * @see is_single()
  422. * @see is_singular()
  423. *
  424. * @see WP_Query::is_page()
  425. * @since 1.5.0
  426. * @uses $wp_query
  427. *
  428. * @param mixed $page Page ID, title, slug, or array of such.
  429. * @return bool
  430. */
  431. function is_page( $page = '' ) {
  432. global $wp_query;
  433. if ( ! isset( $wp_query ) ) {
  434. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  435. return false;
  436. }
  437. return $wp_query->is_page( $page );
  438. }
  439. /**
  440. * Is the query for paged result and not for the first page?
  441. *
  442. * @see WP_Query::is_paged()
  443. * @since 1.5.0
  444. * @uses $wp_query
  445. *
  446. * @return bool
  447. */
  448. function is_paged() {
  449. global $wp_query;
  450. if ( ! isset( $wp_query ) ) {
  451. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  452. return false;
  453. }
  454. return $wp_query->is_paged();
  455. }
  456. /**
  457. * Is the query for a post or page preview?
  458. *
  459. * @see WP_Query::is_preview()
  460. * @since 2.0.0
  461. * @uses $wp_query
  462. *
  463. * @return bool
  464. */
  465. function is_preview() {
  466. global $wp_query;
  467. if ( ! isset( $wp_query ) ) {
  468. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  469. return false;
  470. }
  471. return $wp_query->is_preview();
  472. }
  473. /**
  474. * Is the query for the robots file?
  475. *
  476. * @see WP_Query::is_robots()
  477. * @since 2.1.0
  478. * @uses $wp_query
  479. *
  480. * @return bool
  481. */
  482. function is_robots() {
  483. global $wp_query;
  484. if ( ! isset( $wp_query ) ) {
  485. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  486. return false;
  487. }
  488. return $wp_query->is_robots();
  489. }
  490. /**
  491. * Is the query for a search?
  492. *
  493. * @see WP_Query::is_search()
  494. * @since 1.5.0
  495. * @uses $wp_query
  496. *
  497. * @return bool
  498. */
  499. function is_search() {
  500. global $wp_query;
  501. if ( ! isset( $wp_query ) ) {
  502. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  503. return false;
  504. }
  505. return $wp_query->is_search();
  506. }
  507. /**
  508. * Is the query for an existing single post?
  509. *
  510. * Works for any post type, except attachments and pages
  511. *
  512. * If the $post parameter is specified, this function will additionally
  513. * check if the query is for one of the Posts specified.
  514. *
  515. * @see is_page()
  516. * @see is_singular()
  517. *
  518. * @see WP_Query::is_single()
  519. * @since 1.5.0
  520. * @uses $wp_query
  521. *
  522. * @param mixed $post Post ID, title, slug, or array of such.
  523. * @return bool
  524. */
  525. function is_single( $post = '' ) {
  526. global $wp_query;
  527. if ( ! isset( $wp_query ) ) {
  528. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  529. return false;
  530. }
  531. return $wp_query->is_single( $post );
  532. }
  533. /**
  534. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  535. *
  536. * If the $post_types parameter is specified, this function will additionally
  537. * check if the query is for one of the Posts Types specified.
  538. *
  539. * @see is_page()
  540. * @see is_single()
  541. *
  542. * @see WP_Query::is_singular()
  543. * @since 1.5.0
  544. * @uses $wp_query
  545. *
  546. * @param mixed $post_types Optional. Post Type or array of Post Types
  547. * @return bool
  548. */
  549. function is_singular( $post_types = '' ) {
  550. global $wp_query;
  551. if ( ! isset( $wp_query ) ) {
  552. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  553. return false;
  554. }
  555. return $wp_query->is_singular( $post_types );
  556. }
  557. /**
  558. * Is the query for a specific time?
  559. *
  560. * @see WP_Query::is_time()
  561. * @since 1.5.0
  562. * @uses $wp_query
  563. *
  564. * @return bool
  565. */
  566. function is_time() {
  567. global $wp_query;
  568. if ( ! isset( $wp_query ) ) {
  569. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  570. return false;
  571. }
  572. return $wp_query->is_time();
  573. }
  574. /**
  575. * Is the query for a trackback endpoint call?
  576. *
  577. * @see WP_Query::is_trackback()
  578. * @since 1.5.0
  579. * @uses $wp_query
  580. *
  581. * @return bool
  582. */
  583. function is_trackback() {
  584. global $wp_query;
  585. if ( ! isset( $wp_query ) ) {
  586. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  587. return false;
  588. }
  589. return $wp_query->is_trackback();
  590. }
  591. /**
  592. * Is the query for an existing year archive?
  593. *
  594. * @see WP_Query::is_year()
  595. * @since 1.5.0
  596. * @uses $wp_query
  597. *
  598. * @return bool
  599. */
  600. function is_year() {
  601. global $wp_query;
  602. if ( ! isset( $wp_query ) ) {
  603. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  604. return false;
  605. }
  606. return $wp_query->is_year();
  607. }
  608. /**
  609. * Is the query a 404 (returns no results)?
  610. *
  611. * @see WP_Query::is_404()
  612. * @since 1.5.0
  613. * @uses $wp_query
  614. *
  615. * @return bool
  616. */
  617. function is_404() {
  618. global $wp_query;
  619. if ( ! isset( $wp_query ) ) {
  620. _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
  621. return false;
  622. }
  623. return $wp_query->is_404();
  624. }
  625. /**
  626. * Is the query the main query?
  627. *
  628. * @since 3.3.0
  629. *
  630. * @return bool
  631. */
  632. function is_main_query() {
  633. if ( 'pre_get_posts' === current_filter() ) {
  634. $message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
  635. 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'http://codex.wordpress.org/Function_Reference/is_main_query' ) );
  636. _doing_it_wrong( __FUNCTION__, $message, '3.7' );
  637. }
  638. global $wp_query;
  639. return $wp_query->is_main_query();
  640. }
  641. /*
  642. * The Loop. Post loop control.
  643. */
  644. /**
  645. * Whether current WordPress query has results to loop over.
  646. *
  647. * @see WP_Query::have_posts()
  648. * @since 1.5.0
  649. * @uses $wp_query
  650. *
  651. * @return bool
  652. */
  653. function have_posts() {
  654. global $wp_query;
  655. return $wp_query->have_posts();
  656. }
  657. /**
  658. * Whether the caller is in the Loop.
  659. *
  660. * @since 2.0.0
  661. * @uses $wp_query
  662. *
  663. * @return bool True if caller is within loop, false if loop hasn't started or ended.
  664. */
  665. function in_the_loop() {
  666. global $wp_query;
  667. return $wp_query->in_the_loop;
  668. }
  669. /**
  670. * Rewind the loop posts.
  671. *
  672. * @see WP_Query::rewind_posts()
  673. * @since 1.5.0
  674. * @uses $wp_query
  675. *
  676. * @return null
  677. */
  678. function rewind_posts() {
  679. global $wp_query;
  680. return $wp_query->rewind_posts();
  681. }
  682. /**
  683. * Iterate the post index in the loop.
  684. *
  685. * @see WP_Query::the_post()
  686. * @since 1.5.0
  687. * @uses $wp_query
  688. */
  689. function the_post() {
  690. global $wp_query;
  691. $wp_query->the_post();
  692. }
  693. /*
  694. * Comments loop.
  695. */
  696. /**
  697. * Whether there are comments to loop over.
  698. *
  699. * @see WP_Query::have_comments()
  700. * @since 2.2.0
  701. * @uses $wp_query
  702. *
  703. * @return bool
  704. */
  705. function have_comments() {
  706. global $wp_query;
  707. return $wp_query->have_comments();
  708. }
  709. /**
  710. * Iterate comment index in the comment loop.
  711. *
  712. * @see WP_Query::the_comment()
  713. * @since 2.2.0
  714. * @uses $wp_query
  715. *
  716. * @return object
  717. */
  718. function the_comment() {
  719. global $wp_query;
  720. return $wp_query->the_comment();
  721. }
  722. /*
  723. * WP_Query
  724. */
  725. /**
  726. * The WordPress Query class.
  727. *
  728. * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  729. *
  730. * @since 1.5.0
  731. */
  732. class WP_Query {
  733. /**
  734. * Query vars set by the user
  735. *
  736. * @since 1.5.0
  737. * @access public
  738. * @var array
  739. */
  740. public $query;
  741. /**
  742. * Query vars, after parsing
  743. *
  744. * @since 1.5.0
  745. * @access public
  746. * @var array
  747. */
  748. public $query_vars = array();
  749. /**
  750. * Taxonomy query, as passed to get_tax_sql()
  751. *
  752. * @since 3.1.0
  753. * @access public
  754. * @var object WP_Tax_Query
  755. */
  756. public $tax_query;
  757. /**
  758. * Metadata query container
  759. *
  760. * @since 3.2.0
  761. * @access public
  762. * @var object WP_Meta_Query
  763. */
  764. public $meta_query = false;
  765. /**
  766. * Date query container
  767. *
  768. * @since 3.7.0
  769. * @access public
  770. * @var object WP_Date_Query
  771. */
  772. public $date_query = false;
  773. /**
  774. * Holds the data for a single object that is queried.
  775. *
  776. * Holds the contents of a post, page, category, attachment.
  777. *
  778. * @since 1.5.0
  779. * @access public
  780. * @var object|array
  781. */
  782. public $queried_object;
  783. /**
  784. * The ID of the queried object.
  785. *
  786. * @since 1.5.0
  787. * @access public
  788. * @var int
  789. */
  790. public $queried_object_id;
  791. /**
  792. * Get post database query.
  793. *
  794. * @since 2.0.1
  795. * @access public
  796. * @var string
  797. */
  798. public $request;
  799. /**
  800. * List of posts.
  801. *
  802. * @since 1.5.0
  803. * @access public
  804. * @var array
  805. */
  806. public $posts;
  807. /**
  808. * The amount of posts for the current query.
  809. *
  810. * @since 1.5.0
  811. * @access public
  812. * @var int
  813. */
  814. public $post_count = 0;
  815. /**
  816. * Index of the current item in the loop.
  817. *
  818. * @since 1.5.0
  819. * @access public
  820. * @var int
  821. */
  822. public $current_post = -1;
  823. /**
  824. * Whether the loop has started and the caller is in the loop.
  825. *
  826. * @since 2.0.0
  827. * @access public
  828. * @var bool
  829. */
  830. public $in_the_loop = false;
  831. /**
  832. * The current post.
  833. *
  834. * @since 1.5.0
  835. * @access public
  836. * @var WP_Post
  837. */
  838. public $post;
  839. /**
  840. * The list of comments for current post.
  841. *
  842. * @since 2.2.0
  843. * @access public
  844. * @var array
  845. */
  846. public $comments;
  847. /**
  848. * The amount of comments for the posts.
  849. *
  850. * @since 2.2.0
  851. * @access public
  852. * @var int
  853. */
  854. public $comment_count = 0;
  855. /**
  856. * The index of the comment in the comment loop.
  857. *
  858. * @since 2.2.0
  859. * @access public
  860. * @var int
  861. */
  862. public $current_comment = -1;
  863. /**
  864. * Current comment ID.
  865. *
  866. * @since 2.2.0
  867. * @access public
  868. * @var int
  869. */
  870. public $comment;
  871. /**
  872. * The amount of found posts for the current query.
  873. *
  874. * If limit clause was not used, equals $post_count.
  875. *
  876. * @since 2.1.0
  877. * @access public
  878. * @var int
  879. */
  880. public $found_posts = 0;
  881. /**
  882. * The amount of pages.
  883. *
  884. * @since 2.1.0
  885. * @access public
  886. * @var int
  887. */
  888. public $max_num_pages = 0;
  889. /**
  890. * The amount of comment pages.
  891. *
  892. * @since 2.7.0
  893. * @access public
  894. * @var int
  895. */
  896. public $max_num_comment_pages = 0;
  897. /**
  898. * Set if query is single post.
  899. *
  900. * @since 1.5.0
  901. * @access public
  902. * @var bool
  903. */
  904. public $is_single = false;
  905. /**
  906. * Set if query is preview of blog.
  907. *
  908. * @since 2.0.0
  909. * @access public
  910. * @var bool
  911. */
  912. public $is_preview = false;
  913. /**
  914. * Set if query returns a page.
  915. *
  916. * @since 1.5.0
  917. * @access public
  918. * @var bool
  919. */
  920. public $is_page = false;
  921. /**
  922. * Set if query is an archive list.
  923. *
  924. * @since 1.5.0
  925. * @access public
  926. * @var bool
  927. */
  928. public $is_archive = false;
  929. /**
  930. * Set if query is part of a date.
  931. *
  932. * @since 1.5.0
  933. * @access public
  934. * @var bool
  935. */
  936. public $is_date = false;
  937. /**
  938. * Set if query contains a year.
  939. *
  940. * @since 1.5.0
  941. * @access public
  942. * @var bool
  943. */
  944. public $is_year = false;
  945. /**
  946. * Set if query contains a month.
  947. *
  948. * @since 1.5.0
  949. * @access public
  950. * @var bool
  951. */
  952. public $is_month = false;
  953. /**
  954. * Set if query contains a day.
  955. *
  956. * @since 1.5.0
  957. * @access public
  958. * @var bool
  959. */
  960. public $is_day = false;
  961. /**
  962. * Set if query contains time.
  963. *
  964. * @since 1.5.0
  965. * @access public
  966. * @var bool
  967. */
  968. public $is_time = false;
  969. /**
  970. * Set if query contains an author.
  971. *
  972. * @since 1.5.0
  973. * @access public
  974. * @var bool
  975. */
  976. public $is_author = false;
  977. /**
  978. * Set if query contains category.
  979. *
  980. * @since 1.5.0
  981. * @access public
  982. * @var bool
  983. */
  984. public $is_category = false;
  985. /**
  986. * Set if query contains tag.
  987. *
  988. * @since 2.3.0
  989. * @access public
  990. * @var bool
  991. */
  992. public $is_tag = false;
  993. /**
  994. * Set if query contains taxonomy.
  995. *
  996. * @since 2.5.0
  997. * @access public
  998. * @var bool
  999. */
  1000. public $is_tax = false;
  1001. /**
  1002. * Set if query was part of a search result.
  1003. *
  1004. * @since 1.5.0
  1005. * @access public
  1006. * @var bool
  1007. */
  1008. public $is_search = false;
  1009. /**
  1010. * Set if query is feed display.
  1011. *
  1012. * @since 1.5.0
  1013. * @access public
  1014. * @var bool
  1015. */
  1016. public $is_feed = false;
  1017. /**
  1018. * Set if query is comment feed display.
  1019. *
  1020. * @since 2.2.0
  1021. * @access public
  1022. * @var bool
  1023. */
  1024. public $is_comment_feed = false;
  1025. /**
  1026. * Set if query is trackback.
  1027. *
  1028. * @since 1.5.0
  1029. * @access public
  1030. * @var bool
  1031. */
  1032. public $is_trackback = false;
  1033. /**
  1034. * Set if query is blog homepage.
  1035. *
  1036. * @since 1.5.0
  1037. * @access public
  1038. * @var bool
  1039. */
  1040. public $is_home = false;
  1041. /**
  1042. * Set if query couldn't found anything.
  1043. *
  1044. * @since 1.5.0
  1045. * @access public
  1046. * @var bool
  1047. */
  1048. public $is_404 = false;
  1049. /**
  1050. * Set if query is within comments popup window.
  1051. *
  1052. * @since 1.5.0
  1053. * @access public
  1054. * @var bool
  1055. */
  1056. public $is_comments_popup = false;
  1057. /**
  1058. * Set if query is paged
  1059. *
  1060. * @since 1.5.0
  1061. * @access public
  1062. * @var bool
  1063. */
  1064. public $is_paged = false;
  1065. /**
  1066. * Set if query is part of administration page.
  1067. *
  1068. * @since 1.5.0
  1069. * @access public
  1070. * @var bool
  1071. */
  1072. public $is_admin = false;
  1073. /**
  1074. * Set if query is an attachment.
  1075. *
  1076. * @since 2.0.0
  1077. * @access public
  1078. * @var bool
  1079. */
  1080. public $is_attachment = false;
  1081. /**
  1082. * Set if is single, is a page, or is an attachment.
  1083. *
  1084. * @since 2.1.0
  1085. * @access public
  1086. * @var bool
  1087. */
  1088. public $is_singular = false;
  1089. /**
  1090. * Set if query is for robots.
  1091. *
  1092. * @since 2.1.0
  1093. * @access public
  1094. * @var bool
  1095. */
  1096. public $is_robots = false;
  1097. /**
  1098. * Set if query contains posts.
  1099. *
  1100. * Basically, the homepage if the option isn't set for the static homepage.
  1101. *
  1102. * @since 2.1.0
  1103. * @access public
  1104. * @var bool
  1105. */
  1106. public $is_posts_page = false;
  1107. /**
  1108. * Set if query is for a post type archive.
  1109. *
  1110. * @since 3.1.0
  1111. * @access public
  1112. * @var bool
  1113. */
  1114. public $is_post_type_archive = false;
  1115. /**
  1116. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  1117. * whether we have to re-parse because something has changed
  1118. *
  1119. * @since 3.1.0
  1120. * @access private
  1121. */
  1122. private $query_vars_hash = false;
  1123. /**
  1124. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  1125. * via pre_get_posts hooks.
  1126. *
  1127. * @since 3.1.1
  1128. * @access private
  1129. */
  1130. private $query_vars_changed = true;
  1131. /**
  1132. * Set if post thumbnails are cached
  1133. *
  1134. * @since 3.2.0
  1135. * @access public
  1136. * @var bool
  1137. */
  1138. public $thumbnails_cached = false;
  1139. /**
  1140. * Cached list of search stopwords.
  1141. *
  1142. * @since 3.7.0
  1143. * @var array
  1144. */
  1145. private $stopwords;
  1146. /**
  1147. * Resets query flags to false.
  1148. *
  1149. * The query flags are what page info WordPress was able to figure out.
  1150. *
  1151. * @since 2.0.0
  1152. * @access private
  1153. */
  1154. private function init_query_flags() {
  1155. $this->is_single = false;
  1156. $this->is_preview = false;
  1157. $this->is_page = false;
  1158. $this->is_archive = false;
  1159. $this->is_date = false;
  1160. $this->is_year = false;
  1161. $this->is_month = false;
  1162. $this->is_day = false;
  1163. $this->is_time = false;
  1164. $this->is_author = false;
  1165. $this->is_category = false;
  1166. $this->is_tag = false;
  1167. $this->is_tax = false;
  1168. $this->is_search = false;
  1169. $this->is_feed = false;
  1170. $this->is_comment_feed = false;
  1171. $this->is_trackback = false;
  1172. $this->is_home = false;
  1173. $this->is_404 = false;
  1174. $this->is_comments_popup = false;
  1175. $this->is_paged = false;
  1176. $this->is_admin = false;
  1177. $this->is_attachment = false;
  1178. $this->is_singular = false;
  1179. $this->is_robots = false;
  1180. $this->is_posts_page = false;
  1181. $this->is_post_type_archive = false;
  1182. }
  1183. /**
  1184. * Initiates object properties and sets default values.
  1185. *
  1186. * @since 1.5.0
  1187. * @access public
  1188. */
  1189. public function init() {
  1190. unset($this->posts);
  1191. unset($this->query);
  1192. $this->query_vars = array();
  1193. unset($this->queried_object);
  1194. unset($this->queried_object_id);
  1195. $this->post_count = 0;
  1196. $this->current_post = -1;
  1197. $this->in_the_loop = false;
  1198. unset( $this->request );
  1199. unset( $this->post );
  1200. unset( $this->comments );
  1201. unset( $this->comment );
  1202. $this->comment_count = 0;
  1203. $this->current_comment = -1;
  1204. $this->found_posts = 0;
  1205. $this->max_num_pages = 0;
  1206. $this->max_num_comment_pages = 0;
  1207. $this->init_query_flags();
  1208. }
  1209. /**
  1210. * Reparse the query vars.
  1211. *
  1212. * @since 1.5.0
  1213. * @access public
  1214. */
  1215. public function parse_query_vars() {
  1216. $this->parse_query();
  1217. }
  1218. /**
  1219. * Fills in the query variables, which do not exist within the parameter.
  1220. *
  1221. * @since 2.1.0
  1222. * @access public
  1223. *
  1224. * @param array $array Defined query variables.
  1225. * @return array Complete query variables with undefined ones filled in empty.
  1226. */
  1227. public function fill_query_vars($array) {
  1228. $keys = array(
  1229. 'error'
  1230. , 'm'
  1231. , 'p'
  1232. , 'post_parent'
  1233. , 'subpost'
  1234. , 'subpost_id'
  1235. , 'attachment'
  1236. , 'attachment_id'
  1237. , 'name'
  1238. , 'static'
  1239. , 'pagename'
  1240. , 'page_id'
  1241. , 'second'
  1242. , 'minute'
  1243. , 'hour'
  1244. , 'day'
  1245. , 'monthnum'
  1246. , 'year'
  1247. , 'w'
  1248. , 'category_name'
  1249. , 'tag'
  1250. , 'cat'
  1251. , 'tag_id'
  1252. , 'author'
  1253. , 'author_name'
  1254. , 'feed'
  1255. , 'tb'
  1256. , 'paged'
  1257. , 'comments_popup'
  1258. , 'meta_key'
  1259. , 'meta_value'
  1260. , 'preview'
  1261. , 's'
  1262. , 'sentence'
  1263. , 'fields'
  1264. , 'menu_order'
  1265. );
  1266. foreach ( $keys as $key ) {
  1267. if ( !isset($array[$key]) )
  1268. $array[$key] = '';
  1269. }
  1270. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in',
  1271. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
  1272. 'author__in', 'author__not_in' );
  1273. foreach ( $array_keys as $key ) {
  1274. if ( !isset($array[$key]) )
  1275. $array[$key] = array();
  1276. }
  1277. return $array;
  1278. }
  1279. /**
  1280. * Parse a query string and set query type booleans.
  1281. *
  1282. * @since 1.5.0
  1283. * @access public
  1284. *
  1285. * @param string|array $query {
  1286. * Optional. Array or string of Query parameters.
  1287. *
  1288. * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type.
  1289. * @type int|string $author Author ID, or comma-separated list of IDs.
  1290. * @type string $author_name User 'user_nicename'.
  1291. * @type array $author__in An array of author IDs to query from.
  1292. * @type array $author__not_in An array of author IDs not to query from.
  1293. * @type bool $cache_results Whether to cache post information. Default true.
  1294. * @type int|string $cat Category ID or comma-separated list of IDs (this or any children).
  1295. * @type array $category__and An array of category IDs (AND in).
  1296. * @type array $category__in An array of category IDs (OR in, no children).
  1297. * @type array $category__not_in An array of category IDs (NOT in).
  1298. * @type string $category_name Use category slug (not name, this or any children).
  1299. * @type int $comments_per_page The number of comments to return per page.
  1300. * Default 'comments_per_page' option.
  1301. * @type int|string $comments_popup Whether the query is within the comments popup. Default empty.
  1302. * @type array $date_query An associative array of WP_Date_Query arguments.
  1303. * {@see WP_Date_Query::__construct()}
  1304. * @type int $day Day of the month. Default empty. Accepts numbers 1-31.
  1305. * @type bool $exact Whether to search by exact keyword. Default false.
  1306. * @type string|array $fields Which fields to return. Single field or all fields (string),
  1307. * or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
  1308. * Default all fields. Accepts 'ids', 'id=>parent'.
  1309. * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23.
  1310. * @type bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
  1311. * excludes stickies from 'post__in'. Accepts 1|true, 0|false.
  1312. * Default 0|false.
  1313. * @type int $m Combination YearMonth. Accepts any four-digit year and month
  1314. * numbers 1-12. Default empty.
  1315. * @type string $meta_compare Comparison operator to test the 'meta_value'.
  1316. * @type string $meta_key Custom field key.
  1317. * @type array $meta_query An associative array of WP_Meta_Query arguments.
  1318. * {@see WP_Meta_Query->queries}
  1319. * @type string $meta_value Custom field value.
  1320. * @type int $meta_value_num Custom field value number.
  1321. * @type int $menu_order The menu order of the posts.
  1322. * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12.
  1323. * @type string $name Post slug.
  1324. * @type bool $nopaging Show all posts (true) or paginate (false). Default false.
  1325. * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve
  1326. * performance. Default false.
  1327. * @type int $offset The number of posts to offset before retrieval.
  1328. * @type string $order Designates ascending or descending order of posts. Default 'DESC'.
  1329. * Accepts 'ASC', 'DESC'.
  1330. * @type string $orderby Sort retrieved posts by parameter. One or more options can be
  1331. * passed. To use 'meta_value', or 'meta_value_num',
  1332. * 'meta_key=keyname' must be also be defined. Default 'date'.
  1333. * Accepts 'none', 'name', 'author', 'date', 'title', 'modified',
  1334. * 'menu_order', 'parent', 'ID', 'rand', 'comment_count'.
  1335. * @type int $p Post ID.
  1336. * @type int $page Show the number of posts that would show up on page X of a
  1337. * static front page.
  1338. * @type int $paged The number of the current page.
  1339. * @type int $page_id Page ID.
  1340. * @type string $pagename Page slug.
  1341. * @type string $perm Show posts if user has the appropriate capability.
  1342. * @type array $post__in An array of post IDs to retrieve, sticky posts will be included
  1343. * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type.
  1344. * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma-
  1345. * separated IDs will NOT work.
  1346. * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve
  1347. * top-level pages.
  1348. * @type array $post_parent__in An array containing parent page IDs to query child pages from.
  1349. * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from.
  1350. * @type string|array $post_type A post type slug (string) or array of post type slugs.
  1351. * Default 'any' if using 'tax_query'.
  1352. * @type string|array $post_status A post status (string) or array of post statuses.
  1353. * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts.
  1354. * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
  1355. * 'posts_per_page' when is_archive(), or is_search() are true.
  1356. * @type string $s Search keyword.
  1357. * @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
  1358. * @type array $search_terms Array of search terms.
  1359. * @type bool $sentence Whether to search by phrase. Default false.
  1360. * @type bool $suppress_filters Whether to suppress filters. Default false.
  1361. * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all).
  1362. * @type array $tag__and An array of tag ids (AND in).
  1363. * @type array $tag__in An array of tag ids (OR in).
  1364. * @type array $tag__not_in An array of tag ids (NOT in).
  1365. * @type int $tag_id Tag id or comma-separated list of IDs.
  1366. * @type array $tag_slug__and An array of tag slugs (AND in).
  1367. * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is
  1368. * true. Note: a string of comma-separated IDs will NOT work.
  1369. * @type array $tax_query An associative array of WP_Tax_Query arguments.
  1370. * {@see WP_Tax_Query->queries}
  1371. * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
  1372. * @type bool $update_post_term_cache Whether to update the post term cache. Default true.
  1373. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
  1374. * @type int $year The four-digit year. Default empty. Accepts any four-digit year.
  1375. * }
  1376. */
  1377. public function parse_query( $query = '' ) {
  1378. if ( ! empty( $query ) ) {
  1379. $this->init();
  1380. $this->query = $this->query_vars = wp_parse_args( $query );
  1381. } elseif ( ! isset( $this->query ) ) {
  1382. $this->query = $this->query_vars;
  1383. }
  1384. $this->query_vars = $this->fill_query_vars($this->query_vars);
  1385. $qv = &$this->query_vars;
  1386. $this->query_vars_changed = true;
  1387. if ( ! empty($qv['robots']) )
  1388. $this->is_robots = true;
  1389. $qv['p'] = absint($qv['p']);
  1390. $qv['page_id'] = absint($qv['page_id']);
  1391. $qv['year'] = absint($qv['year']);
  1392. $qv['monthnum'] = absint($qv['monthnum']);
  1393. $qv['day'] = absint($qv['day']);
  1394. $qv['w'] = absint($qv['w']);
  1395. $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] );
  1396. $qv['paged'] = absint($qv['paged']);
  1397. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  1398. $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
  1399. $qv['pagename'] = trim( $qv['pagename'] );
  1400. $qv['name'] = trim( $qv['name'] );
  1401. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  1402. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  1403. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  1404. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  1405. // Fairly insane upper bound for search string lengths.
  1406. if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
  1407. $qv['s'] = '';
  1408. }
  1409. // Compat. Map subpost to attachment.
  1410. if ( '' != $qv['subpost'] )
  1411. $qv['attachment'] = $qv['subpost'];
  1412. if ( '' != $qv['subpost_id'] )
  1413. $qv['attachment_id'] = $qv['subpost_id'];
  1414. $qv['attachment_id'] = absint($qv['attachment_id']);
  1415. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  1416. $this->is_single = true;
  1417. $this->is_attachment = true;
  1418. } elseif ( '' != $qv['name'] ) {
  1419. $this->is_single = true;
  1420. } elseif ( $qv['p'] ) {
  1421. $this->is_single = true;
  1422. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  1423. // If year, month, day, hour, minute, and second are set, a single
  1424. // post is being queried.
  1425. $this->is_single = true;
  1426. } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  1427. $this->is_page = true;
  1428. $this->is_single = false;
  1429. } else {
  1430. // Look for archive queries. Dates, categories, authors, search, post type archives.
  1431. if ( isset( $this->query['s'] ) ) {
  1432. $this->is_search = true;
  1433. }
  1434. if ( '' !== $qv['second'] ) {
  1435. $this->is_time = true;
  1436. $this->is_date = true;
  1437. }
  1438. if ( '' !== $qv['minute'] ) {
  1439. $this->is_time = true;
  1440. $this->is_date = true;
  1441. }
  1442. if ( '' !== $qv['hour'] ) {
  1443. $this->is_time = true;
  1444. $this->is_date = true;
  1445. }
  1446. if ( $qv['day'] ) {
  1447. if ( ! $this->is_date ) {
  1448. $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
  1449. if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
  1450. $qv['error'] = '404';
  1451. } else {
  1452. $this->is_day = true;
  1453. $this->is_date = true;
  1454. }
  1455. }
  1456. }
  1457. if ( $qv['monthnum'] ) {
  1458. if ( ! $this->is_date ) {
  1459. if ( 12 < $qv['monthnum'] ) {
  1460. $qv['error'] = '404';
  1461. } else {
  1462. $this->is_month = true;
  1463. $this->is_date = true;
  1464. }
  1465. }
  1466. }
  1467. if ( $qv['year'] ) {
  1468. if ( ! $this->is_date ) {
  1469. $this->is_year = true;
  1470. $this->is_date = true;
  1471. }
  1472. }
  1473. if ( $qv['m'] ) {
  1474. $this->is_date = true;
  1475. if ( strlen($qv['m']) > 9 ) {
  1476. $this->is_time = true;
  1477. } else if ( strlen($qv['m']) > 7 ) {
  1478. $this->is_day = true;
  1479. } else if ( strlen($qv['m']) > 5 ) {
  1480. $this->is_month = true;
  1481. } else {
  1482. $this->is_year = true;
  1483. }
  1484. }
  1485. if ( '' != $qv['w'] ) {
  1486. $this->is_date = true;
  1487. }
  1488. $this->query_vars_hash = false;
  1489. $this->parse_tax_query( $qv );
  1490. foreach ( $this->tax_query->queries as $tax_query ) {
  1491. if ( ! is_array( $tax_query ) ) {
  1492. continue;
  1493. }
  1494. if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
  1495. switch ( $tax_query['taxonomy'] ) {
  1496. case 'category':
  1497. $this->is_category = true;
  1498. break;
  1499. case 'post_tag':
  1500. $this->is_tag = true;
  1501. break;
  1502. default:
  1503. $this->is_tax = true;
  1504. }
  1505. }
  1506. }
  1507. unset( $tax_query );
  1508. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  1509. $this->is_author = false;
  1510. } else {
  1511. $this->is_author = true;
  1512. }
  1513. if ( '' != $qv['author_name'] )
  1514. $this->is_author = true;
  1515. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  1516. $post_type_obj = get_post_type_object( $qv['post_type'] );
  1517. if ( ! empty( $post_type_obj->has_archive ) )
  1518. $this->is_post_type_archive = true;
  1519. }
  1520. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  1521. $this->is_archive = true;
  1522. }
  1523. if ( '' != $qv['feed'] )
  1524. $this->is_feed = true;
  1525. if ( '' != $qv['tb'] )
  1526. $this->is_trackback = true;
  1527. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  1528. $this->is_paged = true;
  1529. if ( '' != $qv['comments_popup'] )
  1530. $this->is_comments_popup = true;
  1531. // if we're previewing inside the write screen
  1532. if ( '' != $qv['preview'] )
  1533. $this->is_preview = true;
  1534. if ( is_admin() )
  1535. $this->is_admin = true;
  1536. if ( false !== strpos($qv['feed'], 'comments-') ) {
  1537. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  1538. $qv['withcomments'] = 1;
  1539. }
  1540. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1541. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  1542. $this->is_comment_feed = true;
  1543. 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 ) )
  1544. $this->is_home = true;
  1545. // Correct is_* for page_on_front and page_for_posts
  1546. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  1547. $_query = wp_parse_args($this->query);
  1548. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  1549. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  1550. unset($_query['pagename']);
  1551. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  1552. $this->is_page = true;
  1553. $this->is_home = false;
  1554. $qv['page_id'] = get_option('page_on_front');
  1555. // Correct <!--nextpage--> for page_on_front
  1556. if ( !empty($qv['paged']) ) {
  1557. $qv['page'] = $qv['paged'];
  1558. unset($qv['paged']);
  1559. }
  1560. }
  1561. }
  1562. if ( '' != $qv['pagename'] ) {
  1563. $this->queried_object = get_page_by_path($qv['pagename']);
  1564. if ( !empty($this->queried_object) )
  1565. $this->queried_object_id = (int) $this->queried_object->ID;
  1566. else
  1567. unset($this->queried_object);
  1568. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  1569. $this->is_page = false;
  1570. $this->is_home = true;
  1571. $this->is_posts_page = true;
  1572. }
  1573. }
  1574. if ( $qv['page_id'] ) {
  1575. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  1576. $this->is_page = false;
  1577. $this->is_home = true;
  1578. $this->is_posts_page = true;
  1579. }
  1580. }
  1581. if ( !empty($qv['post_type']) ) {
  1582. if ( is_array($qv['post_type']) )
  1583. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  1584. else
  1585. $qv['post_type'] = sanitize_key($qv['post_type']);
  1586. }
  1587. if ( ! empty( $qv['post_status'] ) ) {
  1588. if ( is_array( $qv['post_status'] ) )
  1589. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  1590. else
  1591. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  1592. }
  1593. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  1594. $this->is_comment_feed = false;
  1595. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  1596. // Done correcting is_* for page_on_front and page_for_posts
  1597. if ( '404' == $qv['error'] )
  1598. $this->set_404();
  1599. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  1600. $this->query_vars_changed = false;
  1601. /**
  1602. * Fires after the main query vars have been parsed.
  1603. *
  1604. * @since 1.5.0
  1605. *
  1606. * @param WP_Query &$this The WP_Query instance (passed by reference).
  1607. */
  1608. do_action_ref_array( 'parse_query', array( &$this ) );
  1609. }
  1610. /**
  1611. * Parses various taxonomy related query vars.
  1612. *
  1613. * For BC, this method is not marked as protected. See [28987].
  1614. *
  1615. * @access protected
  1616. * @since 3.1.0
  1617. *
  1618. * @param array &$q The query variables
  1619. */
  1620. function parse_tax_query( &$q ) {
  1621. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  1622. $tax_query = $q['tax_query'];
  1623. } else {
  1624. $tax_query = array();
  1625. }
  1626. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  1627. $tax_query[] = array(
  1628. 'taxonomy' => $q['taxonomy'],
  1629. 'terms' => array( $q['term'] ),
  1630. 'field' => 'slug',
  1631. );
  1632. }
  1633. foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
  1634. if ( 'post_tag' == $taxonomy )
  1635. continue; // Handled further down in the $q['tag'] block
  1636. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  1637. $tax_query_defaults = array(
  1638. 'taxonomy' => $taxonomy,
  1639. 'field' => 'slug',
  1640. );
  1641. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  1642. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  1643. }
  1644. $term = $q[$t->query_var];
  1645. if ( strpos($term, '+') !== false ) {
  1646. $terms = preg_split( '/[+]+/', $term );
  1647. foreach ( $terms as $term ) {
  1648. $tax_query[] = array_merge( $tax_query_defaults, array(
  1649. 'terms' => array( $term )
  1650. ) );
  1651. }
  1652. } else {
  1653. $tax_query[] = array_merge( $tax_query_defaults, array(
  1654. 'terms' => preg_split( '/[,]+/', $term )
  1655. ) );
  1656. }
  1657. }
  1658. }
  1659. // Category stuff
  1660. if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
  1661. $cat_in = $cat_not_in = array();
  1662. $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
  1663. $cat_array = array_map( 'intval', $cat_array );
  1664. $q['cat'] = implode( ',', $cat_array );
  1665. foreach ( $cat_array as $cat ) {
  1666. if ( $cat > 0 )
  1667. $cat_in[] = $cat;
  1668. elseif ( $cat < 0 )
  1669. $cat_not_in[] = abs( $cat );
  1670. }
  1671. if ( ! empty( $cat_in ) ) {
  1672. $tax_query[] = array(
  1673. 'taxonomy' => 'category',
  1674. 'terms' => $cat_in,
  1675. 'field' => 'term_id',
  1676. 'include_children' => true
  1677. );
  1678. }
  1679. if ( ! empty( $cat_not_in ) ) {
  1680. $tax_query[] = array(
  1681. 'taxonomy' => 'category',
  1682. 'terms' => $cat_not_in,
  1683. 'field' => 'term_id',
  1684. 'operator' => 'NOT IN',
  1685. 'include_children' => true
  1686. );
  1687. }
  1688. unset( $cat_array, $cat_in, $cat_not_in );
  1689. }
  1690. if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
  1691. $q['category__and'] = (array) $q['category__and'];
  1692. if ( ! isset( $q['category__in'] ) )
  1693. $q['category__in'] = array();
  1694. $q['category__in'][] = absint( reset( $q['category__and'] ) );
  1695. unset( $q['category__and'] );
  1696. }
  1697. if ( ! empty( $q['category__in'] ) ) {
  1698. $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
  1699. $tax_query[] = array(
  1700. 'taxonomy' => 'category',
  1701. 'terms' => $q['category__in'],
  1702. 'field' => 'term_id',
  1703. 'include_children' => false
  1704. );
  1705. }
  1706. if ( ! empty($q['category__not_in']) ) {
  1707. $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
  1708. $tax_query[] = array(
  1709. 'taxonomy' => 'category',
  1710. 'terms' => $q['category__not_in'],
  1711. 'operator' => 'NOT IN',
  1712. 'include_children' => false
  1713. );
  1714. }
  1715. if ( ! empty($q['category__and']) ) {
  1716. $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
  1717. $tax_query[] = array(
  1718. 'taxonomy' => 'category',
  1719. 'terms' => $q['category__and'],
  1720. 'field' => 'term_id',
  1721. 'operator' => 'AND',
  1722. 'include_children' => false
  1723. );
  1724. }
  1725. // Tag stuff
  1726. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1727. if ( strpos($q['tag'], ',') !== false ) {
  1728. $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
  1729. foreach ( (array) $tags as $tag ) {
  1730. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1731. $q['tag_slug__in'][] = $tag;
  1732. }
  1733. } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
  1734. $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
  1735. foreach ( (array) $tags as $tag ) {
  1736. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1737. $q['tag_slug__and'][] = $tag;
  1738. }
  1739. } else {
  1740. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1741. $q['tag_slug__in'][] = $q['tag'];
  1742. }
  1743. }
  1744. if ( !empty($q['tag_id']) ) {
  1745. $q['tag_id'] = absint( $q['tag_id'] );
  1746. $tax_query[] = array(
  1747. 'taxonomy' => 'post_tag',
  1748. 'terms' => $q['tag_id']
  1749. );
  1750. }
  1751. if ( !empty($q['tag__in']) ) {
  1752. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1753. $tax_query[] = array(
  1754. 'taxonomy' => 'post_tag',
  1755. 'terms' => $q['tag__in']
  1756. );
  1757. }
  1758. if ( !empty($q['tag__not_in']) ) {
  1759. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1760. $tax_query[] = array(
  1761. 'taxonomy' => 'post_tag',
  1762. 'terms' => $q['tag__not_in'],
  1763. 'operator' => 'NOT IN'
  1764. );
  1765. }
  1766. if ( !empty($q['tag__and']) ) {
  1767. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1768. $tax_query[] = array(
  1769. 'taxonomy' => 'post_tag',
  1770. 'terms' => $q['tag__and'],
  1771. 'operator' => 'AND'
  1772. );
  1773. }
  1774. if ( !empty($q['tag_slug__in']) ) {
  1775. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
  1776. $tax_query[] = array(
  1777. 'taxonomy' => 'post_tag',
  1778. 'terms' => $q['tag_slug__in'],
  1779. 'field' => 'slug'
  1780. );
  1781. }
  1782. if ( !empty($q['tag_slug__and']) ) {
  1783. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
  1784. $tax_query[] = array(
  1785. 'taxonomy' => 'post_tag',
  1786. 'terms' => $q['tag_slug__and'],
  1787. 'field' => 'slug',
  1788. 'operator' => 'AND'
  1789. );
  1790. }
  1791. $this->tax_query = new WP_Tax_Query( $tax_query );
  1792. /**
  1793. * Fires after taxonomy-related query vars have been parsed.
  1794. *
  1795. * @since 3.7.0
  1796. *
  1797. * @param WP_Query $this The WP_Query instance.
  1798. */
  1799. do_action( 'parse_tax_query', $this );
  1800. }
  1801. /**
  1802. * Generate SQL for the WHERE clause based on passed search terms.
  1803. *
  1804. * @since 3.7.0
  1805. *
  1806. * @global wpdb $wpdb
  1807. * @param array $q Query variables.
  1808. * @return string WHERE clause.
  1809. */
  1810. protected function parse_search( &$q ) {
  1811. global $wpdb;
  1812. $search = '';
  1813. // added slashes screw with quote grouping when done early, so done later
  1814. $q['s'] = stripslashes( $q['s'] );
  1815. if ( empty( $_GET['s'] ) && $this->is_main_query() )
  1816. $q['s'] = urldecode( $q['s'] );
  1817. // there are no line breaks in <input /> fields
  1818. $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
  1819. $q['search_terms_count'] = 1;
  1820. if ( ! empty( $q['sentence'] ) ) {
  1821. $q['search_terms'] = array( $q['s'] );
  1822. } else {
  1823. if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
  1824. $q['search_terms_count'] = count( $matches[0] );
  1825. $q['search_terms'] = $this->parse_search_terms( $matches[0] );
  1826. // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
  1827. if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
  1828. $q['search_terms'] = array( $q['s'] );
  1829. } else {
  1830. $q['search_terms'] = array( $q['s'] );
  1831. }
  1832. }
  1833. $n = ! empty( $q['exact'] ) ? '' : '%';
  1834. $searchand = '';
  1835. $q['search_orderby_title'] = array();
  1836. foreach ( $q['search_terms'] as $term ) {
  1837. if ( $n ) {
  1838. $like = '%' . $wpdb->esc_like( $term ) . '%';
  1839. $q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );
  1840. }
  1841. $like = $n . $wpdb->esc_like( $term ) . $n;
  1842. $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title LIKE %s) OR ($wpdb->posts.post_content LIKE %s))", $like, $like );
  1843. $searchand = ' AND ';
  1844. }
  1845. if ( ! empty( $search ) ) {
  1846. $search = " AND ({$search}) ";
  1847. if ( ! is_user_logged_in() )
  1848. $search .= " AND ($wpdb->posts.post_password = '') ";
  1849. }
  1850. return $search;
  1851. }
  1852. /**
  1853. * Check if the terms are suitable for searching.
  1854. *
  1855. * Uses an array of stopwords (terms) that are excluded from the separate
  1856. * term matching when searching for posts. The list of English stopwords is
  1857. * the approximate search engines list, and is translatable.
  1858. *
  1859. * @since 3.7.0
  1860. *
  1861. * @param array $terms Terms to check.
  1862. * @return array Terms that are not stopwords.
  1863. */
  1864. protected function parse_search_terms( $terms ) {
  1865. $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
  1866. $checked = array();
  1867. $stopwords = $this->get_search_stopwords();
  1868. foreach ( $terms as $term ) {
  1869. // keep before/after spaces when term is for exact match
  1870. if ( preg_match( '/^".+"$/', $term ) )
  1871. $term = trim( $term, "\"'" );
  1872. else
  1873. $term = trim( $term, "\"' " );
  1874. // Avoid single A-Z.
  1875. if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
  1876. continue;
  1877. if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
  1878. continue;
  1879. $checked[] = $term;
  1880. }
  1881. return $checked;
  1882. }
  1883. /**
  1884. * Retrieve stopwords used when parsing search terms.
  1885. *
  1886. * @since 3.7.0
  1887. *
  1888. * @return array Stopwords.
  1889. */
  1890. protected function get_search_stopwords() {
  1891. if ( isset( $this->stopwords ) )
  1892. return $this->stopwords;
  1893. /* translators: This is a comma-separated list of very common words that should be excluded from a search,
  1894. * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
  1895. * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
  1896. */
  1897. $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
  1898. 'Comma-separated list of search stopwords in your language' ) );
  1899. $stopwords = array();
  1900. foreach( $words as $word ) {
  1901. $word = trim( $word, "\r\n\t " );
  1902. if ( $word )
  1903. $stopwords[] = $word;
  1904. }
  1905. /**
  1906. * Filter stopwords used when parsing search terms.
  1907. *
  1908. * @since 3.7.0
  1909. *
  1910. * @param array $stopwords Stopwords.
  1911. */
  1912. $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
  1913. return $this->stopwords;
  1914. }
  1915. /**
  1916. * Generate SQL for the ORDER BY condition based on passed search terms.
  1917. *
  1918. * @global wpdb $wpdb
  1919. * @param array $q Query variables.
  1920. * @return string ORDER BY clause.
  1921. */
  1922. protected function parse_search_order( &$q ) {
  1923. global $wpdb;
  1924. if ( $q['search_terms_count'] > 1 ) {
  1925. $num_terms = count( $q['search_orderby_title'] );
  1926. $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
  1927. $search_orderby = '(CASE ';
  1928. // sentence match in 'post_title'
  1929. $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
  1930. // sanity limit, sort as sentence when more than 6 terms
  1931. // (few searches are longer than 6 terms and most titles are not)
  1932. if ( $num_terms < 7 ) {
  1933. // all words in title
  1934. $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
  1935. // any word in title, not needed when $num_terms == 1
  1936. if ( $num_terms > 1 )
  1937. $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
  1938. }
  1939. // sentence match in 'post_content'
  1940. $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
  1941. $search_orderby .= 'ELSE 5 END)';
  1942. } else {
  1943. // single word or sentence search
  1944. $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
  1945. }
  1946. return $search_orderby;
  1947. }
  1948. /**
  1949. * If the passed orderby value is allowed, convert the alias to a
  1950. * properly-prefixed orderby value.
  1951. *
  1952. * @since 4.0.0
  1953. * @access protected
  1954. *
  1955. * @global wpdb $wpdb WordPress database abstraction object.
  1956. *
  1957. * @param string $orderby Alias for the field to order by.
  1958. * @return string|bool Table-prefixed value to used in the ORDER clause. False otherwise.
  1959. */
  1960. protected function parse_orderby( $orderby ) {
  1961. global $wpdb;
  1962. // Used to filter values.
  1963. $allowed_keys = array(
  1964. 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
  1965. 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified',
  1966. 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',
  1967. );
  1968. $primary_meta_key = '';
  1969. $primary_meta_query = false;
  1970. if ( ! empty( $this->meta_query->queries ) ) {
  1971. $primary_meta_query = reset( $this->meta_query->queries );
  1972. if ( ! empty( $primary_meta_query['key'] ) ) {
  1973. $primary_meta_key = $primary_meta_query['key'];
  1974. $allowed_keys[] = $primary_meta_key;
  1975. }
  1976. $allowed_keys[] = 'meta_value';
  1977. $allowed_keys[] = 'meta_value_num';
  1978. }
  1979. if ( ! in_array( $orderby, $allowed_keys ) ) {
  1980. return false;
  1981. }
  1982. switch ( $orderby ) {
  1983. case 'post_name':
  1984. case 'post_author':
  1985. case 'post_date':
  1986. case 'post_title':
  1987. case 'post_modified':
  1988. case 'post_parent':
  1989. case 'post_type':
  1990. case 'ID':
  1991. case 'menu_order':
  1992. case 'comment_count':
  1993. $orderby = "$wpdb->posts.{$orderby}";
  1994. break;
  1995. case 'rand':
  1996. $orderby = 'RAND()';
  1997. break;
  1998. case $primary_meta_key:
  1999. case 'meta_value':
  2000. if ( ! empty( $primary_meta_query['type'] ) ) {
  2001. $sql_type = $this->meta_query->get_cast_for_type( $primary_meta_query['type'] );
  2002. $orderby = "CAST($wpdb->postmeta.meta_value AS {$sql_type})";
  2003. } else {
  2004. $orderby = "$wpdb->postmeta.meta_value";
  2005. }
  2006. break;
  2007. case 'meta_value_num':
  2008. $orderby = "$wpdb->postmeta.meta_value+0";
  2009. break;
  2010. default:
  2011. $orderby = "$wpdb->posts.post_" . $orderby;
  2012. break;
  2013. }
  2014. return $orderby;
  2015. }
  2016. /**
  2017. * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
  2018. *
  2019. * @since 4.0.0
  2020. * @access protected
  2021. *
  2022. * @param string $order The 'order' query variable.
  2023. * @return string The sanitized 'order' query variable.
  2024. */
  2025. protected function parse_order( $order ) {
  2026. if ( ! is_string( $order ) || empty( $order ) ) {
  2027. return 'DESC';
  2028. }
  2029. if ( 'ASC' === strtoupper( $order ) ) {
  2030. return 'ASC';
  2031. } else {
  2032. return 'DESC';
  2033. }
  2034. }
  2035. /**
  2036. * Sets the 404 property and saves whether query is feed.
  2037. *
  2038. * @since 2.0.0
  2039. * @access public
  2040. */
  2041. public function set_404() {
  2042. $is_feed = $this->is_feed;
  2043. $this->init_query_flags();
  2044. $this->is_404 = true;
  2045. $this->is_feed = $is_feed;
  2046. }
  2047. /**
  2048. * Retrieve query variable.
  2049. *
  2050. * @since 1.5.0
  2051. * @access public
  2052. *
  2053. * @param string $query_var Query variable key.
  2054. * @param mixed $default Value to return if the query variable is not set. Default ''.
  2055. * @return mixed
  2056. */
  2057. public function get( $query_var, $default = '' ) {
  2058. if ( isset( $this->query_vars[ $query_var ] ) ) {
  2059. return $this->query_vars[ $query_var ];
  2060. }
  2061. return $default;
  2062. }
  2063. /**
  2064. * Set query variable.
  2065. *
  2066. * @since 1.5.0
  2067. * @access public
  2068. *
  2069. * @param string $query_var Query variable key.
  2070. * @param mixed $value Query variable value.
  2071. */
  2072. public function set($query_var, $value) {
  2073. $this->query_vars[$query_var] = $value;
  2074. }
  2075. /**
  2076. * Retrieve the posts based on query variables.
  2077. *
  2078. * There are a few filters and actions that can be used to modify the post
  2079. * database query.
  2080. *
  2081. * @since 1.5.0
  2082. * @access public
  2083. *
  2084. * @return array List of posts.
  2085. */
  2086. public function get_posts() {
  2087. global $wpdb;
  2088. $this->parse_query();
  2089. /**
  2090. * Fires after the query variable object is created, but before the actual query is run.
  2091. *
  2092. * Note: If using conditional tags, use the method versions within the passed instance
  2093. * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
  2094. * like is_main_query() test against the global $wp_query instance, not the passed one.
  2095. *
  2096. * @since 2.0.0
  2097. *
  2098. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2099. */
  2100. do_action_ref_array( 'pre_get_posts', array( &$this ) );
  2101. // Shorthand.
  2102. $q = &$this->query_vars;
  2103. // Fill again in case pre_get_posts unset some vars.
  2104. $q = $this->fill_query_vars($q);
  2105. // Parse meta query
  2106. $this->meta_query = new WP_Meta_Query();
  2107. $this->meta_query->parse_query_vars( $q );
  2108. // Set a flag if a pre_get_posts hook changed the query vars.
  2109. $hash = md5( serialize( $this->query_vars ) );
  2110. if ( $hash != $this->query_vars_hash ) {
  2111. $this->query_vars_changed = true;
  2112. $this->query_vars_hash = $hash;
  2113. }
  2114. unset($hash);
  2115. // First let's clear some variables
  2116. $distinct = '';
  2117. $whichauthor = '';
  2118. $whichmimetype = '';
  2119. $where = '';
  2120. $limits = '';
  2121. $join = '';
  2122. $search = '';
  2123. $groupby = '';
  2124. $post_status_join = false;
  2125. $page = 1;
  2126. if ( isset( $q['caller_get_posts'] ) ) {
  2127. _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
  2128. if ( !isset( $q['ignore_sticky_posts'] ) )
  2129. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  2130. }
  2131. if ( !isset( $q['ignore_sticky_posts'] ) )
  2132. $q['ignore_sticky_posts'] = false;
  2133. if ( !isset($q['suppress_filters']) )
  2134. $q['suppress_filters'] = false;
  2135. if ( !isset($q['cache_results']) ) {
  2136. if ( wp_using_ext_object_cache() )
  2137. $q['cache_results'] = false;
  2138. else
  2139. $q['cache_results'] = true;
  2140. }
  2141. if ( !isset($q['update_post_term_cache']) )
  2142. $q['update_post_term_cache'] = true;
  2143. if ( !isset($q['update_post_meta_cache']) )
  2144. $q['update_post_meta_cache'] = true;
  2145. if ( !isset($q['post_type']) ) {
  2146. if ( $this->is_search )
  2147. $q['post_type'] = 'any';
  2148. else
  2149. $q['post_type'] = '';
  2150. }
  2151. $post_type = $q['post_type'];
  2152. if ( empty( $q['posts_per_page'] ) ) {
  2153. $q['posts_per_page'] = get_option( 'posts_per_page' );
  2154. }
  2155. if ( isset($q['showposts']) && $q['showposts'] ) {
  2156. $q['showposts'] = (int) $q['showposts'];
  2157. $q['posts_per_page'] = $q['showposts'];
  2158. }
  2159. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  2160. $q['posts_per_page'] = $q['posts_per_archive_page'];
  2161. if ( !isset($q['nopaging']) ) {
  2162. if ( $q['posts_per_page'] == -1 ) {
  2163. $q['nopaging'] = true;
  2164. } else {
  2165. $q['nopaging'] = false;
  2166. }
  2167. }
  2168. if ( $this->is_feed ) {
  2169. // This overrides posts_per_page.
  2170. if ( ! empty( $q['posts_per_rss'] ) ) {
  2171. $q['posts_per_page'] = $q['posts_per_rss'];
  2172. } else {
  2173. $q['posts_per_page'] = get_option( 'posts_per_rss' );
  2174. }
  2175. $q['nopaging'] = false;
  2176. }
  2177. $q['posts_per_page'] = (int) $q['posts_per_page'];
  2178. if ( $q['posts_per_page'] < -1 )
  2179. $q['posts_per_page'] = abs($q['posts_per_page']);
  2180. else if ( $q['posts_per_page'] == 0 )
  2181. $q['posts_per_page'] = 1;
  2182. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  2183. $q['comments_per_page'] = get_option('comments_per_page');
  2184. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  2185. $this->is_page = true;
  2186. $this->is_home = false;
  2187. $q['page_id'] = get_option('page_on_front');
  2188. }
  2189. if ( isset($q['page']) ) {
  2190. $q['page'] = trim($q['page'], '/');
  2191. $q['page'] = absint($q['page']);
  2192. }
  2193. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  2194. if ( isset($q['no_found_rows']) )
  2195. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  2196. else
  2197. $q['no_found_rows'] = false;
  2198. switch ( $q['fields'] ) {
  2199. case 'ids':
  2200. $fields = "$wpdb->posts.ID";
  2201. break;
  2202. case 'id=>parent':
  2203. $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
  2204. break;
  2205. default:
  2206. $fields = "$wpdb->posts.*";
  2207. }
  2208. if ( '' !== $q['menu_order'] )
  2209. $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
  2210. // The "m" parameter is meant for months but accepts datetimes of varying specificity
  2211. if ( $q['m'] ) {
  2212. $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  2213. if ( strlen($q['m']) > 5 )
  2214. $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  2215. if ( strlen($q['m']) > 7 )
  2216. $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  2217. if ( strlen($q['m']) > 9 )
  2218. $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  2219. if ( strlen($q['m']) > 11 )
  2220. $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  2221. if ( strlen($q['m']) > 13 )
  2222. $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  2223. }
  2224. // Handle the other individual date parameters
  2225. $date_parameters = array();
  2226. if ( '' !== $q['hour'] )
  2227. $date_parameters['hour'] = $q['hour'];
  2228. if ( '' !== $q['minute'] )
  2229. $date_parameters['minute'] = $q['minute'];
  2230. if ( '' !== $q['second'] )
  2231. $date_parameters['second'] = $q['second'];
  2232. if ( $q['year'] )
  2233. $date_parameters['year'] = $q['year'];
  2234. if ( $q['monthnum'] )
  2235. $date_parameters['monthnum'] = $q['monthnum'];
  2236. if ( $q['w'] )
  2237. $date_parameters['week'] = $q['w'];
  2238. if ( $q['day'] )
  2239. $date_parameters['day'] = $q['day'];
  2240. if ( $date_parameters ) {
  2241. $date_query = new WP_Date_Query( array( $date_parameters ) );
  2242. $where .= $date_query->get_sql();
  2243. }
  2244. unset( $date_parameters, $date_query );
  2245. // Handle complex date queries
  2246. if ( ! empty( $q['date_query'] ) ) {
  2247. $this->date_query = new WP_Date_Query( $q['date_query'] );
  2248. $where .= $this->date_query->get_sql();
  2249. }
  2250. // If we've got a post_type AND it's not "any" post_type.
  2251. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  2252. foreach ( (array)$q['post_type'] as $_post_type ) {
  2253. $ptype_obj = get_post_type_object($_post_type);
  2254. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  2255. continue;
  2256. if ( ! $ptype_obj->hierarchical ) {
  2257. // Non-hierarchical post types can directly use 'name'.
  2258. $q['name'] = $q[ $ptype_obj->query_var ];
  2259. } else {
  2260. // Hierarchical post types will operate through 'pagename'.
  2261. $q['pagename'] = $q[ $ptype_obj->query_var ];
  2262. $q['name'] = '';
  2263. }
  2264. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  2265. break;
  2266. } //end foreach
  2267. unset($ptype_obj);
  2268. }
  2269. if ( '' != $q['name'] ) {
  2270. $q['name'] = sanitize_title_for_query( $q['name'] );
  2271. $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  2272. } elseif ( '' != $q['pagename'] ) {
  2273. if ( isset($this->queried_object_id) ) {
  2274. $reqpage = $this->queried_object_id;
  2275. } else {
  2276. if ( 'page' != $q['post_type'] ) {
  2277. foreach ( (array)$q['post_type'] as $_post_type ) {
  2278. $ptype_obj = get_post_type_object($_post_type);
  2279. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  2280. continue;
  2281. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  2282. if ( $reqpage )
  2283. break;
  2284. }
  2285. unset($ptype_obj);
  2286. } else {
  2287. $reqpage = get_page_by_path($q['pagename']);
  2288. }
  2289. if ( !empty($reqpage) )
  2290. $reqpage = $reqpage->ID;
  2291. else
  2292. $reqpage = 0;
  2293. }
  2294. $page_for_posts = get_option('page_for_posts');
  2295. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  2296. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  2297. $q['name'] = $q['pagename'];
  2298. $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  2299. $reqpage_obj = get_post( $reqpage );
  2300. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  2301. $this->is_attachment = true;
  2302. $post_type = $q['post_type'] = 'attachment';
  2303. $this->is_page = true;
  2304. $q['attachment_id'] = $reqpage;
  2305. }
  2306. }
  2307. } elseif ( '' != $q['attachment'] ) {
  2308. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  2309. $q['name'] = $q['attachment'];
  2310. $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  2311. }
  2312. if ( intval($q['comments_popup']) )
  2313. $q['p'] = absint($q['comments_popup']);
  2314. // If an attachment is requested by number, let it supersede any post number.
  2315. if ( $q['attachment_id'] )
  2316. $q['p'] = absint($q['attachment_id']);
  2317. // If a post number is specified, load that post
  2318. if ( $q['p'] ) {
  2319. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  2320. } elseif ( $q['post__in'] ) {
  2321. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  2322. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  2323. } elseif ( $q['post__not_in'] ) {
  2324. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  2325. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  2326. }
  2327. if ( is_numeric( $q['post_parent'] ) ) {
  2328. $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  2329. } elseif ( $q['post_parent__in'] ) {
  2330. $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
  2331. $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
  2332. } elseif ( $q['post_parent__not_in'] ) {
  2333. $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
  2334. $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
  2335. }
  2336. if ( $q['page_id'] ) {
  2337. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  2338. $q['p'] = $q['page_id'];
  2339. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  2340. }
  2341. }
  2342. // If a search pattern is specified, load the posts that match.
  2343. if ( ! empty( $q['s'] ) ) {
  2344. $search = $this->parse_search( $q );
  2345. }
  2346. /**
  2347. * Filter the search SQL that is used in the WHERE clause of WP_Query.
  2348. *
  2349. * @since 3.0.0
  2350. *
  2351. * @param string $search Search SQL for WHERE clause.
  2352. * @param WP_Query $this The current WP_Query object.
  2353. */
  2354. $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
  2355. // Taxonomies
  2356. if ( !$this->is_singular ) {
  2357. $this->parse_tax_query( $q );
  2358. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
  2359. $join .= $clauses['join'];
  2360. $where .= $clauses['where'];
  2361. }
  2362. if ( $this->is_tax ) {
  2363. if ( empty($post_type) ) {
  2364. // Do a fully inclusive search for currently registered post types of queried taxonomies
  2365. $post_type = array();
  2366. $taxonomies = array_keys( $this->tax_query->queried_terms );
  2367. foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
  2368. $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
  2369. if ( array_intersect( $taxonomies, $object_taxonomies ) )
  2370. $post_type[] = $pt;
  2371. }
  2372. if ( ! $post_type )
  2373. $post_type = 'any';
  2374. elseif ( count( $post_type ) == 1 )
  2375. $post_type = $post_type[0];
  2376. $post_status_join = true;
  2377. } elseif ( in_array('attachment', (array) $post_type) ) {
  2378. $post_status_join = true;
  2379. }
  2380. }
  2381. /*
  2382. * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
  2383. * 'category_name' vars are set for backward compatibility.
  2384. */
  2385. if ( ! empty( $this->tax_query->queried_terms ) ) {
  2386. /*
  2387. * Set 'taxonomy', 'term', and 'term_id' to the
  2388. * first taxonomy other than 'post_tag' or 'category'.
  2389. */
  2390. if ( ! isset( $q['taxonomy'] ) ) {
  2391. foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
  2392. if ( empty( $queried_items['terms'][0] ) ) {
  2393. continue;
  2394. }
  2395. if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
  2396. $q['taxonomy'] = $queried_taxonomy;
  2397. if ( 'slug' === $queried_items['field'] ) {
  2398. $q['term'] = $queried_items['terms'][0];
  2399. } else {
  2400. $q['term_id'] = $queried_items['terms'][0];
  2401. }
  2402. }
  2403. }
  2404. }
  2405. // 'cat', 'category_name', 'tag_id'
  2406. foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
  2407. if ( empty( $queried_items['terms'][0] ) ) {
  2408. continue;
  2409. }
  2410. if ( 'category' === $queried_taxonomy ) {
  2411. $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' );
  2412. if ( $the_cat ) {
  2413. $this->set( 'cat', $the_cat->term_id );
  2414. $this->set( 'category_name', $the_cat->slug );
  2415. }
  2416. unset( $the_cat );
  2417. }
  2418. if ( 'post_tag' === $queried_taxonomy ) {
  2419. $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' );
  2420. if ( $the_tag ) {
  2421. $this->set( 'tag_id', $the_tag->term_id );
  2422. }
  2423. unset( $the_tag );
  2424. }
  2425. }
  2426. }
  2427. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
  2428. $groupby = "{$wpdb->posts}.ID";
  2429. }
  2430. // Author/user stuff
  2431. if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
  2432. $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
  2433. $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
  2434. foreach ( $authors as $author ) {
  2435. $key = $author > 0 ? 'author__in' : 'author__not_in';
  2436. $q[$key][] = abs( $author );
  2437. }
  2438. $q['author'] = implode( ',', $authors );
  2439. }
  2440. if ( ! empty( $q['author__not_in'] ) ) {
  2441. $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
  2442. $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
  2443. } elseif ( ! empty( $q['author__in'] ) ) {
  2444. $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
  2445. $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
  2446. }
  2447. // Author stuff for nice URLs
  2448. if ( '' != $q['author_name'] ) {
  2449. if ( strpos($q['author_name'], '/') !== false ) {
  2450. $q['author_name'] = explode('/', $q['author_name']);
  2451. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  2452. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  2453. } else {
  2454. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
  2455. }
  2456. }
  2457. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  2458. $q['author'] = get_user_by('slug', $q['author_name']);
  2459. if ( $q['author'] )
  2460. $q['author'] = $q['author']->ID;
  2461. $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  2462. }
  2463. // MIME-Type stuff for attachment browsing
  2464. if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
  2465. $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
  2466. $where .= $search . $whichauthor . $whichmimetype;
  2467. $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
  2468. if ( ! isset( $q['order'] ) ) {
  2469. $q['order'] = $rand ? '' : 'DESC';
  2470. } else {
  2471. $q['order'] = $rand ? '' : $this->parse_order( $q['order'] );
  2472. }
  2473. // Order by.
  2474. if ( empty( $q['orderby'] ) ) {
  2475. /*
  2476. * Boolean false or empty array blanks out ORDER BY,
  2477. * while leaving the value unset or otherwise empty sets the default.
  2478. */
  2479. if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) {
  2480. $orderby = '';
  2481. } else {
  2482. $orderby = "$wpdb->posts.post_date " . $q['order'];
  2483. }
  2484. } elseif ( 'none' == $q['orderby'] ) {
  2485. $orderby = '';
  2486. } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
  2487. $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
  2488. } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
  2489. $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
  2490. } else {
  2491. $orderby_array = array();
  2492. if ( is_array( $q['orderby'] ) ) {
  2493. foreach ( $q['orderby'] as $_orderby => $order ) {
  2494. $orderby = addslashes_gpc( urldecode( $_orderby ) );
  2495. $parsed = $this->parse_orderby( $orderby );
  2496. if ( ! $parsed ) {
  2497. continue;
  2498. }
  2499. $orderby_array[] = $parsed . ' ' . $this->parse_order( $order );
  2500. }
  2501. $orderby = implode( ', ', $orderby_array );
  2502. } else {
  2503. $q['orderby'] = urldecode( $q['orderby'] );
  2504. $q['orderby'] = addslashes_gpc( $q['orderby'] );
  2505. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  2506. $parsed = $this->parse_orderby( $orderby );
  2507. // Only allow certain values for safety.
  2508. if ( ! $parsed ) {
  2509. continue;
  2510. }
  2511. $orderby_array[] = $parsed;
  2512. }
  2513. $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
  2514. if ( empty( $orderby ) ) {
  2515. $orderby = "$wpdb->posts.post_date " . $q['order'];
  2516. } elseif ( ! empty( $q['order'] ) ) {
  2517. $orderby .= " {$q['order']}";
  2518. }
  2519. }
  2520. }
  2521. // Order search results by relevance only when another "orderby" is not specified in the query.
  2522. if ( ! empty( $q['s'] ) ) {
  2523. $search_orderby = '';
  2524. if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
  2525. $search_orderby = $this->parse_search_order( $q );
  2526. /**
  2527. * Filter the ORDER BY used when ordering search results.
  2528. *
  2529. * @since 3.7.0
  2530. *
  2531. * @param string $search_orderby The ORDER BY clause.
  2532. * @param WP_Query $this The current WP_Query instance.
  2533. */
  2534. $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
  2535. if ( $search_orderby )
  2536. $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
  2537. }
  2538. if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
  2539. $post_type_cap = 'multiple_post_type';
  2540. } else {
  2541. if ( is_array( $post_type ) )
  2542. $post_type = reset( $post_type );
  2543. $post_type_object = get_post_type_object( $post_type );
  2544. if ( empty( $post_type_object ) )
  2545. $post_type_cap = $post_type;
  2546. }
  2547. if ( isset( $q['post_password'] ) ) {
  2548. $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
  2549. if ( empty( $q['perm'] ) ) {
  2550. $q['perm'] = 'readable';
  2551. }
  2552. } elseif ( isset( $q['has_password'] ) ) {
  2553. $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
  2554. }
  2555. if ( 'any' == $post_type ) {
  2556. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  2557. if ( empty( $in_search_post_types ) )
  2558. $where .= ' AND 1=0 ';
  2559. else
  2560. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
  2561. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  2562. $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  2563. } elseif ( ! empty( $post_type ) ) {
  2564. $where .= " AND $wpdb->posts.post_type = '$post_type'";
  2565. $post_type_object = get_post_type_object ( $post_type );
  2566. } elseif ( $this->is_attachment ) {
  2567. $where .= " AND $wpdb->posts.post_type = 'attachment'";
  2568. $post_type_object = get_post_type_object ( 'attachment' );
  2569. } elseif ( $this->is_page ) {
  2570. $where .= " AND $wpdb->posts.post_type = 'page'";
  2571. $post_type_object = get_post_type_object ( 'page' );
  2572. } else {
  2573. $where .= " AND $wpdb->posts.post_type = 'post'";
  2574. $post_type_object = get_post_type_object ( 'post' );
  2575. }
  2576. $edit_cap = 'edit_post';
  2577. $read_cap = 'read_post';
  2578. if ( ! empty( $post_type_object ) ) {
  2579. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  2580. $read_private_cap = $post_type_object->cap->read_private_posts;
  2581. } else {
  2582. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  2583. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  2584. }
  2585. $user_id = get_current_user_id();
  2586. if ( ! empty( $q['post_status'] ) ) {
  2587. $statuswheres = array();
  2588. $q_status = $q['post_status'];
  2589. if ( ! is_array( $q_status ) )
  2590. $q_status = explode(',', $q_status);
  2591. $r_status = array();
  2592. $p_status = array();
  2593. $e_status = array();
  2594. if ( in_array( 'any', $q_status ) ) {
  2595. foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) {
  2596. if ( ! in_array( $status, $q_status ) ) {
  2597. $e_status[] = "$wpdb->posts.post_status <> '$status'";
  2598. }
  2599. }
  2600. } else {
  2601. foreach ( get_post_stati() as $status ) {
  2602. if ( in_array( $status, $q_status ) ) {
  2603. if ( 'private' == $status )
  2604. $p_status[] = "$wpdb->posts.post_status = '$status'";
  2605. else
  2606. $r_status[] = "$wpdb->posts.post_status = '$status'";
  2607. }
  2608. }
  2609. }
  2610. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2611. $r_status = array_merge($r_status, $p_status);
  2612. unset($p_status);
  2613. }
  2614. if ( !empty($e_status) ) {
  2615. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2616. }
  2617. if ( !empty($r_status) ) {
  2618. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  2619. $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
  2620. else
  2621. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2622. }
  2623. if ( !empty($p_status) ) {
  2624. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  2625. $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
  2626. else
  2627. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2628. }
  2629. if ( $post_status_join ) {
  2630. $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  2631. foreach ( $statuswheres as $index => $statuswhere )
  2632. $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  2633. }
  2634. $where_status = implode( ' OR ', $statuswheres );
  2635. if ( ! empty( $where_status ) ) {
  2636. $where .= " AND ($where_status)";
  2637. }
  2638. } elseif ( !$this->is_singular ) {
  2639. $where .= " AND ($wpdb->posts.post_status = 'publish'";
  2640. // Add public states.
  2641. $public_states = get_post_stati( array('public' => true) );
  2642. foreach ( (array) $public_states as $state ) {
  2643. if ( 'publish' == $state ) // Publish is hard-coded above.
  2644. continue;
  2645. $where .= " OR $wpdb->posts.post_status = '$state'";
  2646. }
  2647. if ( $this->is_admin ) {
  2648. // Add protected states that should show in the admin all list.
  2649. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2650. foreach ( (array) $admin_all_states as $state )
  2651. $where .= " OR $wpdb->posts.post_status = '$state'";
  2652. }
  2653. if ( is_user_logged_in() ) {
  2654. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2655. $private_states = get_post_stati( array('private' => true) );
  2656. foreach ( (array) $private_states as $state )
  2657. $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'";
  2658. }
  2659. $where .= ')';
  2660. }
  2661. if ( !empty( $this->meta_query->queries ) ) {
  2662. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  2663. $join .= $clauses['join'];
  2664. $where .= $clauses['where'];
  2665. }
  2666. /*
  2667. * Apply filters on where and join prior to paging so that any
  2668. * manipulations to them are reflected in the paging by day queries.
  2669. */
  2670. if ( !$q['suppress_filters'] ) {
  2671. /**
  2672. * Filter the WHERE clause of the query.
  2673. *
  2674. * @since 1.5.0
  2675. *
  2676. * @param string $where The WHERE clause of the query.
  2677. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2678. */
  2679. $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) );
  2680. /**
  2681. * Filter the JOIN clause of the query.
  2682. *
  2683. * @since 1.5.0
  2684. *
  2685. * @param string $where The JOIN clause of the query.
  2686. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2687. */
  2688. $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) );
  2689. }
  2690. // Paging
  2691. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2692. $page = absint($q['paged']);
  2693. if ( !$page )
  2694. $page = 1;
  2695. if ( empty($q['offset']) ) {
  2696. $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', ';
  2697. } else { // we're ignoring $page and using 'offset'
  2698. $q['offset'] = absint($q['offset']);
  2699. $pgstrt = $q['offset'] . ', ';
  2700. }
  2701. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2702. }
  2703. // Comments feeds
  2704. if ( $this->is_comment_feed && ! $this->is_singular ) {
  2705. if ( $this->is_archive || $this->is_search ) {
  2706. $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  2707. $cwhere = "WHERE comment_approved = '1' $where";
  2708. $cgroupby = "$wpdb->comments.comment_id";
  2709. } else { // Other non singular e.g. front
  2710. $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  2711. $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  2712. $cgroupby = '';
  2713. }
  2714. if ( !$q['suppress_filters'] ) {
  2715. /**
  2716. * Filter the JOIN clause of the comments feed query before sending.
  2717. *
  2718. * @since 2.2.0
  2719. *
  2720. * @param string $cjoin The JOIN clause of the query.
  2721. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2722. */
  2723. $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) );
  2724. /**
  2725. * Filter the WHERE clause of the comments feed query before sending.
  2726. *
  2727. * @since 2.2.0
  2728. *
  2729. * @param string $cwhere The WHERE clause of the query.
  2730. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2731. */
  2732. $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) );
  2733. /**
  2734. * Filter the GROUP BY clause of the comments feed query before sending.
  2735. *
  2736. * @since 2.2.0
  2737. *
  2738. * @param string $cgroupby The GROUP BY clause of the query.
  2739. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2740. */
  2741. $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) );
  2742. /**
  2743. * Filter the ORDER BY clause of the comments feed query before sending.
  2744. *
  2745. * @since 2.8.0
  2746. *
  2747. * @param string $corderby The ORDER BY clause of the query.
  2748. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2749. */
  2750. $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2751. /**
  2752. * Filter the LIMIT clause of the comments feed query before sending.
  2753. *
  2754. * @since 2.8.0
  2755. *
  2756. * @param string $climits The JOIN clause of the query.
  2757. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2758. */
  2759. $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2760. }
  2761. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2762. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2763. $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  2764. $this->comment_count = count($this->comments);
  2765. $post_ids = array();
  2766. foreach ( $this->comments as $comment )
  2767. $post_ids[] = (int) $comment->comment_post_ID;
  2768. $post_ids = join(',', $post_ids);
  2769. $join = '';
  2770. if ( $post_ids )
  2771. $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  2772. else
  2773. $where = "AND 0";
  2774. }
  2775. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2776. /*
  2777. * Apply post-paging filters on where and join. Only plugins that
  2778. * manipulate paging queries should use these hooks.
  2779. */
  2780. if ( !$q['suppress_filters'] ) {
  2781. /**
  2782. * Filter the WHERE clause of the query.
  2783. *
  2784. * Specifically for manipulating paging queries.
  2785. *
  2786. * @since 1.5.0
  2787. *
  2788. * @param string $where The WHERE clause of the query.
  2789. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2790. */
  2791. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2792. /**
  2793. * Filter the GROUP BY clause of the query.
  2794. *
  2795. * @since 2.0.0
  2796. *
  2797. * @param string $groupby The GROUP BY clause of the query.
  2798. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2799. */
  2800. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2801. /**
  2802. * Filter the JOIN clause of the query.
  2803. *
  2804. * Specifically for manipulating paging queries.
  2805. *
  2806. * @since 1.5.0
  2807. *
  2808. * @param string $join The JOIN clause of the query.
  2809. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2810. */
  2811. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2812. /**
  2813. * Filter the ORDER BY clause of the query.
  2814. *
  2815. * @since 1.5.1
  2816. *
  2817. * @param string $orderby The ORDER BY clause of the query.
  2818. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2819. */
  2820. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2821. /**
  2822. * Filter the DISTINCT clause of the query.
  2823. *
  2824. * @since 2.1.0
  2825. *
  2826. * @param string $distinct The DISTINCT clause of the query.
  2827. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2828. */
  2829. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2830. /**
  2831. * Filter the LIMIT clause of the query.
  2832. *
  2833. * @since 2.1.0
  2834. *
  2835. * @param string $limits The LIMIT clause of the query.
  2836. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2837. */
  2838. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2839. /**
  2840. * Filter the SELECT clause of the query.
  2841. *
  2842. * @since 2.1.0
  2843. *
  2844. * @param string $fields The SELECT clause of the query.
  2845. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2846. */
  2847. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2848. /**
  2849. * Filter all query clauses at once, for convenience.
  2850. *
  2851. * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
  2852. * fields (SELECT), and LIMITS clauses.
  2853. *
  2854. * @since 3.1.0
  2855. *
  2856. * @param array $clauses The list of clauses for the query.
  2857. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2858. */
  2859. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2860. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
  2861. $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
  2862. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
  2863. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
  2864. $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
  2865. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
  2866. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
  2867. }
  2868. /**
  2869. * Fires to announce the query's current selection parameters.
  2870. *
  2871. * For use by caching plugins.
  2872. *
  2873. * @since 2.3.0
  2874. *
  2875. * @param string $selection The assembled selection query.
  2876. */
  2877. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2878. /*
  2879. * Filter again for the benefit of caching plugins.
  2880. * Regular plugins should use the hooks above.
  2881. */
  2882. if ( !$q['suppress_filters'] ) {
  2883. /**
  2884. * Filter the WHERE clause of the query.
  2885. *
  2886. * For use by caching plugins.
  2887. *
  2888. * @since 2.5.0
  2889. *
  2890. * @param string $where The WHERE clause of the query.
  2891. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2892. */
  2893. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2894. /**
  2895. * Filter the GROUP BY clause of the query.
  2896. *
  2897. * For use by caching plugins.
  2898. *
  2899. * @since 2.5.0
  2900. *
  2901. * @param string $groupby The GROUP BY clause of the query.
  2902. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2903. */
  2904. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2905. /**
  2906. * Filter the JOIN clause of the query.
  2907. *
  2908. * For use by caching plugins.
  2909. *
  2910. * @since 2.5.0
  2911. *
  2912. * @param string $join The JOIN clause of the query.
  2913. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2914. */
  2915. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2916. /**
  2917. * Filter the ORDER BY clause of the query.
  2918. *
  2919. * For use by caching plugins.
  2920. *
  2921. * @since 2.5.0
  2922. *
  2923. * @param string $orderby The ORDER BY clause of the query.
  2924. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2925. */
  2926. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2927. /**
  2928. * Filter the DISTINCT clause of the query.
  2929. *
  2930. * For use by caching plugins.
  2931. *
  2932. * @since 2.5.0
  2933. *
  2934. * @param string $distinct The DISTINCT clause of the query.
  2935. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2936. */
  2937. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2938. /**
  2939. * Filter the SELECT clause of the query.
  2940. *
  2941. * For use by caching plugins.
  2942. *
  2943. * @since 2.5.0
  2944. *
  2945. * @param string $fields The SELECT clause of the query.
  2946. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2947. */
  2948. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2949. /**
  2950. * Filter the LIMIT clause of the query.
  2951. *
  2952. * For use by caching plugins.
  2953. *
  2954. * @since 2.5.0
  2955. *
  2956. * @param string $limits The LIMIT clause of the query.
  2957. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2958. */
  2959. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2960. /**
  2961. * Filter all query clauses at once, for convenience.
  2962. *
  2963. * For use by caching plugins.
  2964. *
  2965. * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
  2966. * fields (SELECT), and LIMITS clauses.
  2967. *
  2968. * @since 3.1.0
  2969. *
  2970. * @param array $pieces The pieces of the query.
  2971. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2972. */
  2973. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
  2974. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
  2975. $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
  2976. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
  2977. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
  2978. $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
  2979. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
  2980. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
  2981. }
  2982. if ( ! empty($groupby) )
  2983. $groupby = 'GROUP BY ' . $groupby;
  2984. if ( !empty( $orderby ) )
  2985. $orderby = 'ORDER BY ' . $orderby;
  2986. $found_rows = '';
  2987. if ( !$q['no_found_rows'] && !empty($limits) )
  2988. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2989. $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  2990. if ( !$q['suppress_filters'] ) {
  2991. /**
  2992. * Filter the completed SQL query before sending.
  2993. *
  2994. * @since 2.0.0
  2995. *
  2996. * @param array $request The complete SQL query.
  2997. * @param WP_Query &$this The WP_Query instance (passed by reference).
  2998. */
  2999. $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
  3000. }
  3001. if ( 'ids' == $q['fields'] ) {
  3002. $this->posts = $wpdb->get_col( $this->request );
  3003. $this->post_count = count( $this->posts );
  3004. $this->set_found_posts( $q, $limits );
  3005. return array_map( 'intval', $this->posts );
  3006. }
  3007. if ( 'id=>parent' == $q['fields'] ) {
  3008. $this->posts = $wpdb->get_results( $this->request );
  3009. $this->post_count = count( $this->posts );
  3010. $this->set_found_posts( $q, $limits );
  3011. $r = array();
  3012. foreach ( $this->posts as $post ) {
  3013. $r[ (int) $post->ID ] = (int) $post->post_parent;
  3014. }
  3015. return $r;
  3016. }
  3017. $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
  3018. /**
  3019. * Filter whether to split the query.
  3020. *
  3021. * Splitting the query will cause it to fetch just the IDs of the found posts
  3022. * (and then individually fetch each post by ID), rather than fetching every
  3023. * complete row at once. One massive result vs. many small results.
  3024. *
  3025. * @since 3.4.0
  3026. *
  3027. * @param bool $split_the_query Whether or not to split the query.
  3028. * @param WP_Query $this The WP_Query instance.
  3029. */
  3030. $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
  3031. if ( $split_the_query ) {
  3032. // First get the IDs and then fill in the objects
  3033. $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  3034. /**
  3035. * Filter the Post IDs SQL request before sending.
  3036. *
  3037. * @since 3.4.0
  3038. *
  3039. * @param string $request The post ID request.
  3040. * @param WP_Query $this The WP_Query instance.
  3041. */
  3042. $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
  3043. $ids = $wpdb->get_col( $this->request );
  3044. if ( $ids ) {
  3045. $this->posts = $ids;
  3046. $this->set_found_posts( $q, $limits );
  3047. _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
  3048. } else {
  3049. $this->posts = array();
  3050. }
  3051. } else {
  3052. $this->posts = $wpdb->get_results( $this->request );
  3053. $this->set_found_posts( $q, $limits );
  3054. }
  3055. // Convert to WP_Post objects
  3056. if ( $this->posts )
  3057. $this->posts = array_map( 'get_post', $this->posts );
  3058. if ( ! $q['suppress_filters'] ) {
  3059. /**
  3060. * Filter the raw post results array, prior to status checks.
  3061. *
  3062. * @since 2.3.0
  3063. *
  3064. * @param array $posts The post results array.
  3065. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3066. */
  3067. $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) );
  3068. }
  3069. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  3070. /** This filter is documented in wp-includes/query.php */
  3071. $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) );
  3072. /** This filter is documented in wp-includes/query.php */
  3073. $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  3074. /** This filter is documented in wp-includes/query.php */
  3075. $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) );
  3076. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  3077. /** This filter is documented in wp-includes/query.php */
  3078. $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  3079. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  3080. /** This filter is documented in wp-includes/query.php */
  3081. $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  3082. $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  3083. $this->comments = $wpdb->get_results($comments_request);
  3084. $this->comment_count = count($this->comments);
  3085. }
  3086. // Check post status to determine if post should be displayed.
  3087. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  3088. $status = get_post_status($this->posts[0]);
  3089. $post_status_obj = get_post_status_object($status);
  3090. //$type = get_post_type($this->posts[0]);
  3091. if ( !$post_status_obj->public ) {
  3092. if ( ! is_user_logged_in() ) {
  3093. // User must be logged in to view unpublished posts.
  3094. $this->posts = array();
  3095. } else {
  3096. if ( $post_status_obj->protected ) {
  3097. // User must have edit permissions on the draft to preview.
  3098. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  3099. $this->posts = array();
  3100. } else {
  3101. $this->is_preview = true;
  3102. if ( 'future' != $status )
  3103. $this->posts[0]->post_date = current_time('mysql');
  3104. }
  3105. } elseif ( $post_status_obj->private ) {
  3106. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  3107. $this->posts = array();
  3108. } else {
  3109. $this->posts = array();
  3110. }
  3111. }
  3112. }
  3113. if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) {
  3114. /**
  3115. * Filter the single post for preview mode.
  3116. *
  3117. * @since 2.7.0
  3118. *
  3119. * @param WP_Post $post_preview The Post object.
  3120. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3121. */
  3122. $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
  3123. }
  3124. }
  3125. // Put sticky posts at the top of the posts array
  3126. $sticky_posts = get_option('sticky_posts');
  3127. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  3128. $num_posts = count($this->posts);
  3129. $sticky_offset = 0;
  3130. // Loop over posts and relocate stickies to the front.
  3131. for ( $i = 0; $i < $num_posts; $i++ ) {
  3132. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  3133. $sticky_post = $this->posts[$i];
  3134. // Remove sticky from current position
  3135. array_splice($this->posts, $i, 1);
  3136. // Move to front, after other stickies
  3137. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  3138. // Increment the sticky offset. The next sticky will be placed at this offset.
  3139. $sticky_offset++;
  3140. // Remove post from sticky posts array
  3141. $offset = array_search($sticky_post->ID, $sticky_posts);
  3142. unset( $sticky_posts[$offset] );
  3143. }
  3144. }
  3145. // If any posts have been excluded specifically, Ignore those that are sticky.
  3146. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  3147. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  3148. // Fetch sticky posts that weren't in the query results
  3149. if ( !empty($sticky_posts) ) {
  3150. $stickies = get_posts( array(
  3151. 'post__in' => $sticky_posts,
  3152. 'post_type' => $post_type,
  3153. 'post_status' => 'publish',
  3154. 'nopaging' => true
  3155. ) );
  3156. foreach ( $stickies as $sticky_post ) {
  3157. array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
  3158. $sticky_offset++;
  3159. }
  3160. }
  3161. }
  3162. if ( ! $q['suppress_filters'] ) {
  3163. /**
  3164. * Filter the array of retrieved posts after they've been fetched and
  3165. * internally processed.
  3166. *
  3167. * @since 1.5.0
  3168. *
  3169. * @param array $posts The array of retrieved posts.
  3170. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3171. */
  3172. $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) );
  3173. }
  3174. // Ensure that any posts added/modified via one of the filters above are
  3175. // of the type WP_Post and are filtered.
  3176. if ( $this->posts ) {
  3177. $this->post_count = count( $this->posts );
  3178. $this->posts = array_map( 'get_post', $this->posts );
  3179. if ( $q['cache_results'] )
  3180. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  3181. $this->post = reset( $this->posts );
  3182. } else {
  3183. $this->post_count = 0;
  3184. $this->posts = array();
  3185. }
  3186. return $this->posts;
  3187. }
  3188. /**
  3189. * Set up the amount of found posts and the number of pages (if limit clause was used)
  3190. * for the current query.
  3191. *
  3192. * @since 3.5.0
  3193. * @access private
  3194. */
  3195. private function set_found_posts( $q, $limits ) {
  3196. global $wpdb;
  3197. // Bail if posts is an empty array. Continue if posts is an empty string,
  3198. // null, or false to accommodate caching plugins that fill posts later.
  3199. if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
  3200. return;
  3201. if ( ! empty( $limits ) ) {
  3202. /**
  3203. * Filter the query to run for retrieving the found posts.
  3204. *
  3205. * @since 2.1.0
  3206. *
  3207. * @param string $found_posts The query to run to find the found posts.
  3208. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3209. */
  3210. $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
  3211. } else {
  3212. $this->found_posts = count( $this->posts );
  3213. }
  3214. /**
  3215. * Filter the number of found posts for the query.
  3216. *
  3217. * @since 2.1.0
  3218. *
  3219. * @param int $found_posts The number of posts found.
  3220. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3221. */
  3222. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  3223. if ( ! empty( $limits ) )
  3224. $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
  3225. }
  3226. /**
  3227. * Set up the next post and iterate current post index.
  3228. *
  3229. * @since 1.5.0
  3230. * @access public
  3231. *
  3232. * @return WP_Post Next post.
  3233. */
  3234. public function next_post() {
  3235. $this->current_post++;
  3236. $this->post = $this->posts[$this->current_post];
  3237. return $this->post;
  3238. }
  3239. /**
  3240. * Sets up the current post.
  3241. *
  3242. * Retrieves the next post, sets up the post, sets the 'in the loop'
  3243. * property to true.
  3244. *
  3245. * @since 1.5.0
  3246. * @access public
  3247. */
  3248. public function the_post() {
  3249. global $post;
  3250. $this->in_the_loop = true;
  3251. if ( $this->current_post == -1 ) // loop has just started
  3252. /**
  3253. * Fires once the loop is started.
  3254. *
  3255. * @since 2.0.0
  3256. *
  3257. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3258. */
  3259. do_action_ref_array( 'loop_start', array( &$this ) );
  3260. $post = $this->next_post();
  3261. $this->setup_postdata( $post );
  3262. }
  3263. /**
  3264. * Whether there are more posts available in the loop.
  3265. *
  3266. * Calls action 'loop_end', when the loop is complete.
  3267. *
  3268. * @since 1.5.0
  3269. * @access public
  3270. *
  3271. * @return bool True if posts are available, false if end of loop.
  3272. */
  3273. public function have_posts() {
  3274. if ( $this->current_post + 1 < $this->post_count ) {
  3275. return true;
  3276. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  3277. /**
  3278. * Fires once the loop has ended.
  3279. *
  3280. * @since 2.0.0
  3281. *
  3282. * @param WP_Query &$this The WP_Query instance (passed by reference).
  3283. */
  3284. do_action_ref_array( 'loop_end', array( &$this ) );
  3285. // Do some cleaning up after the loop
  3286. $this->rewind_posts();
  3287. }
  3288. $this->in_the_loop = false;
  3289. return false;
  3290. }
  3291. /**
  3292. * Rewind the posts and reset post index.
  3293. *
  3294. * @since 1.5.0
  3295. * @access public
  3296. */
  3297. public function rewind_posts() {
  3298. $this->current_post = -1;
  3299. if ( $this->post_count > 0 ) {
  3300. $this->post = $this->posts[0];
  3301. }
  3302. }
  3303. /**
  3304. * Iterate current comment index and return comment object.
  3305. *
  3306. * @since 2.2.0
  3307. * @access public
  3308. *
  3309. * @return object Comment object.
  3310. */
  3311. public function next_comment() {
  3312. $this->current_comment++;
  3313. $this->comment = $this->comments[$this->current_comment];
  3314. return $this->comment;
  3315. }
  3316. /**
  3317. * Sets up the current comment.
  3318. *
  3319. * @since 2.2.0
  3320. * @access public
  3321. * @global object $comment Current comment.
  3322. */
  3323. public function the_comment() {
  3324. global $comment;
  3325. $comment = $this->next_comment();
  3326. if ( $this->current_comment == 0 ) {
  3327. /**
  3328. * Fires once the comment loop is started.
  3329. *
  3330. * @since 2.2.0
  3331. */
  3332. do_action( 'comment_loop_start' );
  3333. }
  3334. }
  3335. /**
  3336. * Whether there are more comments available.
  3337. *
  3338. * Automatically rewinds comments when finished.
  3339. *
  3340. * @since 2.2.0
  3341. * @access public
  3342. *
  3343. * @return bool True, if more comments. False, if no more posts.
  3344. */
  3345. public function have_comments() {
  3346. if ( $this->current_comment + 1 < $this->comment_count ) {
  3347. return true;
  3348. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  3349. $this->rewind_comments();
  3350. }
  3351. return false;
  3352. }
  3353. /**
  3354. * Rewind the comments, resets the comment index and comment to first.
  3355. *
  3356. * @since 2.2.0
  3357. * @access public
  3358. */
  3359. public function rewind_comments() {
  3360. $this->current_comment = -1;
  3361. if ( $this->comment_count > 0 ) {
  3362. $this->comment = $this->comments[0];
  3363. }
  3364. }
  3365. /**
  3366. * Sets up the WordPress query by parsing query string.
  3367. *
  3368. * @since 1.5.0
  3369. * @access public
  3370. *
  3371. * @param string $query URL query string.
  3372. * @return array List of posts.
  3373. */
  3374. public function query( $query ) {
  3375. $this->init();
  3376. $this->query = $this->query_vars = wp_parse_args( $query );
  3377. return $this->get_posts();
  3378. }
  3379. /**
  3380. * Retrieve queried object.
  3381. *
  3382. * If queried object is not set, then the queried object will be set from
  3383. * the category, tag, taxonomy, posts page, single post, page, or author
  3384. * query variable. After it is set up, it will be returned.
  3385. *
  3386. * @since 1.5.0
  3387. * @access public
  3388. *
  3389. * @return object
  3390. */
  3391. public function get_queried_object() {
  3392. if ( isset($this->queried_object) )
  3393. return $this->queried_object;
  3394. $this->queried_object = null;
  3395. $this->queried_object_id = 0;
  3396. if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  3397. if ( $this->is_category ) {
  3398. if ( $this->get( 'cat' ) ) {
  3399. $term = get_term( $this->get( 'cat' ), 'category' );
  3400. } elseif ( $this->get( 'category_name' ) ) {
  3401. $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
  3402. }
  3403. } elseif ( $this->is_tag ) {
  3404. if ( $this->get( 'tag_id' ) ) {
  3405. $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
  3406. } elseif ( $this->get( 'tag' ) ) {
  3407. $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
  3408. }
  3409. } else {
  3410. // For other tax queries, grab the first term from the first clause.
  3411. $tax_query_in_and = wp_list_filter( $this->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' );
  3412. $queried_taxonomies = array_keys( $tax_query_in_and );
  3413. $matched_taxonomy = reset( $queried_taxonomies );
  3414. $query = $tax_query_in_and[ $matched_taxonomy ];
  3415. if ( $query['terms'] ) {
  3416. if ( 'term_id' == $query['field'] ) {
  3417. $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
  3418. } else {
  3419. $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
  3420. }
  3421. }
  3422. }
  3423. if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
  3424. $this->queried_object = $term;
  3425. $this->queried_object_id = (int) $term->term_id;
  3426. if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
  3427. _make_cat_compat( $this->queried_object );
  3428. }
  3429. } elseif ( $this->is_post_type_archive ) {
  3430. $post_type = $this->get( 'post_type' );
  3431. if ( is_array( $post_type ) )
  3432. $post_type = reset( $post_type );
  3433. $this->queried_object = get_post_type_object( $post_type );
  3434. } elseif ( $this->is_posts_page ) {
  3435. $page_for_posts = get_option('page_for_posts');
  3436. $this->queried_object = get_post( $page_for_posts );
  3437. $this->queried_object_id = (int) $this->queried_object->ID;
  3438. } elseif ( $this->is_singular && ! empty( $this->post ) ) {
  3439. $this->queried_object = $this->post;
  3440. $this->queried_object_id = (int) $this->post->ID;
  3441. } elseif ( $this->is_author ) {
  3442. $this->queried_object_id = (int) $this->get('author');
  3443. $this->queried_object = get_userdata( $this->queried_object_id );
  3444. }
  3445. return $this->queried_object;
  3446. }
  3447. /**
  3448. * Retrieve ID of the current queried object.
  3449. *
  3450. * @since 1.5.0
  3451. * @access public
  3452. *
  3453. * @return int
  3454. */
  3455. public function get_queried_object_id() {
  3456. $this->get_queried_object();
  3457. if ( isset($this->queried_object_id) ) {
  3458. return $this->queried_object_id;
  3459. }
  3460. return 0;
  3461. }
  3462. /**
  3463. * Constructor.
  3464. *
  3465. * Sets up the WordPress query, if parameter is not empty.
  3466. *
  3467. * @since 1.5.0
  3468. * @access public
  3469. *
  3470. * @param string $query URL query string.
  3471. * @return WP_Query
  3472. */
  3473. public function __construct($query = '') {
  3474. if ( ! empty($query) ) {
  3475. $this->query($query);
  3476. }
  3477. }
  3478. /**
  3479. * Make private properties readable for backwards compatibility.
  3480. *
  3481. * @since 4.0.0
  3482. * @access public
  3483. *
  3484. * @param string $name Property to get.
  3485. * @return mixed Property.
  3486. */
  3487. public function __get( $name ) {
  3488. return $this->$name;
  3489. }
  3490. /**
  3491. * Make private properties settable for backwards compatibility.
  3492. *
  3493. * @since 4.0.0
  3494. * @access public
  3495. *
  3496. * @param string $name Property to check if set.
  3497. * @return bool Whether the property is set.
  3498. */
  3499. public function __isset( $name ) {
  3500. return isset( $this->$name );
  3501. }
  3502. /**
  3503. * Make private properties settable for backwards compatibility.
  3504. *
  3505. * @since 4.0.0
  3506. * @access public
  3507. *
  3508. * @param string $name Property to unset.
  3509. */
  3510. public function __unset( $name ) {
  3511. unset( $this->$name );
  3512. }
  3513. /**
  3514. * Make private/protected methods readable for backwards compatibility.
  3515. *
  3516. * @since 4.0.0
  3517. * @access public
  3518. *
  3519. * @param callable $name Method to call.
  3520. * @param array $arguments Arguments to pass when calling.
  3521. * @return mixed|bool Return value of the callback, otherwise false.
  3522. */
  3523. public function __call( $name, $arguments ) {
  3524. return call_user_func_array( array( $this, $name ), $arguments );
  3525. }
  3526. /**
  3527. * Is the query for an existing archive page?
  3528. *
  3529. * Month, Year, Category, Author, Post Type archive...
  3530. *
  3531. * @since 3.1.0
  3532. *
  3533. * @return bool
  3534. */
  3535. public function is_archive() {
  3536. return (bool) $this->is_archive;
  3537. }
  3538. /**
  3539. * Is the query for an existing post type archive page?
  3540. *
  3541. * @since 3.1.0
  3542. *
  3543. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  3544. * @return bool
  3545. */
  3546. public function is_post_type_archive( $post_types = '' ) {
  3547. if ( empty( $post_types ) || ! $this->is_post_type_archive )
  3548. return (bool) $this->is_post_type_archive;
  3549. $post_type = $this->get( 'post_type' );
  3550. if ( is_array( $post_type ) )
  3551. $post_type = reset( $post_type );
  3552. $post_type_object = get_post_type_object( $post_type );
  3553. return in_array( $post_type_object->name, (array) $post_types );
  3554. }
  3555. /**
  3556. * Is the query for an existing attachment page?
  3557. *
  3558. * @since 3.1.0
  3559. *
  3560. * @param mixed $attachment Attachment ID, title, slug, or array of such.
  3561. * @return bool
  3562. */
  3563. public function is_attachment( $attachment = '' ) {
  3564. if ( ! $this->is_attachment ) {
  3565. return false;
  3566. }
  3567. if ( empty( $attachment ) ) {
  3568. return true;
  3569. }
  3570. $attachment = (array) $attachment;
  3571. $post_obj = $this->get_queried_object();
  3572. if ( in_array( $post_obj->ID, $attachment ) ) {
  3573. return true;
  3574. } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
  3575. return true;
  3576. } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
  3577. return true;
  3578. }
  3579. return false;
  3580. }
  3581. /**
  3582. * Is the query for an existing author archive page?
  3583. *
  3584. * If the $author parameter is specified, this function will additionally
  3585. * check if the query is for one of the authors specified.
  3586. *
  3587. * @since 3.1.0
  3588. *
  3589. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  3590. * @return bool
  3591. */
  3592. public function is_author( $author = '' ) {
  3593. if ( !$this->is_author )
  3594. return false;
  3595. if ( empty($author) )
  3596. return true;
  3597. $author_obj = $this->get_queried_object();
  3598. $author = (array) $author;
  3599. if ( in_array( $author_obj->ID, $author ) )
  3600. return true;
  3601. elseif ( in_array( $author_obj->nickname, $author ) )
  3602. return true;
  3603. elseif ( in_array( $author_obj->user_nicename, $author ) )
  3604. return true;
  3605. return false;
  3606. }
  3607. /**
  3608. * Is the query for an existing category archive page?
  3609. *
  3610. * If the $category parameter is specified, this function will additionally
  3611. * check if the query is for one of the categories specified.
  3612. *
  3613. * @since 3.1.0
  3614. *
  3615. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  3616. * @return bool
  3617. */
  3618. public function is_category( $category = '' ) {
  3619. if ( !$this->is_category )
  3620. return false;
  3621. if ( empty($category) )
  3622. return true;
  3623. $cat_obj = $this->get_queried_object();
  3624. $category = (array) $category;
  3625. if ( in_array( $cat_obj->term_id, $category ) )
  3626. return true;
  3627. elseif ( in_array( $cat_obj->name, $category ) )
  3628. return true;
  3629. elseif ( in_array( $cat_obj->slug, $category ) )
  3630. return true;
  3631. return false;
  3632. }
  3633. /**
  3634. * Is the query for an existing tag archive page?
  3635. *
  3636. * If the $tag parameter is specified, this function will additionally
  3637. * check if the query is for one of the tags specified.
  3638. *
  3639. * @since 3.1.0
  3640. *
  3641. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  3642. * @return bool
  3643. */
  3644. public function is_tag( $tag = '' ) {
  3645. if ( ! $this->is_tag )
  3646. return false;
  3647. if ( empty( $tag ) )
  3648. return true;
  3649. $tag_obj = $this->get_queried_object();
  3650. $tag = (array) $tag;
  3651. if ( in_array( $tag_obj->term_id, $tag ) )
  3652. return true;
  3653. elseif ( in_array( $tag_obj->name, $tag ) )
  3654. return true;
  3655. elseif ( in_array( $tag_obj->slug, $tag ) )
  3656. return true;
  3657. return false;
  3658. }
  3659. /**
  3660. * Is the query for an existing taxonomy archive page?
  3661. *
  3662. * If the $taxonomy parameter is specified, this function will additionally
  3663. * check if the query is for that specific $taxonomy.
  3664. *
  3665. * If the $term parameter is specified in addition to the $taxonomy parameter,
  3666. * this function will additionally check if the query is for one of the terms
  3667. * specified.
  3668. *
  3669. * @since 3.1.0
  3670. *
  3671. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  3672. * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  3673. * @return bool
  3674. */
  3675. public function is_tax( $taxonomy = '', $term = '' ) {
  3676. global $wp_taxonomies;
  3677. if ( !$this->is_tax )
  3678. return false;
  3679. if ( empty( $taxonomy ) )
  3680. return true;
  3681. $queried_object = $this->get_queried_object();
  3682. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
  3683. $term_array = (array) $term;
  3684. // Check that the taxonomy matches.
  3685. if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
  3686. return false;
  3687. // Only a Taxonomy provided.
  3688. if ( empty( $term ) )
  3689. return true;
  3690. return isset( $queried_object->term_id ) &&
  3691. count( array_intersect(
  3692. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
  3693. $term_array
  3694. ) );
  3695. }
  3696. /**
  3697. * Whether the current URL is within the comments popup window.
  3698. *
  3699. * @since 3.1.0
  3700. *
  3701. * @return bool
  3702. */
  3703. public function is_comments_popup() {
  3704. return (bool) $this->is_comments_popup;
  3705. }
  3706. /**
  3707. * Is the query for an existing date archive?
  3708. *
  3709. * @since 3.1.0
  3710. *
  3711. * @return bool
  3712. */
  3713. public function is_date() {
  3714. return (bool) $this->is_date;
  3715. }
  3716. /**
  3717. * Is the query for an existing day archive?
  3718. *
  3719. * @since 3.1.0
  3720. *
  3721. * @return bool
  3722. */
  3723. public function is_day() {
  3724. return (bool) $this->is_day;
  3725. }
  3726. /**
  3727. * Is the query for a feed?
  3728. *
  3729. * @since 3.1.0
  3730. *
  3731. * @param string|array $feeds Optional feed types to check.
  3732. * @return bool
  3733. */
  3734. public function is_feed( $feeds = '' ) {
  3735. if ( empty( $feeds ) || ! $this->is_feed )
  3736. return (bool) $this->is_feed;
  3737. $qv = $this->get( 'feed' );
  3738. if ( 'feed' == $qv )
  3739. $qv = get_default_feed();
  3740. return in_array( $qv, (array) $feeds );
  3741. }
  3742. /**
  3743. * Is the query for a comments feed?
  3744. *
  3745. * @since 3.1.0
  3746. *
  3747. * @return bool
  3748. */
  3749. public function is_comment_feed() {
  3750. return (bool) $this->is_comment_feed;
  3751. }
  3752. /**
  3753. * Is the query for the front page of the site?
  3754. *
  3755. * This is for what is displayed at your site's main URL.
  3756. *
  3757. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  3758. *
  3759. * If you set a static page for the front page of your site, this function will return
  3760. * true when viewing that page.
  3761. *
  3762. * Otherwise the same as @see WP_Query::is_home()
  3763. *
  3764. * @since 3.1.0
  3765. *
  3766. * @return bool True, if front of site.
  3767. */
  3768. public function is_front_page() {
  3769. // most likely case
  3770. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
  3771. return true;
  3772. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
  3773. return true;
  3774. else
  3775. return false;
  3776. }
  3777. /**
  3778. * Is the query for the blog homepage?
  3779. *
  3780. * This is the page which shows the time based blog content of your site.
  3781. *
  3782. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  3783. *
  3784. * If you set a static page for the front page of your site, this function will return
  3785. * true only on the page you set as the "Posts page".
  3786. *
  3787. * @see WP_Query::is_front_page()
  3788. *
  3789. * @since 3.1.0
  3790. *
  3791. * @return bool True if blog view homepage.
  3792. */
  3793. public function is_home() {
  3794. return (bool) $this->is_home;
  3795. }
  3796. /**
  3797. * Is the query for an existing month archive?
  3798. *
  3799. * @since 3.1.0
  3800. *
  3801. * @return bool
  3802. */
  3803. public function is_month() {
  3804. return (bool) $this->is_month;
  3805. }
  3806. /**
  3807. * Is the query for an existing single page?
  3808. *
  3809. * If the $page parameter is specified, this function will additionally
  3810. * check if the query is for one of the pages specified.
  3811. *
  3812. * @see WP_Query::is_single()
  3813. * @see WP_Query::is_singular()
  3814. *
  3815. * @since 3.1.0
  3816. *
  3817. * @param mixed $page Page ID, title, slug, path, or array of such.
  3818. * @return bool
  3819. */
  3820. public function is_page( $page = '' ) {
  3821. if ( !$this->is_page )
  3822. return false;
  3823. if ( empty( $page ) )
  3824. return true;
  3825. $page_obj = $this->get_queried_object();
  3826. $page = (array) $page;
  3827. if ( in_array( $page_obj->ID, $page ) ) {
  3828. return true;
  3829. } elseif ( in_array( $page_obj->post_title, $page ) ) {
  3830. return true;
  3831. } else if ( in_array( $page_obj->post_name, $page ) ) {
  3832. return true;
  3833. } else {
  3834. foreach ( $page as $pagepath ) {
  3835. if ( ! strpos( $pagepath, '/' ) ) {
  3836. continue;
  3837. }
  3838. $pagepath_obj = get_page_by_path( $pagepath );
  3839. if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
  3840. return true;
  3841. }
  3842. }
  3843. }
  3844. return false;
  3845. }
  3846. /**
  3847. * Is the query for paged result and not for the first page?
  3848. *
  3849. * @since 3.1.0
  3850. *
  3851. * @return bool
  3852. */
  3853. public function is_paged() {
  3854. return (bool) $this->is_paged;
  3855. }
  3856. /**
  3857. * Is the query for a post or page preview?
  3858. *
  3859. * @since 3.1.0
  3860. *
  3861. * @return bool
  3862. */
  3863. public function is_preview() {
  3864. return (bool) $this->is_preview;
  3865. }
  3866. /**
  3867. * Is the query for the robots file?
  3868. *
  3869. * @since 3.1.0
  3870. *
  3871. * @return bool
  3872. */
  3873. public function is_robots() {
  3874. return (bool) $this->is_robots;
  3875. }
  3876. /**
  3877. * Is the query for a search?
  3878. *
  3879. * @since 3.1.0
  3880. *
  3881. * @return bool
  3882. */
  3883. public function is_search() {
  3884. return (bool) $this->is_search;
  3885. }
  3886. /**
  3887. * Is the query for an existing single post?
  3888. *
  3889. * Works for any post type, except attachments and pages
  3890. *
  3891. * If the $post parameter is specified, this function will additionally
  3892. * check if the query is for one of the Posts specified.
  3893. *
  3894. * @see WP_Query::is_page()
  3895. * @see WP_Query::is_singular()
  3896. *
  3897. * @since 3.1.0
  3898. *
  3899. * @param mixed $post Post ID, title, slug, path, or array of such.
  3900. * @return bool
  3901. */
  3902. public function is_single( $post = '' ) {
  3903. if ( !$this->is_single )
  3904. return false;
  3905. if ( empty($post) )
  3906. return true;
  3907. $post_obj = $this->get_queried_object();
  3908. $post = (array) $post;
  3909. if ( in_array( $post_obj->ID, $post ) ) {
  3910. return true;
  3911. } elseif ( in_array( $post_obj->post_title, $post ) ) {
  3912. return true;
  3913. } elseif ( in_array( $post_obj->post_name, $post ) ) {
  3914. return true;
  3915. } else {
  3916. foreach ( $post as $postpath ) {
  3917. if ( ! strpos( $postpath, '/' ) ) {
  3918. continue;
  3919. }
  3920. $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );
  3921. if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
  3922. return true;
  3923. }
  3924. }
  3925. }
  3926. return false;
  3927. }
  3928. /**
  3929. * Is the query for an existing single post of any post type (post, attachment, page, ... )?
  3930. *
  3931. * If the $post_types parameter is specified, this function will additionally
  3932. * check if the query is for one of the Posts Types specified.
  3933. *
  3934. * @see WP_Query::is_page()
  3935. * @see WP_Query::is_single()
  3936. *
  3937. * @since 3.1.0
  3938. *
  3939. * @param mixed $post_types Optional. Post Type or array of Post Types
  3940. * @return bool
  3941. */
  3942. public function is_singular( $post_types = '' ) {
  3943. if ( empty( $post_types ) || !$this->is_singular )
  3944. return (bool) $this->is_singular;
  3945. $post_obj = $this->get_queried_object();
  3946. return in_array( $post_obj->post_type, (array) $post_types );
  3947. }
  3948. /**
  3949. * Is the query for a specific time?
  3950. *
  3951. * @since 3.1.0
  3952. *
  3953. * @return bool
  3954. */
  3955. public function is_time() {
  3956. return (bool) $this->is_time;
  3957. }
  3958. /**
  3959. * Is the query for a trackback endpoint call?
  3960. *
  3961. * @since 3.1.0
  3962. *
  3963. * @return bool
  3964. */
  3965. public function is_trackback() {
  3966. return (bool) $this->is_trackback;
  3967. }
  3968. /**
  3969. * Is the query for an existing year archive?
  3970. *
  3971. * @since 3.1.0
  3972. *
  3973. * @return bool
  3974. */
  3975. public function is_year() {
  3976. return (bool) $this->is_year;
  3977. }
  3978. /**
  3979. * Is the query a 404 (returns no results)?
  3980. *
  3981. * @since 3.1.0
  3982. *
  3983. * @return bool
  3984. */
  3985. public function is_404() {
  3986. return (bool) $this->is_404;
  3987. }
  3988. /**
  3989. * Is the query the main query?
  3990. *
  3991. * @since 3.3.0
  3992. *
  3993. * @return bool
  3994. */
  3995. public function is_main_query() {
  3996. global $wp_the_query;
  3997. return $wp_the_query === $this;
  3998. }
  3999. /**
  4000. * Set up global post data.
  4001. *
  4002. * @since 4.1.0
  4003. *
  4004. * @param WP_Post $post Post data.
  4005. * @return bool True when finished.
  4006. */
  4007. public function setup_postdata( $post ) {
  4008. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  4009. $id = (int) $post->ID;
  4010. $authordata = get_userdata($post->post_author);
  4011. $currentday = mysql2date('d.m.y', $post->post_date, false);
  4012. $currentmonth = mysql2date('m', $post->post_date, false);
  4013. $numpages = 1;
  4014. $multipage = 0;
  4015. $page = $this->get( 'page' );
  4016. if ( ! $page )
  4017. $page = 1;
  4018. /*
  4019. * Force full post content when viewing the permalink for the $post,
  4020. * or when on an RSS feed. Otherwise respect the 'more' tag.
  4021. */
  4022. if ( $post->ID === get_queried_object_id() && ( $this->is_page() || $this->is_single() ) ) {
  4023. $more = 1;
  4024. } else if ( $this->is_feed() ) {
  4025. $more = 1;
  4026. } else {
  4027. $more = 0;
  4028. }
  4029. $content = $post->post_content;
  4030. if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
  4031. if ( $page > 1 )
  4032. $more = 1;
  4033. $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
  4034. $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
  4035. $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
  4036. // Ignore nextpage at the beginning of the content.
  4037. if ( 0 === strpos( $content, '<!--nextpage-->' ) )
  4038. $content = substr( $content, 15 );
  4039. $pages = explode('<!--nextpage-->', $content);
  4040. $numpages = count($pages);
  4041. if ( $numpages > 1 )
  4042. $multipage = 1;
  4043. } else {
  4044. $pages = array( $post->post_content );
  4045. }
  4046. /**
  4047. * Fires once the post data has been setup.
  4048. *
  4049. * @since 2.8.0
  4050. * @since 4.1.0 Introduced `$this` parameter.
  4051. *
  4052. * @param WP_Post &$post The Post object (passed by reference).
  4053. * @param WP_Query &$this The current Query object (passed by reference).
  4054. */
  4055. do_action_ref_array( 'the_post', array( &$post, &$this ) );
  4056. return true;
  4057. }
  4058. /**
  4059. * After looping through a nested query, this function
  4060. * restores the $post global to the current post in this query.
  4061. *
  4062. * @since 3.7.0
  4063. */
  4064. public function reset_postdata() {
  4065. if ( ! empty( $this->post ) ) {
  4066. $GLOBALS['post'] = $this->post;
  4067. setup_postdata( $this->post );
  4068. }
  4069. }
  4070. }
  4071. /**
  4072. * Redirect old slugs to the correct permalink.
  4073. *
  4074. * Attempts to find the current slug from the past slugs.
  4075. *
  4076. * @since 2.1.0
  4077. *
  4078. * @uses $wp_query
  4079. * @global wpdb $wpdb WordPress database abstraction object.
  4080. *
  4081. * @return null If no link is found, null is returned.
  4082. */
  4083. function wp_old_slug_redirect() {
  4084. global $wp_query;
  4085. if ( is_404() && '' != $wp_query->query_vars['name'] ) :
  4086. global $wpdb;
  4087. // Guess the current post_type based on the query vars.
  4088. if ( get_query_var('post_type') )
  4089. $post_type = get_query_var('post_type');
  4090. elseif ( !empty($wp_query->query_vars['pagename']) )
  4091. $post_type = 'page';
  4092. else
  4093. $post_type = 'post';
  4094. if ( is_array( $post_type ) ) {
  4095. if ( count( $post_type ) > 1 )
  4096. return;
  4097. $post_type = array_shift( $post_type );
  4098. }
  4099. // Do not attempt redirect for hierarchical post types
  4100. if ( is_post_type_hierarchical( $post_type ) )
  4101. return;
  4102. $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']);
  4103. // if year, monthnum, or day have been specified, make our query more precise
  4104. // just in case there are multiple identical _wp_old_slug values
  4105. if ( '' != $wp_query->query_vars['year'] )
  4106. $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']);
  4107. if ( '' != $wp_query->query_vars['monthnum'] )
  4108. $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']);
  4109. if ( '' != $wp_query->query_vars['day'] )
  4110. $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']);
  4111. $id = (int) $wpdb->get_var($query);
  4112. if ( ! $id )
  4113. return;
  4114. $link = get_permalink($id);
  4115. if ( !$link )
  4116. return;
  4117. wp_redirect( $link, 301 ); // Permanent redirect
  4118. exit;
  4119. endif;
  4120. }
  4121. /**
  4122. * Set up global post data.
  4123. *
  4124. * @since 1.5.0
  4125. *
  4126. * @param object $post Post data.
  4127. * @return bool True when finished.
  4128. */
  4129. function setup_postdata( $post ) {
  4130. global $wp_query;
  4131. if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
  4132. return $wp_query->setup_postdata( $post );
  4133. }
  4134. return false;
  4135. }