[wp-trac] [WordPress Trac] #58516: url_to_postid does not return post id for static Posts page
WordPress Trac
noreply at wordpress.org
Mon Jun 12 13:29:36 UTC 2023
#58516: url_to_postid does not return post id for static Posts page
---------------------------+-----------------------------
Reporter: nextendweb | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Rewrite Rules | Version:
Severity: normal | Keywords:
Focuses: |
---------------------------+-----------------------------
Steps to reproduce:
- Create a page called "Blog", slug: blog. ID: 10 permalink:
https://a.com/blog/
- Go to WordPress->Settings->Reading-> Your homepage displays -> A static
page -> Posts page -> set the previously created "Blog" page.
- Call ->
{{{#!php
<?php
var_dump(url_to_postid('https://a.com/blog/'));
}}}
**Result:** 0
**Expected result:** 10
The cause is that this query is not for a singular page, so
$query->is_singular will return false.
The following code would solve this issue:
{{{#!php
<?php
// Do the query.
// Do the query.
$query = new \WP_Query($query);
if (!empty($query->posts) && $query->is_singular) {
return $query->post->ID;
} else {
if ($query->get_queried_object() instanceof WP_POST) {
return $query->queried_object_id;
}
return 0;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58516>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list