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

WordPress Trac noreply at wordpress.org
Fri Jul 26 17:01:54 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             |  Resolution:
 Keywords:                     |     Focuses:
-------------------------------+------------------------------

Comment (by rajanit2000):

 {{{
 #!div style="font-size: 80%"
   {{{#!php
         function get_posts( $args = null ) {
                 $defaults = array(
                         'numberposts'      => 5,
                         'category'         => 0,
                         'orderby'          => 'date',
                         'order'            => 'DESC',
                         'include'          => array(),
                         'exclude'          => array(),
                         'meta_key'         => '',
                         'meta_value'       => '',
                         'post_type'        => 'post',
                         'suppress_filters' => true,
                         'unregistered_post'=> false, // New argument to
 include unregistered posts
                 );

                 $parsed_args = wp_parse_args( $args, $defaults );

                 // Handle the unregistered_post argument
                 if ( ! $parsed_args['unregistered_post'] ) {
                         $registered_post_types = get_post_types( array(),
 'names' );
                         if ( ! in_array( $parsed_args['post_type'],
 $registered_post_types ) ) {
                                 // If the post type is unregistered,
 return an empty array
                                 return array();
                         }
                 }

                 if ( empty( $parsed_args['post_status'] ) ) {
                         $parsed_args['post_status'] = ( 'attachment' ===
 $parsed_args['post_type'] ) ? 'inherit' : 'publish';
                 }
                 if ( ! empty( $parsed_args['numberposts'] ) && empty(
 $parsed_args['posts_per_page'] ) ) {
                         $parsed_args['posts_per_page'] =
 $parsed_args['numberposts'];
                 }
                 if ( ! empty( $parsed_args['category'] ) ) {
                         $parsed_args['cat'] = $parsed_args['category'];
                 }
                 if ( ! empty( $parsed_args['include'] ) ) {
                         $incposts                      = wp_parse_id_list(
 $parsed_args['include'] );
                         $parsed_args['posts_per_page'] = count( $incposts
 );  // Only the number of posts included.
                         $parsed_args['post__in']       = $incposts;
                 } elseif ( ! empty( $parsed_args['exclude'] ) ) {
                         $parsed_args['post__not_in'] = wp_parse_id_list(
 $parsed_args['exclude'] );
                 }

                 $parsed_args['ignore_sticky_posts'] = true;
                 $parsed_args['no_found_rows']       = true;

                 $get_posts = new WP_Query();
                 return $get_posts->query( $parsed_args );
         }
 }}}
 }}}

 We can improve this function by adding a new argument to include
 unregistered post types. This will allow developers to include
 unregistered post types in the query. The new argument is called
 `unregistered_post` and is set to `false` by default. If set to `true`,
 the function will return an empty array if the post type is unregistered.

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


More information about the wp-trac mailing list