rewrite.php 19 KB

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