PageRenderTime 87ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/query.php

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