Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#58516 new defect (bug)

url_to_postid does not return post id for static Posts page

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

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.


3 years ago
#1

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