[wp-trac] [WordPress Trac] #60154: Add filter hook to either get_post_stati or WP_List_Util::filter

WordPress Trac noreply at wordpress.org
Thu Feb 15 06:17:55 UTC 2024


#60154: Add filter hook to either get_post_stati or WP_List_Util::filter
-------------------------+------------------------------
 Reporter:  Malaiac      |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Query        |     Version:  trunk
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+------------------------------

Comment (by oxjhfyoq92):

 To address the issue you've described and allow for filtering of post
 statuses based on custom criteria, such as the absence of a term from a
 taxonomy, we can indeed introduce a filter hook to the `get_post_stati`
 function in WordPress. Here's how we can implement it:

 ```php

 function get_post_stati( $args = array(), $output = 'names', $operator =
 'and' ) {
     global $wp_post_statuses;

     $field = ( 'names' === $output ) ? 'name' : false;


     $statuses = wp_filter_object_list( $wp_post_statuses, $args,
 $operator, $field );

     /**
      * Filters the list of post statuses.
      *
      * @param array  $statuses List of post statuses.
      * @param array  $args     Array of arguments used to retrieve the
 post statuses.
      * @param string $operator The logical operator to use for comparing
 status criteria.
      * @param string $field    The field to retrieve from the post status
 objects ('name' or false).
      */
     return apply_filters( 'get_post_stati', $statuses, $args, $operator,
 $field );
 }
 ```

 With this modification, we've added a filter hook named
 `'get_post_stati'`, which allows developers to customize the list of post
 statuses according to their specific requirements.

 Here's an example of how you can use this filter to remove "dead" statuses
 when the `$_GET['taxonomy']` parameter is set to `-1`:

 ```php

 add_filter( 'get_post_stati', 'custom_get_post_stati', 10, 4 );

 function custom_get_post_stati( $statuses, $args, $operator, $field ) {
     // Check if we're in the admin and the 'taxonomy' parameter is set to
 -1
     if ( is_admin() && isset( $_GET['taxonomy'] ) && $_GET['taxonomy'] ==
 -1 ) {
         // Remove "dead" statuses from the list
         $statuses = array_filter( $statuses, function( $status ) {
             return ! in_array( $status, array( 'wc-refunded', 'wc-failed',
 'wc-cancelled' ) );
         } );
     }

     return $statuses;
 }
 ```

 By adding this filter, you can now customize the list of post statuses
 returned by `get_post_stati` based on any criteria you desire, such as the
 absence of a term from a taxonomy. This provides greater flexibility and
 control over the behavior of post status queries in WordPress.

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


More information about the wp-trac mailing list