[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> rewrite.php (source)

   1  <?php
   2  /**
   3   * WordPress Rewrite API
   4   *
   5   * @package WordPress
   6   * @subpackage Rewrite
   7   */
   8  
   9  /**
  10   * Endpoint mask that matches nothing.
  11   *
  12   * @since 2.1.0
  13   */
  14  define( 'EP_NONE', 0 );
  15  
  16  /**
  17   * Endpoint mask that matches post permalinks.
  18   *
  19   * @since 2.1.0
  20   */
  21  define( 'EP_PERMALINK', 1 );
  22  
  23  /**
  24   * Endpoint mask that matches attachment permalinks.
  25   *
  26   * @since 2.1.0
  27   */
  28  define( 'EP_ATTACHMENT', 2 );
  29  
  30  /**
  31   * Endpoint mask that matches any date archives.
  32   *
  33   * @since 2.1.0
  34   */
  35  define( 'EP_DATE', 4 );
  36  
  37  /**
  38   * Endpoint mask that matches yearly archives.
  39   *
  40   * @since 2.1.0
  41   */
  42  define( 'EP_YEAR', 8 );
  43  
  44  /**
  45   * Endpoint mask that matches monthly archives.
  46   *
  47   * @since 2.1.0
  48   */
  49  define( 'EP_MONTH', 16 );
  50  
  51  /**
  52   * Endpoint mask that matches daily archives.
  53   *
  54   * @since 2.1.0
  55   */
  56  define( 'EP_DAY', 32 );
  57  
  58  /**
  59   * Endpoint mask that matches the site root.
  60   *
  61   * @since 2.1.0
  62   */
  63  define( 'EP_ROOT', 64 );
  64  
  65  /**
  66   * Endpoint mask that matches comment feeds.
  67   *
  68   * @since 2.1.0
  69   */
  70  define( 'EP_COMMENTS', 128 );
  71  
  72  /**
  73   * Endpoint mask that matches searches.
  74   *
  75   * Note that this only matches a search at a "pretty" URL such as
  76   * `/search/my-search-term`, not `?s=my-search-term`.
  77   *
  78   * @since 2.1.0
  79   */
  80  define( 'EP_SEARCH', 256 );
  81  
  82  /**
  83   * Endpoint mask that matches category archives.
  84   *
  85   * @since 2.1.0
  86   */
  87  define( 'EP_CATEGORIES', 512 );
  88  
  89  /**
  90   * Endpoint mask that matches tag archives.
  91   *
  92   * @since 2.3.0
  93   */
  94  define( 'EP_TAGS', 1024 );
  95  
  96  /**
  97   * Endpoint mask that matches author archives.
  98   *
  99   * @since 2.1.0
 100   */
 101  define( 'EP_AUTHORS', 2048 );
 102  
 103  /**
 104   * Endpoint mask that matches pages.
 105   *
 106   * @since 2.1.0
 107   */
 108  define( 'EP_PAGES', 4096 );
 109  
 110  /**
 111   * Endpoint mask that matches all archive views.
 112   *
 113   * @since 3.7.0
 114   */
 115  define( 'EP_ALL_ARCHIVES', EP_DATE | EP_YEAR | EP_MONTH | EP_DAY | EP_CATEGORIES | EP_TAGS | EP_AUTHORS );
 116  
 117  /**
 118   * Endpoint mask that matches everything.
 119   *
 120   * @since 2.1.0
 121   */
 122  define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEARCH | EP_PAGES | EP_ALL_ARCHIVES );
 123  
 124  /**
 125   * Adds a rewrite rule that transforms a URL structure to a set of query vars.
 126   *
 127   * Any value in the $after parameter that isn't 'bottom' will result in the rule
 128   * being placed at the top of the rewrite rules.
 129   *
 130   * @since 2.1.0
 131   * @since 4.4.0 Array support was added to the `$query` parameter.
 132   *
 133   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 134   *
 135   * @param string       $regex Regular expression to match request against.
 136   * @param string|array $query The corresponding query vars for this rewrite rule.
 137   * @param string       $after Optional. Priority of the new rule. Accepts 'top'
 138   *                            or 'bottom'. Default 'bottom'.
 139   */
 140  function add_rewrite_rule( $regex, $query, $after = 'bottom' ) {
 141      global $wp_rewrite;
 142  
 143      $wp_rewrite->add_rule( $regex, $query, $after );
 144  }
 145  
 146  /**
 147   * Adds a new rewrite tag (like %postname%).
 148   *
 149   * The `$query` parameter is optional. If it is omitted you must ensure that you call
 150   * this on, or before, the {@see 'init'} hook. This is because `$query` defaults to
 151   * `$tag=`, and for this to work a new query var has to be added.
 152   *
 153   * @since 2.1.0
 154   *
 155   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 156   * @global WP         $wp         Current WordPress environment instance.
 157   *
 158   * @param string $tag   Name of the new rewrite tag.
 159   * @param string $regex Regular expression to substitute the tag for in rewrite rules.
 160   * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
 161   */
 162  function add_rewrite_tag( $tag, $regex, $query = '' ) {
 163      // Validate the tag's name.
 164      if ( strlen( $tag ) < 3 || '%' !== $tag[0] || '%' !== $tag[ strlen( $tag ) - 1 ] ) {
 165          return;
 166      }
 167  
 168      global $wp_rewrite, $wp;
 169  
 170      if ( empty( $query ) ) {
 171          $qv = trim( $tag, '%' );
 172          $wp->add_query_var( $qv );
 173          $query = $qv . '=';
 174      }
 175  
 176      $wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
 177  }
 178  
 179  /**
 180   * Removes an existing rewrite tag (like %postname%).
 181   *
 182   * @since 4.5.0
 183   *
 184   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 185   *
 186   * @param string $tag Name of the rewrite tag.
 187   */
 188  function remove_rewrite_tag( $tag ) {
 189      global $wp_rewrite;
 190      $wp_rewrite->remove_rewrite_tag( $tag );
 191  }
 192  
 193  /**
 194   * Adds a permalink structure.
 195   *
 196   * @since 3.0.0
 197   *
 198   * @see WP_Rewrite::add_permastruct()
 199   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 200   *
 201   * @param string $name   Name for permalink structure.
 202   * @param string $struct Permalink structure.
 203   * @param array  $args   Optional. Arguments for building the rules from the permalink structure,
 204   *                       see WP_Rewrite::add_permastruct() for full details. Default empty array.
 205   */
 206  function add_permastruct( $name, $struct, $args = array() ) {
 207      global $wp_rewrite;
 208  
 209      // Back-compat for the old parameters: $with_front and $ep_mask.
 210      if ( ! is_array( $args ) ) {
 211          $args = array( 'with_front' => $args );
 212      }
 213  
 214      if ( func_num_args() === 4 ) {
 215          $args['ep_mask'] = func_get_arg( 3 );
 216      }
 217  
 218      $wp_rewrite->add_permastruct( $name, $struct, $args );
 219  }
 220  
 221  /**
 222   * Removes a permalink structure.
 223   *
 224   * Can only be used to remove permastructs that were added using add_permastruct().
 225   * Built-in permastructs cannot be removed.
 226   *
 227   * @since 4.5.0
 228   *
 229   * @see WP_Rewrite::remove_permastruct()
 230   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 231   *
 232   * @param string $name Name for permalink structure.
 233   */
 234  function remove_permastruct( $name ) {
 235      global $wp_rewrite;
 236  
 237      $wp_rewrite->remove_permastruct( $name );
 238  }
 239  
 240  /**
 241   * Adds a new feed type like /atom1/.
 242   *
 243   * @since 2.1.0
 244   *
 245   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 246   *
 247   * @param string   $feedname Feed name. Should not start with '_'.
 248   * @param callable $callback Callback to run on feed display.
 249   * @return string Feed action name.
 250   *
 251   * @phpstan-param non-empty-string                        $feedname
 252   * @phpstan-param callable(bool, non-empty-string): mixed $callback
 253   * @phpstan-return non-falsy-string
 254   */
 255  function add_feed( $feedname, $callback ) {
 256      global $wp_rewrite;
 257  
 258      if ( ! in_array( $feedname, $wp_rewrite->feeds, true ) ) {
 259          $wp_rewrite->feeds[] = $feedname;
 260      }
 261  
 262      $hook = 'do_feed_' . $feedname;
 263  
 264      // Remove default function hook.
 265      remove_action( $hook, $hook );
 266  
 267      add_action( $hook, $callback, 10, 2 );
 268  
 269      return $hook;
 270  }
 271  
 272  /**
 273   * Removes rewrite rules and then recreate rewrite rules.
 274   *
 275   * @since 3.0.0
 276   *
 277   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 278   *
 279   * @param bool $hard Whether to update .htaccess (hard flush) or just update
 280   *                   rewrite_rules option (soft flush). Default is true (hard).
 281   */
 282  function flush_rewrite_rules( $hard = true ) {
 283      global $wp_rewrite;
 284  
 285      if ( is_callable( array( $wp_rewrite, 'flush_rules' ) ) ) {
 286          $wp_rewrite->flush_rules( $hard );
 287      }
 288  }
 289  
 290  /**
 291   * Clears the rewrite rules when the site language changes.
 292   *
 293   * Rewrite rules can contain translated slugs, such as the default random content
 294   * base, see {@see WP_Rewrite::get_random_base()}. Clearing the rules causes them to be
 295   * regenerated in the new site language.
 296   *
 297   * The rules are regenerated on the next request rather than immediately, because
 298   * the new language is not loaded until after the option is saved. Clearing the
 299   * rules also avoids rewriting the .htaccess file.
 300   *
 301   * Runs on the {@see 'add_option_WPLANG'} and {@see 'update_option_WPLANG'} actions.
 302   *
 303   * @since 7.2.0
 304   */
 305  function wp_flush_rewrite_rules_on_site_language_change(): void {
 306      update_option( 'rewrite_rules', '' );
 307  }
 308  
 309  /**
 310   * Adds an endpoint, like /trackback/.
 311   *
 312   * Adding an endpoint creates extra rewrite rules for each of the matching
 313   * places specified by the provided bitmask. For example:
 314   *
 315   *     add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
 316   *
 317   * will add a new rewrite rule ending with "json(/(.*))?/?$" for every permastruct
 318   * that describes a permalink (post) or page. This is rewritten to "json=$match"
 319   * where $match is the part of the URL matched by the endpoint regex (e.g. "foo" in
 320   * "[permalink]/json/foo/").
 321   *
 322   * A new query var with the same name as the endpoint will also be created.
 323   *
 324   * When specifying $places ensure that you are using the EP_* constants (or a
 325   * combination of them using the bitwise OR operator) as their values are not
 326   * guaranteed to remain static (especially `EP_ALL`).
 327   *
 328   * Be sure to flush the rewrite rules - see flush_rewrite_rules() - when your plugin gets
 329   * activated and deactivated.
 330   *
 331   * @since 2.1.0
 332   * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
 333   *
 334   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 335   *
 336   * @param string      $name      Name of the endpoint.
 337   * @param int         $places    Endpoint mask describing the places the endpoint should be added.
 338   *                               Accepts a mask of:
 339   *                               - `EP_ALL`
 340   *                               - `EP_NONE`
 341   *                               - `EP_ALL_ARCHIVES`
 342   *                               - `EP_ATTACHMENT`
 343   *                               - `EP_AUTHORS`
 344   *                               - `EP_CATEGORIES`
 345   *                               - `EP_COMMENTS`
 346   *                               - `EP_DATE`
 347   *                               - `EP_DAY`
 348   *                               - `EP_MONTH`
 349   *                               - `EP_PAGES`
 350   *                               - `EP_PERMALINK`
 351   *                               - `EP_ROOT`
 352   *                               - `EP_SEARCH`
 353   *                               - `EP_TAGS`
 354   *                               - `EP_YEAR`
 355   * @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var
 356   *                               for this endpoint. Defaults to the value of `$name`.
 357   */
 358  function add_rewrite_endpoint( $name, $places, $query_var = true ) {
 359      global $wp_rewrite;
 360      $wp_rewrite->add_endpoint( $name, $places, $query_var );
 361  }
 362  
 363  /**
 364   * Filters the URL base for taxonomies.
 365   *
 366   * To remove any manually prepended /index.php/.
 367   *
 368   * @access private
 369   * @since 2.6.0
 370   *
 371   * @param string $base The taxonomy base that we're going to filter
 372   * @return string
 373   */
 374  function _wp_filter_taxonomy_base( $base ) {
 375      if ( ! empty( $base ) ) {
 376          $base = preg_replace( '|^/index\.php/|', '', $base );
 377          $base = trim( $base, '/' );
 378      }
 379      return $base;
 380  }
 381  
 382  
 383  /**
 384   * Resolves numeric slugs that collide with date permalinks.
 385   *
 386   * Permalinks of posts with numeric slugs can sometimes look to WP_Query::parse_query()
 387   * like a date archive, as when your permalink structure is `/%year%/%postname%/` and
 388   * a post with post_name '05' has the URL `/2015/05/`.
 389   *
 390   * This function detects conflicts of this type and resolves them in favor of the
 391   * post permalink.
 392   *
 393   * Note that, since 4.3.0, wp_unique_post_slug() prevents the creation of post slugs
 394   * that would result in a date archive conflict. The resolution performed in this
 395   * function is primarily for legacy content, as well as cases when the admin has changed
 396   * the site's permalink structure in a way that introduces URL conflicts.
 397   *
 398   * @since 4.3.0
 399   *
 400   * @param array $query_vars Optional. Query variables for setting up the loop, as determined in
 401   *                          WP::parse_request(). Default empty array.
 402   * @return array Returns the original array of query vars, with date/post conflicts resolved.
 403   */
 404  function wp_resolve_numeric_slug_conflicts( $query_vars = array() ) {
 405      if ( ! isset( $query_vars['year'] ) && ! isset( $query_vars['monthnum'] ) && ! isset( $query_vars['day'] ) ) {
 406          return $query_vars;
 407      }
 408  
 409      // Identify the 'postname' position in the permastruct array.
 410      $permastructs   = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
 411      $postname_index = array_search( '%postname%', $permastructs, true );
 412  
 413      if ( false === $postname_index ) {
 414          return $query_vars;
 415      }
 416  
 417      /*
 418       * A numeric slug could be confused with a year, month, or day, depending on position. To account for
 419       * the possibility of post pagination (eg 2015/2 for the second page of a post called '2015'), our
 420       * `is_*` checks are generous: check for year-slug clashes when `is_year` *or* `is_month`, and check
 421       * for month-slug clashes when `is_month` *or* `is_day`.
 422       */
 423      $compare = '';
 424      if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) {
 425          $compare = 'year';
 426      } elseif ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
 427          $compare = 'monthnum';
 428      } elseif ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) {
 429          $compare = 'day';
 430      }
 431  
 432      if ( ! $compare ) {
 433          return $query_vars;
 434      }
 435  
 436      // This is the potentially clashing slug.
 437      $value = '';
 438      if ( array_key_exists( $compare, $query_vars ) ) {
 439          $value = $query_vars[ $compare ];
 440      }
 441  
 442      $post = get_page_by_path( $value, OBJECT, 'post' );
 443      if ( ! ( $post instanceof WP_Post ) ) {
 444          return $query_vars;
 445      }
 446  
 447      // If the date of the post doesn't match the date specified in the URL, resolve to the date archive.
 448      if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) {
 449          // $matches[1] is the year the post was published.
 450          if ( (int) $query_vars['year'] !== (int) $matches[1] ) {
 451              return $query_vars;
 452          }
 453  
 454          // $matches[2] is the month the post was published.
 455          if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && (int) $query_vars['monthnum'] !== (int) $matches[2] ) {
 456              return $query_vars;
 457          }
 458      }
 459  
 460      /*
 461       * If the located post contains nextpage pagination, then the URL chunk following postname may be
 462       * intended as the page number. Verify that it's a valid page before resolving to it.
 463       */
 464      $maybe_page = '';
 465      if ( 'year' === $compare && isset( $query_vars['monthnum'] ) ) {
 466          $maybe_page = $query_vars['monthnum'];
 467      } elseif ( 'monthnum' === $compare && isset( $query_vars['day'] ) ) {
 468          $maybe_page = $query_vars['day'];
 469      }
 470      // Bug found in #11694 - 'page' was returning '/4'.
 471      $maybe_page = (int) trim( $maybe_page, '/' );
 472  
 473      $post_page_count = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
 474  
 475      // If the post doesn't have multiple pages, but a 'page' candidate is found, resolve to the date archive.
 476      if ( 1 === $post_page_count && $maybe_page ) {
 477          return $query_vars;
 478      }
 479  
 480      // If the post has multiple pages and the 'page' number isn't valid, resolve to the date archive.
 481      if ( $post_page_count > 1 && $maybe_page > $post_page_count ) {
 482          return $query_vars;
 483      }
 484  
 485      // If we've gotten to this point, we have a slug/date clash. First, adjust for nextpage.
 486      $query_vars['page'] = $maybe_page;
 487  
 488      // Next, unset autodetected date-related query vars.
 489      unset( $query_vars['year'] );
 490      unset( $query_vars['monthnum'] );
 491      unset( $query_vars['day'] );
 492  
 493      // Then, set the identified post.
 494      $query_vars['name'] = $post->post_name;
 495  
 496      // Finally, return the modified query vars.
 497      return $query_vars;
 498  }
 499  
 500  /**
 501   * Examines a URL and try to determine the post ID it represents.
 502   *
 503   * Checks are supposedly from the hosted site blog.
 504   *
 505   * @since 1.0.0
 506   *
 507   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 508   * @global WP         $wp         Current WordPress environment instance.
 509   *
 510   * @param string $url Permalink to check.
 511   * @return int Post ID, or 0 on failure.
 512   */
 513  function url_to_postid( $url ) {
 514      global $wp_rewrite;
 515  
 516      /**
 517       * Filters the URL to derive the post ID from.
 518       *
 519       * @since 2.2.0
 520       *
 521       * @param string $url The URL to derive the post ID from.
 522       */
 523      $url = apply_filters( 'url_to_postid', $url );
 524  
 525      $url_host = parse_url( $url, PHP_URL_HOST );
 526  
 527      if ( is_string( $url_host ) ) {
 528          // Only a leading 'www.' is optional. Removing it anywhere else would match a different host.
 529          $url_host = preg_replace( '|^www\.|', '', $url_host );
 530      } else {
 531          $url_host = '';
 532      }
 533  
 534      $home_url_host = parse_url( home_url(), PHP_URL_HOST );
 535  
 536      if ( is_string( $home_url_host ) ) {
 537          $home_url_host = preg_replace( '|^www\.|', '', $home_url_host );
 538      } else {
 539          $home_url_host = '';
 540      }
 541  
 542      // Bail early if the URL does not belong to this site.
 543      if ( $url_host && $url_host !== $home_url_host ) {
 544          return 0;
 545      }
 546  
 547      // First, check to see if there is a 'p=N' or 'page_id=N' to match against.
 548      if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) {
 549          $id = absint( $values[2] );
 550          if ( $id ) {
 551              return $id;
 552          }
 553      }
 554  
 555      // Get rid of the #anchor.
 556      $url_split = explode( '#', $url );
 557      $url       = $url_split[0];
 558  
 559      // Get rid of URL ?query=string.
 560      $url_split = explode( '?', $url );
 561      $url       = $url_split[0];
 562  
 563      // Set the correct URL scheme.
 564      $scheme = parse_url( home_url(), PHP_URL_SCHEME );
 565      $url    = set_url_scheme( $url, $scheme );
 566  
 567      // Add 'www.' if it is absent and should be there.
 568      if ( str_contains( home_url(), '://www.' ) && ! str_contains( $url, '://www.' ) ) {
 569          $url = str_replace( '://', '://www.', $url );
 570      }
 571  
 572      // Strip 'www.' if it is present and shouldn't be.
 573      if ( ! str_contains( home_url(), '://www.' ) ) {
 574          $url = str_replace( '://www.', '://', $url );
 575      }
 576  
 577      if ( trim( $url, '/' ) === home_url() && 'page' === get_option( 'show_on_front' ) ) {
 578          $page_on_front = get_option( 'page_on_front' );
 579  
 580          if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
 581              return (int) $page_on_front;
 582          }
 583      }
 584  
 585      // Check to see if we are using rewrite rules.
 586      $rewrite = $wp_rewrite->wp_rewrite_rules();
 587  
 588      // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options.
 589      if ( empty( $rewrite ) ) {
 590          return 0;
 591      }
 592  
 593      // Strip 'index.php/' if we're not using path info permalinks.
 594      if ( ! $wp_rewrite->using_index_permalinks() ) {
 595          $url = str_replace( $wp_rewrite->index . '/', '', $url );
 596      }
 597  
 598      if ( str_contains( trailingslashit( $url ), home_url( '/' ) ) ) {
 599          // Chop off http://domain.com/[path].
 600          $url = str_replace( home_url(), '', $url );
 601      } else {
 602          // Chop off /path/to/blog.
 603          $home_path = parse_url( home_url( '/' ) );
 604          $home_path = $home_path['path'] ?? '';
 605          $url       = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
 606      }
 607  
 608      // Trim leading and lagging slashes.
 609      $url = trim( $url, '/' );
 610  
 611      $request              = $url;
 612      $post_type_query_vars = array();
 613  
 614      foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) {
 615          if ( ! empty( $t->query_var ) ) {
 616              $post_type_query_vars[ $t->query_var ] = $post_type;
 617          }
 618      }
 619  
 620      // Look for matches.
 621      $request_match = $request;
 622      foreach ( (array) $rewrite as $match => $query ) {
 623  
 624          /*
 625           * If the requesting file is the anchor of the match,
 626           * prepend it to the path info.
 627           */
 628          if ( ! empty( $url ) && ( $url !== $request ) && str_starts_with( $match, $url ) ) {
 629              $request_match = $url . '/' . $request;
 630          }
 631  
 632          if ( preg_match( "#^$match#", $request_match, $matches ) ) {
 633  
 634              if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
 635                  // This is a verbose page match, let's check to be sure about it.
 636                  $page = get_page_by_path( $matches[ $varmatch[1] ] );
 637                  if ( ! $page ) {
 638                      continue;
 639                  }
 640  
 641                  $post_status_obj = get_post_status_object( $page->post_status );
 642                  if ( ! $post_status_obj->public && ! $post_status_obj->protected
 643                      && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
 644                      continue;
 645                  }
 646              }
 647  
 648              /*
 649               * Got a match.
 650               * Trim the query of everything up to the '?'.
 651               */
 652              $query = preg_replace( '!^.+\?!', '', $query );
 653  
 654              // Substitute the substring matches into the query.
 655              $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
 656  
 657              // Filter out non-public query vars.
 658              global $wp;
 659              parse_str( $query, $query_vars );
 660              $query = array();
 661              foreach ( (array) $query_vars as $key => $value ) {
 662                  if ( in_array( (string) $key, $wp->public_query_vars, true ) ) {
 663                      $query[ $key ] = $value;
 664                      if ( isset( $post_type_query_vars[ $key ] ) ) {
 665                          $query['post_type'] = $post_type_query_vars[ $key ];
 666                          $query['name']      = $value;
 667                      }
 668                  }
 669              }
 670  
 671              // Resolve conflicts between posts with numeric slugs and date archive queries.
 672              $query = wp_resolve_numeric_slug_conflicts( $query );
 673  
 674              // Do the query.
 675              $query = new WP_Query( $query );
 676              if ( ! empty( $query->posts ) && $query->is_singular ) {
 677                  return $query->post->ID;
 678              } else {
 679                  return 0;
 680              }
 681          }
 682      }
 683      return 0;
 684  }


Generated : Wed Oct 28 08:20:35 2026 Cross-referenced by PHPXref