[wp-trac] [WordPress Trac] #61773: get_posts() returns posts of removed custom post type

WordPress Trac noreply at wordpress.org
Fri Jul 26 16:53:40 UTC 2024


#61773: get_posts() returns posts of removed custom post type
-------------------------------+-----------------------------
 Reporter:  rajanit2000        |      Owner:  (none)
     Type:  enhancement        |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 I’ve encountered an issue with the get_posts() function related to custom
 post types. Here’s the detailed process to reproduce the problem:

         * Register a Custom Post Type:
             1. Register a custom post type, for example, movie.
             2. Use the register_post_type() function to create the custom
 post type.
         * Add Posts:
             1. Add several posts to the movie custom post type.
             2. Ensure these posts are correctly saved and retrievable
 using get_posts().
         * Remove the Custom Post Type:
             1. Remove or unregister the movie custom post type.
             2. This can be done by not calling the register_post_type()
 function for movie in your theme or plugin.
         * Retrieve Posts:
             1. Use the get_posts() function to retrieve posts.
             2. Notice that posts belonging to the movie custom post type
 are still being returned by get_posts().

 === Expected Behavior:
 After the custom post type has been removed/unregistered, the get_posts()
 function should not return posts associated with the removed custom post
 type. These posts should be excluded from the query results, as their post
 type is no longer registered.

 === Actual Behavior:
 The get_posts() function continues to return posts of the removed custom
 post type. This can lead to unexpected behavior and confusion, especially
 in dynamic environments where custom post types may be added and removed
 frequently.

 === Proposed Solution:
 Introduce a new argument in the get_posts() function to control whether to
 include posts from unregistered post types. For example:

 {{{
 #!div style="font-size: 80%"
   {{{#!php
     $args = array(
         'unregistered_post' => true // This will return all posts
 including unregistered posts, default is false
     );

     $latest_posts = get_posts($args);
     }}}
 }}}

 By default, unregistered_post should be false, ensuring that posts from
 unregistered custom post types are excluded from the query results. When
 explicitly set to true, it will include posts from unregistered custom
 post types.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61773>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list