PageRenderTime 77ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/sandbox/wp-includes/query.php

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