get_post_stati()
Gets all existing post statuses as an array of names or an array of objects.
Works based on the global variable $wp_post_statuses, which contains complete information about statuses, in the form of an array of objects.
No Hooks.
Returns
String[]|stdClass[]. an array of names or an array of objects, depending on the $output parameter.
Usage
get_post_stati( $args, $output, $operator );
- $args(string/array)
An array
key=>valuefor filtering statuses. The specified key and value must be present in the retrieved statuses for the status to be included in the list.Default: array()
- $output(string)
Output names or objects.
names- get names.objects- get objects.
Default: 'names'
- $operator(string)
Logical operator for filtering $args.
or― will search for any parameter from those specified in $args in the statuses.and― will search for all specified parameters in the status simultaneously.
Default: 'and'
Examples
#1 Get the names of all kinds of statuses on the blog:
$stati = get_post_stati(); /* $stati = Array ( [publish] => publish [future] => future [draft] => draft [pending] => pending [private] => private [trash] => trash [auto-draft] => auto-draft [inherit] => inherit ) */
#2 Get objects of all kinds of statuses on the blog:
$stati = get_post_stati( [], 'objects' );
$stati will contain:
#3 Get the statuses with the desired parameters (status filter)
In this example, we get the names of the statuses that have the publicly_queryable parameter set, i.e. the statuses that participate in the site search:
$stati = get_post_stati( [ 'publicly_queryable'=>true ], 'names' ); /* $stati = Array( [publish] => publish ) */
#4 An example showing how the parameter $operator works
Let's specify 2 parameters to the array $args. or - will output the statuses that have either the first or the second, and will output the statuses that have both parameters:
$stati = get_post_stati( [ 'show_in_admin_status_list'=>true, 'protected'=>true ], 'names', 'or' ); /* $stati = Array ( [publish] => publish [future] => future [draft] => draft [pending] => pending [private] => private [trash] => trash ) */
and the same with "and":
$stati = get_post_stati( [ 'show_in_admin_status_list'=>true, 'protected'=>true ], 'names', 'and' ); /* $stati = Array ( [future] => future [draft] => draft [pending] => pending ) */
Notes
- See: register_post_status()
- Global. stdClass[]. $wp_post_statuses List of post statuses.
Changelog
| Since 3.0.0 | Introduced. |
get_post_stati() get post stati code WP 6.9
function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
global $wp_post_statuses;
$field = ( 'names' === $output ) ? 'name' : false;
return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field );
}