Advertisement
Guest User

query.php

a guest
Jan 4th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.84 KB | None | 0 0
  1. <?php
  2. /**
  3.  * WordPress Query API
  4.  *
  5.  * The query API attempts to get which part of WordPress the user is on. It
  6.  * also provides functionality for getting URL query information.
  7.  *
  8.  * @link https://codex.wordpress.org/The_Loop More information on The Loop.
  9.  *
  10.  * @package WordPress
  11.  * @subpackage Query
  12.  */
  13.  
  14. /**
  15.  * Retrieve variable in the WP_Query class.
  16.  *
  17.  * @since 1.5.0
  18.  * @since 3.9.0 The `$default` argument was introduced.
  19.  *
  20.  * @global WP_Query $wp_query Global WP_Query instance.
  21.  *
  22.  * @param string $var       The variable key to retrieve.
  23.  * @param mixed  $default   Optional. Value to return if the query variable is not set. Default empty.
  24.  * @return mixed Contents of the query variable.
  25.  */
  26. function get_query_var( $var, $default = '' ) {
  27.     global $wp_query;
  28.     return $wp_query->get( $var, $default );
  29. }
  30.  
  31. /**
  32.  * Retrieve the currently-queried object.
  33.  *
  34.  * Wrapper for WP_Query::get_queried_object().
  35.  *
  36.  * @since 3.1.0
  37.  *
  38.  * @global WP_Query $wp_query Global WP_Query instance.
  39.  *
  40.  * @return object Queried object.
  41.  */
  42. function get_queried_object() {
  43.     global $wp_query;
  44.     return $wp_query->get_queried_object();
  45. }
  46.  
  47. /**
  48.  * Retrieve ID of the current queried object.
  49.  *
  50.  * Wrapper for WP_Query::get_queried_object_id().
  51.  *
  52.  * @since 3.1.0
  53.  *
  54.  * @global WP_Query $wp_query Global WP_Query instance.
  55.  *
  56.  * @return int ID of the queried object.
  57.  */
  58. function get_queried_object_id() {
  59.     global $wp_query;
  60.     return $wp_query->get_queried_object_id();
  61. }
  62.  
  63. /**
  64.  * Set query variable.
  65.  *
  66.  * @since 2.2.0
  67.  *
  68.  * @global WP_Query $wp_query Global WP_Query instance.
  69.  *
  70.  * @param string $var   Query variable key.
  71.  * @param mixed  $value Query variable value.
  72.  */
  73. function set_query_var( $var, $value ) {
  74.     global $wp_query;
  75.     $wp_query->set( $var, $value );
  76. }
  77.  
  78. /**
  79.  * Sets up The Loop with query parameters.
  80.  *
  81.  * Note: This function will completely override the main query and isn't intended for use
  82.  * by plugins or themes. Its overly-simplistic approach to modifying the main query can be
  83.  * problematic and should be avoided wherever possible. In most cases, there are better,
  84.  * more performant options for modifying the main query such as via the {@see 'pre_get_posts'}
  85.  * action within WP_Query.
  86.  *
  87.  * This must not be used within the WordPress Loop.
  88.  *
  89.  * @since 1.5.0
  90.  *
  91.  * @global WP_Query $wp_query Global WP_Query instance.
  92.  *
  93.  * @param array|string $query Array or string of WP_Query arguments.
  94.  * @return array List of post objects.
  95.  */
  96. function query_posts($query) {
  97.     $GLOBALS['wp_query'] = new WP_Query();
  98.     return $GLOBALS['wp_query']->query($query);
  99. }
  100.  
  101. /**
  102.  * Destroys the previous query and sets up a new query.
  103.  *
  104.  * This should be used after query_posts() and before another query_posts().
  105.  * This will remove obscure bugs that occur when the previous WP_Query object
  106.  * is not destroyed properly before another is set up.
  107.  *
  108.  * @since 2.3.0
  109.  *
  110.  * @global WP_Query $wp_query     Global WP_Query instance.
  111.  * @global WP_Query $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
  112.  */
  113. function wp_reset_query() {
  114.     $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
  115.     wp_reset_postdata();
  116. }
  117.  
  118. /**
  119.  * After looping through a separate query, this function restores
  120.  * the $post global to the current post in the main query.
  121.  *
  122.  * @since 3.0.0
  123.  *
  124.  * @global WP_Query $wp_query Global WP_Query instance.
  125.  */
  126. function wp_reset_postdata() {
  127.     global $wp_query;
  128.  
  129.     if ( isset( $wp_query ) ) {
  130.         $wp_query->reset_postdata();
  131.     }
  132. }
  133.  
  134. /*
  135.  * Query type checks.
  136.  */
  137.  
  138. /**
  139.  * Is the query for an existing archive page?
  140.  *
  141.  * Month, Year, Category, Author, Post Type archive...
  142.  *
  143.  * @since 1.5.0
  144.  *
  145.  * @global WP_Query $wp_query Global WP_Query instance.
  146.  *
  147.  * @return bool
  148.  */
  149. function is_archive() {
  150.     global $wp_query;
  151.  
  152.     if ( ! isset( $wp_query ) ) {
  153.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  154.         return false;
  155.     }
  156.  
  157.     return $wp_query->is_archive();
  158. }
  159.  
  160. /**
  161.  * Is the query for an existing post type archive page?
  162.  *
  163.  * @since 3.1.0
  164.  *
  165.  * @global WP_Query $wp_query Global WP_Query instance.
  166.  *
  167.  * @param string|array $post_types Optional. Post type or array of posts types to check against.
  168.  * @return bool
  169.  */
  170. function is_post_type_archive( $post_types = '' ) {
  171.     global $wp_query;
  172.  
  173.     if ( ! isset( $wp_query ) ) {
  174.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  175.         return false;
  176.     }
  177.  
  178.     return $wp_query->is_post_type_archive( $post_types );
  179. }
  180.  
  181. /**
  182.  * Is the query for an existing attachment page?
  183.  *
  184.  * @since 2.0.0
  185.  *
  186.  * @global WP_Query $wp_query Global WP_Query instance.
  187.  *
  188.  * @param int|string|array|object $attachment Attachment ID, title, slug, or array of such.
  189.  * @return bool
  190.  */
  191. function is_attachment( $attachment = '' ) {
  192.     global $wp_query;
  193.  
  194.     if ( ! isset( $wp_query ) ) {
  195.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  196.         return false;
  197.     }
  198.  
  199.     return $wp_query->is_attachment( $attachment );
  200. }
  201.  
  202. /**
  203.  * Is the query for an existing author archive page?
  204.  *
  205.  * If the $author parameter is specified, this function will additionally
  206.  * check if the query is for one of the authors specified.
  207.  *
  208.  * @since 1.5.0
  209.  *
  210.  * @global WP_Query $wp_query Global WP_Query instance.
  211.  *
  212.  * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  213.  * @return bool
  214.  */
  215. function is_author( $author = '' ) {
  216.     global $wp_query;
  217.  
  218.     if ( ! isset( $wp_query ) ) {
  219.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  220.         return false;
  221.     }
  222.  
  223.     return $wp_query->is_author( $author );
  224. }
  225.  
  226. /**
  227.  * Is the query for an existing category archive page?
  228.  *
  229.  * If the $category parameter is specified, this function will additionally
  230.  * check if the query is for one of the categories specified.
  231.  *
  232.  * @since 1.5.0
  233.  *
  234.  * @global WP_Query $wp_query Global WP_Query instance.
  235.  *
  236.  * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  237.  * @return bool
  238.  */
  239. function is_category( $category = '' ) {
  240.     global $wp_query;
  241.  
  242.     if ( ! isset( $wp_query ) ) {
  243.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  244.         return false;
  245.     }
  246.  
  247.     return $wp_query->is_category( $category );
  248. }
  249.  
  250. /**
  251.  * Is the query for an existing tag archive page?
  252.  *
  253.  * If the $tag parameter is specified, this function will additionally
  254.  * check if the query is for one of the tags specified.
  255.  *
  256.  * @since 2.3.0
  257.  *
  258.  * @global WP_Query $wp_query Global WP_Query instance.
  259.  *
  260.  * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  261.  * @return bool
  262.  */
  263. function is_tag( $tag = '' ) {
  264.     global $wp_query;
  265.  
  266.     if ( ! isset( $wp_query ) ) {
  267.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  268.         return false;
  269.     }
  270.  
  271.     return $wp_query->is_tag( $tag );
  272. }
  273.  
  274. /**
  275.  * Is the query for an existing custom taxonomy archive page?
  276.  *
  277.  * If the $taxonomy parameter is specified, this function will additionally
  278.  * check if the query is for that specific $taxonomy.
  279.  *
  280.  * If the $term parameter is specified in addition to the $taxonomy parameter,
  281.  * this function will additionally check if the query is for one of the terms
  282.  * specified.
  283.  *
  284.  * @since 2.5.0
  285.  *
  286.  * @global WP_Query $wp_query Global WP_Query instance.
  287.  *
  288.  * @param string|array     $taxonomy Optional. Taxonomy slug or slugs.
  289.  * @param int|string|array $term     Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  290.  * @return bool True for custom taxonomy archive pages, false for built-in taxonomies (category and tag archives).
  291.  */
  292. function is_tax( $taxonomy = '', $term = '' ) {
  293.     global $wp_query;
  294.  
  295.     if ( ! isset( $wp_query ) ) {
  296.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  297.         return false;
  298.     }
  299.  
  300.     return $wp_query->is_tax( $taxonomy, $term );
  301. }
  302.  
  303. /**
  304.  * Is the query for an existing date archive?
  305.  *
  306.  * @since 1.5.0
  307.  *
  308.  * @global WP_Query $wp_query Global WP_Query instance.
  309.  *
  310.  * @return bool
  311.  */
  312. function is_date() {
  313.     global $wp_query;
  314.  
  315.     if ( ! isset( $wp_query ) ) {
  316.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  317.         return false;
  318.     }
  319.  
  320.     return $wp_query->is_date();
  321. }
  322.  
  323. /**
  324.  * Is the query for an existing day archive?
  325.  *
  326.  * @since 1.5.0
  327.  *
  328.  * @global WP_Query $wp_query Global WP_Query instance.
  329.  *
  330.  * @return bool
  331.  */
  332. function is_day() {
  333.     global $wp_query;
  334.  
  335.     if ( ! isset( $wp_query ) ) {
  336.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  337.         return false;
  338.     }
  339.  
  340.     return $wp_query->is_day();
  341. }
  342.  
  343. /**
  344.  * Is the query for a feed?
  345.  *
  346.  * @since 1.5.0
  347.  *
  348.  * @global WP_Query $wp_query Global WP_Query instance.
  349.  *
  350.  * @param string|array $feeds Optional feed types to check.
  351.  * @return bool
  352.  */
  353. function is_feed( $feeds = '' ) {
  354.     global $wp_query;
  355.  
  356.     if ( ! isset( $wp_query ) ) {
  357.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  358.         return false;
  359.     }
  360.  
  361.     return $wp_query->is_feed( $feeds );
  362. }
  363.  
  364. /**
  365.  * Is the query for a comments feed?
  366.  *
  367.  * @since 3.0.0
  368.  *
  369.  * @global WP_Query $wp_query Global WP_Query instance.
  370.  *
  371.  * @return bool
  372.  */
  373. function is_comment_feed() {
  374.     global $wp_query;
  375.  
  376.     if ( ! isset( $wp_query ) ) {
  377.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  378.         return false;
  379.     }
  380.  
  381.     return $wp_query->is_comment_feed();
  382. }
  383.  
  384. /**
  385.  * Is the query for the front page of the site?
  386.  *
  387.  * This is for what is displayed at your site's main URL.
  388.  *
  389.  * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  390.  *
  391.  * If you set a static page for the front page of your site, this function will return
  392.  * true when viewing that page.
  393.  *
  394.  * Otherwise the same as @see is_home()
  395.  *
  396.  * @since 2.5.0
  397.  *
  398.  * @global WP_Query $wp_query Global WP_Query instance.
  399.  *
  400.  * @return bool True, if front of site.
  401.  */
  402. function is_front_page() {
  403.     global $wp_query;
  404.  
  405.     if ( ! isset( $wp_query ) ) {
  406.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  407.         return false;
  408.     }
  409.  
  410.     return $wp_query->is_front_page();
  411. }
  412.  
  413. /**
  414.  * Determines if the query is for the blog homepage.
  415.  *
  416.  * The blog homepage is the page that shows the time-based blog content of the site.
  417.  *
  418.  * is_home() is dependent on the site's "Front page displays" Reading Settings 'show_on_front'
  419.  * and 'page_for_posts'.
  420.  *
  421.  * If a static page is set for the front page of the site, this function will return true only
  422.  * on the page you set as the "Posts page".
  423.  *
  424.  * @since 1.5.0
  425.  *
  426.  * @see is_front_page()
  427.  * @global WP_Query $wp_query Global WP_Query instance.
  428.  *
  429.  * @return bool True if blog view homepage, otherwise false.
  430.  */
  431. function is_home() {
  432.     global $wp_query;
  433.  
  434.     if ( ! isset( $wp_query ) ) {
  435.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  436.         return false;
  437.     }
  438.  
  439.     return $wp_query->is_home();
  440. }
  441.  
  442. /**
  443.  * Is the query for an existing month archive?
  444.  *
  445.  * @since 1.5.0
  446.  *
  447.  * @global WP_Query $wp_query Global WP_Query instance.
  448.  *
  449.  * @return bool
  450.  */
  451. function is_month() {
  452.     global $wp_query;
  453.  
  454.     if ( ! isset( $wp_query ) ) {
  455.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  456.         return false;
  457.     }
  458.  
  459.     return $wp_query->is_month();
  460. }
  461.  
  462. /**
  463.  * Is the query for an existing single page?
  464.  *
  465.  * If the $page parameter is specified, this function will additionally
  466.  * check if the query is for one of the pages specified.
  467.  *
  468.  * @see is_single()
  469.  * @see is_singular()
  470.  *
  471.  * @since 1.5.0
  472.  *
  473.  * @global WP_Query $wp_query Global WP_Query instance.
  474.  *
  475.  * @param int|string|array $page Optional. Page ID, title, slug, or array of such. Default empty.
  476.  * @return bool Whether the query is for an existing single page.
  477.  */
  478. function is_page( $page = '' ) {
  479.     global $wp_query;
  480.  
  481.     if ( ! isset( $wp_query ) ) {
  482.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  483.         return false;
  484.     }
  485.  
  486.     return $wp_query->is_page( $page );
  487. }
  488.  
  489. /**
  490.  * Is the query for paged result and not for the first page?
  491.  *
  492.  * @since 1.5.0
  493.  *
  494.  * @global WP_Query $wp_query Global WP_Query instance.
  495.  *
  496.  * @return bool
  497.  */
  498. function is_paged() {
  499.     global $wp_query;
  500.  
  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.0' );
  503.         return false;
  504.     }
  505.  
  506.     return $wp_query->is_paged();
  507. }
  508.  
  509. /**
  510.  * Is the query for a post or page preview?
  511.  *
  512.  * @since 2.0.0
  513.  *
  514.  * @global WP_Query $wp_query Global WP_Query instance.
  515.  *
  516.  * @return bool
  517.  */
  518. function is_preview() {
  519.     global $wp_query;
  520.  
  521.     if ( ! isset( $wp_query ) ) {
  522.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  523.         return false;
  524.     }
  525.  
  526.     return $wp_query->is_preview();
  527. }
  528.  
  529. /**
  530.  * Is the query for the robots file?
  531.  *
  532.  * @since 2.1.0
  533.  *
  534.  * @global WP_Query $wp_query Global WP_Query instance.
  535.  *
  536.  * @return bool
  537.  */
  538. function is_robots() {
  539.     global $wp_query;
  540.  
  541.     if ( ! isset( $wp_query ) ) {
  542.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  543.         return false;
  544.     }
  545.  
  546.     return $wp_query->is_robots();
  547. }
  548.  
  549. /**
  550.  * Is the query for a search?
  551.  *
  552.  * @since 1.5.0
  553.  *
  554.  * @global WP_Query $wp_query Global WP_Query instance.
  555.  *
  556.  * @return bool
  557.  */
  558. function is_search() {
  559.     global $wp_query;
  560.  
  561.     if ( ! isset( $wp_query ) ) {
  562.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  563.         return false;
  564.     }
  565.  
  566.     return $wp_query->is_search();
  567. }
  568.  
  569. /**
  570.  * Is the query for an existing single post?
  571.  *
  572.  * Works for any post type, except attachments and pages
  573.  *
  574.  * If the $post parameter is specified, this function will additionally
  575.  * check if the query is for one of the Posts specified.
  576.  *
  577.  * @see is_page()
  578.  * @see is_singular()
  579.  *
  580.  * @since 1.5.0
  581.  *
  582.  * @global WP_Query $wp_query Global WP_Query instance.
  583.  *
  584.  * @param int|string|array $post Optional. Post ID, title, slug, or array of such. Default empty.
  585.  * @return bool Whether the query is for an existing single post.
  586.  */
  587. function is_single( $post = '' ) {
  588.     global $wp_query;
  589.  
  590.     if ( ! isset( $wp_query ) ) {
  591.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  592.         return false;
  593.     }
  594.  
  595.     return $wp_query->is_single( $post );
  596. }
  597.  
  598. /**
  599.  * Is the query for an existing single post of any post type (post, attachment, page,
  600.  * custom post types)?
  601.  *
  602.  * If the $post_types parameter is specified, this function will additionally
  603.  * check if the query is for one of the Posts Types specified.
  604.  *
  605.  * @see is_page()
  606.  * @see is_single()
  607.  *
  608.  * @since 1.5.0
  609.  *
  610.  * @global WP_Query $wp_query Global WP_Query instance.
  611.  *
  612.  * @param string|array $post_types Optional. Post type or array of post types. Default empty.
  613.  * @return bool Whether the query is for an existing single post of any of the given post types.
  614.  */
  615. function is_singular( $post_types = '' ) {
  616.     global $wp_query;
  617.  
  618.     if ( ! isset( $wp_query ) ) {
  619.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  620.         return false;
  621.     }
  622.  
  623.     return $wp_query->is_singular( $post_types );
  624. }
  625.  
  626. /**
  627.  * Is the query for a specific time?
  628.  *
  629.  * @since 1.5.0
  630.  *
  631.  * @global WP_Query $wp_query Global WP_Query instance.
  632.  *
  633.  * @return bool
  634.  */
  635. function is_time() {
  636.     global $wp_query;
  637.  
  638.     if ( ! isset( $wp_query ) ) {
  639.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  640.         return false;
  641.     }
  642.  
  643.     return $wp_query->is_time();
  644. }
  645.  
  646. /**
  647.  * Is the query for a trackback endpoint call?
  648.  *
  649.  * @since 1.5.0
  650.  *
  651.  * @global WP_Query $wp_query Global WP_Query instance.
  652.  *
  653.  * @return bool
  654.  */
  655. function is_trackback() {
  656.     global $wp_query;
  657.  
  658.     if ( ! isset( $wp_query ) ) {
  659.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  660.         return false;
  661.     }
  662.  
  663.     return $wp_query->is_trackback();
  664. }
  665.  
  666. /**
  667.  * Is the query for an existing year archive?
  668.  *
  669.  * @since 1.5.0
  670.  *
  671.  * @global WP_Query $wp_query Global WP_Query instance.
  672.  *
  673.  * @return bool
  674.  */
  675. function is_year() {
  676.     global $wp_query;
  677.  
  678.     if ( ! isset( $wp_query ) ) {
  679.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  680.         return false;
  681.     }
  682.  
  683.     return $wp_query->is_year();
  684. }
  685.  
  686. /**
  687.  * Is the query a 404 (returns no results)?
  688.  *
  689.  * @since 1.5.0
  690.  *
  691.  * @global WP_Query $wp_query Global WP_Query instance.
  692.  *
  693.  * @return bool
  694.  */
  695. function is_404() {
  696.     global $wp_query;
  697.  
  698.     if ( ! isset( $wp_query ) ) {
  699.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  700.         return false;
  701.     }
  702.  
  703.     return $wp_query->is_404();
  704. }
  705.  
  706. /**
  707.  * Is the query for an embedded post?
  708.  *
  709.  * @since 4.4.0
  710.  *
  711.  * @global WP_Query $wp_query Global WP_Query instance.
  712.  *
  713.  * @return bool Whether we're in an embedded post or not.
  714.  */
  715. function is_embed() {
  716.     global $wp_query;
  717.  
  718.     if ( ! isset( $wp_query ) ) {
  719.         _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
  720.         return false;
  721.     }
  722.  
  723.     return $wp_query->is_embed();
  724. }
  725.  
  726. /**
  727.  * Is the query the main query?
  728.  *
  729.  * @since 3.3.0
  730.  *
  731.  * @global WP_Query $wp_query Global WP_Query instance.
  732.  *
  733.  * @return bool
  734.  */
  735. function is_main_query() {
  736.     if ( 'pre_get_posts' === current_filter() ) {
  737.         $message = sprintf(
  738.             /* translators: 1: pre_get_posts 2: WP_Query->is_main_query() 3: is_main_query() 4: link to codex is_main_query() page. */
  739.             __( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
  740.             '<code>pre_get_posts</code>',
  741.             '<code>WP_Query->is_main_query()</code>',
  742.             '<code>is_main_query()</code>',
  743.             __( 'https://codex.wordpress.org/Function_Reference/is_main_query' )
  744.         );
  745.         _doing_it_wrong( __FUNCTION__, $message, '3.7.0' );
  746.     }
  747.  
  748.     global $wp_query;
  749.     return $wp_query->is_main_query();
  750. }
  751.  
  752. /*
  753.  * The Loop. Post loop control.
  754.  */
  755.  
  756. /**
  757.  * Whether current WordPress query has results to loop over.
  758.  *
  759.  * @since 1.5.0
  760.  *
  761.  * @global WP_Query $wp_query Global WP_Query instance.
  762.  *
  763.  * @return bool
  764.  */
  765. function have_posts() {
  766.     global $wp_query;
  767.     return $wp_query->have_posts();
  768. }
  769.  
  770. /**
  771.  * Whether the caller is in the Loop.
  772.  *
  773.  * @since 2.0.0
  774.  *
  775.  * @global WP_Query $wp_query Global WP_Query instance.
  776.  *
  777.  * @return bool True if caller is within loop, false if loop hasn't started or ended.
  778.  */
  779. function in_the_loop() {
  780.     global $wp_query;
  781.     return $wp_query->in_the_loop;
  782. }
  783.  
  784. /**
  785.  * Rewind the loop posts.
  786.  *
  787.  * @since 1.5.0
  788.  *
  789.  * @global WP_Query $wp_query Global WP_Query instance.
  790.  */
  791. function rewind_posts() {
  792.     global $wp_query;
  793.     $wp_query->rewind_posts();
  794. }
  795.  
  796. /**
  797.  * Iterate the post index in the loop.
  798.  *
  799.  * @since 1.5.0
  800.  *
  801.  * @global WP_Query $wp_query Global WP_Query instance.
  802.  */
  803. function the_post() {
  804.     global $wp_query;
  805.     $wp_query->the_post();
  806. }
  807.  
  808. /*
  809.  * Comments loop.
  810.  */
  811.  
  812. /**
  813.  * Whether there are comments to loop over.
  814.  *
  815.  * @since 2.2.0
  816.  *
  817.  * @global WP_Query $wp_query Global WP_Query instance.
  818.  *
  819.  * @return bool
  820.  */
  821. function have_comments() {
  822.     global $wp_query;
  823.     return $wp_query->have_comments();
  824. }
  825.  
  826. /**
  827.  * Iterate comment index in the comment loop.
  828.  *
  829.  * @since 2.2.0
  830.  *
  831.  * @global WP_Query $wp_query Global WP_Query instance.
  832.  *
  833.  * @return object
  834.  */
  835. function the_comment() {
  836.     global $wp_query;
  837.     return $wp_query->the_comment();
  838. }
  839.  
  840. /**
  841.  * Redirect old slugs to the correct permalink.
  842.  *
  843.  * Attempts to find the current slug from the past slugs.
  844.  *
  845.  * @since 2.1.0
  846.  *
  847.  * @global wpdb $wpdb WordPress database abstraction object.
  848.  */
  849. function wp_old_slug_redirect() {
  850.     if ( is_404() && '' !== get_query_var( 'name' ) ) {
  851.         global $wpdb;
  852.  
  853.         // Guess the current post_type based on the query vars.
  854.         if ( get_query_var( 'post_type' ) ) {
  855.             $post_type = get_query_var( 'post_type' );
  856.         } elseif ( get_query_var( 'attachment' ) ) {
  857.             $post_type = 'attachment';
  858.         } elseif ( get_query_var( 'pagename' ) ) {
  859.             $post_type = 'page';
  860.         } else {
  861.             $post_type = 'post';
  862.         }
  863.  
  864.         if ( is_array( $post_type ) ) {
  865.             if ( count( $post_type ) > 1 ) {
  866.                 return;
  867.             }
  868.             $post_type = reset( $post_type );
  869.         }
  870.  
  871.         // Do not attempt redirect for hierarchical post types
  872.         if ( is_post_type_hierarchical( $post_type ) ) {
  873.             return;
  874.         }
  875.  
  876.         $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, get_query_var( 'name' ) );
  877.  
  878.         // if year, monthnum, or day have been specified, make our query more precise
  879.         // just in case there are multiple identical _wp_old_slug values
  880.         if ( get_query_var( 'year' ) ) {
  881.             $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var( 'year' ) );
  882.         }
  883.         if ( get_query_var( 'monthnum' ) ) {
  884.             $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
  885.         }
  886.         if ( get_query_var( 'day' ) ) {
  887.             $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
  888.         }
  889.  
  890.         $id = (int) $wpdb->get_var( $query );
  891.  
  892.         if ( ! $id ) {
  893.             return;
  894.         }
  895.  
  896.         $link = get_permalink( $id );
  897.  
  898.         if ( get_query_var( 'paged' ) > 1 ) {
  899.             $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) );
  900.         } elseif( is_embed() ) {
  901.             $link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
  902.         }
  903.  
  904.         /**
  905.          * Filters the old slug redirect URL.
  906.          *
  907.          * @since 4.4.0
  908.          *
  909.          * @param string $link The redirect URL.
  910.          */
  911.         $link = apply_filters( 'old_slug_redirect_url', $link );
  912.  
  913.         if ( ! $link ) {
  914.             return;
  915.         }
  916.  
  917.         wp_redirect( $link, 301 ); // Permanent redirect
  918.         exit;
  919.     }
  920. }
  921.  
  922. /**
  923.  * Set up global post data.
  924.  *
  925.  * @since 1.5.0
  926.  * @since 4.4.0 Added the ability to pass a post ID to `$post`.
  927.  *
  928.  * @global WP_Query $wp_query Global WP_Query instance.
  929.  *
  930.  * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  931.  * @return bool True when finished.
  932.  */
  933. function setup_postdata( $post ) {
  934.     global $wp_query;
  935.  
  936.     if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
  937.         return $wp_query->setup_postdata( $post );
  938.     }
  939.  
  940.     return false;
  941. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement