[wp-trac] [WordPress Trac] #48903: Drafts or pending posts overwrites existing published post with same url

WordPress Trac noreply at wordpress.org
Mon Dec 9 12:25:58 UTC 2019


#48903: Drafts or pending posts overwrites existing published post with same url
--------------------------+------------------------------
 Reporter:  uatania       |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Permalinks    |     Version:  5.3
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------------------

Comment (by uatania):

 Waiting for a fix I added the code below for filter the posts returned by
 the main query and giving priority to the published (or older) one:

 {{{#!php
 <?php

 add_filter('posts_results', 'fixIfSingleHasMultiplePosts', 10, 2);
 function fixIfSingleHasMultiplePosts($posts, $wp_query)
   {
     // check if main query in single page
     if(!$wp_query->is_main_query() || !$wp_query->is_single()) {
       return $posts;
     }

     // check bug
     $n_posts = count($posts);
     if($n_posts < 2) {
       return $posts;
     }

     $i = 1;
     while ($i < $n_posts && isset($posts[$i])) {

       if(($posts[$i]->post_status == 'publish' && $posts[($i -
 1)]->post_status != 'publish') || $posts[$i]->post_date < $posts[($i -
 1)]->post_date) {
         unset($posts[($i - 1)]);
       }

       $i++;
     }

     return array_values($posts);
   }
 }}}

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


More information about the wp-trac mailing list