Make WordPress Core

Opened 18 months ago

Last modified 18 months ago

#58516 new defect (bug)

url_to_postid does not return post id for static Posts page

Reported by: nextendweb's profile nextendweb Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Rewrite Rules Keywords: has-patch
Focuses: Cc:

Description

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
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
// 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;
}

Change History (1)

This ticket was mentioned in PR #4600 on WordPress/wordpress-develop by nextend.


18 months ago
#1

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.