[wp-trac] [WordPress Trac] #56991: Update to get_page_by_title in 6.1 changes WHERE clause

WordPress Trac noreply at wordpress.org
Fri Nov 4 04:06:45 UTC 2022


#56991: Update to get_page_by_title in 6.1 changes WHERE clause
--------------------------+---------------------
 Reporter:  Bjorn2404     |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  6.1.1
Component:  General       |     Version:  6.1
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+---------------------
Changes (by peterwilsoncc):

 * milestone:  Awaiting Review => 6.1.1


Comment:

 Comparing the old and the new queries:

 **WordPress 6.0 and earlier**
 {{{#!sql
 SELECT ID
 FROM wp_posts
 WHERE post_title = 'Page with title'
 AND post_type = 'page'
 }}}

 **WordPress 6.1**
 {{{#!sql
 SELECT wp_posts.ID
 FROM wp_posts
 WHERE 1=1
         AND wp_posts.post_title = 'Page with title'
         AND wp_posts.post_type = 'page'
         AND (
                 (
                         wp_posts.post_status = 'publish' OR
                         wp_posts.post_status = 'future' OR
                         wp_posts.post_status = 'draft' OR
                         wp_posts.post_status = 'pending' OR
                         wp_posts.post_status = 'trash' OR
                         wp_posts.post_status = 'auto-draft' OR
                         wp_posts.post_status = 'inherit' OR
                         wp_posts.post_status = 'request-pending' OR
                         wp_posts.post_status = 'request-confirmed' OR
                         wp_posts.post_status = 'request-failed' OR
                         wp_posts.post_status = 'request-completed' OR
                         wp_posts.post_status = 'private'
                 )
         )
 ORDER BY wp_posts.post_date ASC, wp_posts.ID ASC
 LIMIT 0, 1
 }}}

 As `get_page_by_title()` is now a wrapper for `WP_Query`, defining all the
 post statuses was required to match the older query (which did not specify
 post_status, thus querying all of them).

 As the previous query didn't specify an order, MySQL would optimize
 however it wanted to, which means the query is inconsistent depending on
 the engine, version or any number of conditions the optimizer took in to
 consideration. This suggest some amount of hope in the docblock the
 orderby clause was based on:

 > If more than one post uses the same title, the post with the smallest ID
 will be returned. Be careful: in case of more than one post having the
 same title, it will check the oldest publication date, not the smallest
 ID.

 I'm able to reproduce the issue described, I'm just trying to figure out
 the ideal solution.

 Thanks for the ticket, @Bjorn2404, and welcome to trac! I appreciate your
 hard work here and elsewhere trying to figure out the changes between 6.0
 and 6.1.

 I've moved this on to the 6.1.1 milestone for visibility.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56991#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list