By samjco


2019-03-09 04:17:58 8 Comments

I have been searching high and low for a way to get ALL statuses, the built-ins, and the customs registered. But i have only been able to retrieve a few of other registered statuses of an installed plugin and the wp builtins. Cannot find the other create statuses.

Am I missing something??

My code is as follows:

global $wp_post_statuses;

// var_dump($wp_post_statuses);

foreach($wp_post_statuses as $status) {
   echo $status->name ." - ". $status->label;
}

1 comments

@userabuser 2019-03-09 09:51:46

This largely depends on where within the boot/request sequence you decide to make an attempt at retrieving statuses and it also depends on whether someone, thing, plugin or theme is doing something, funky, or not but the latter is less likely the case.

Useful API functions:

get_post_statuses docs | source

  • Retrieves statuses for the built in post type of post get_available_post_statuses

get_available_post_statuses docs | source

  • Retrieves statuses for the specific post type, but defaults to post if no value is supplied.

get_post_stati docs| source | RECOMMENDED

  • Retrieve statuses, which by default will retrieve all, including built-ins. This function wraps around global $wp_post_statuses; therefore it is recommended that you use that function in place of a direct call to $wp_post_statuses.

If you are calling the global $wp_post_statuses, pay attention to where you are calling it and when within a action or filter, whether the hook fires before or after the point where a statuses is registered and or if within the same hook, whether your priority is before or after registration.

Imagine the following:

Register a type:

add_action('init', function () {
    register_post_status('custom_status', array(
        'label'  => _x('My Status', 'post'),
        'public' => true,
        // shortened for brevity
    ));
}, 20);

Retrieve types:

add_action('init', function () {
    global $wp_post_statuses;
    var_dump( $wp_post_statuses );

    # result... no 'custom_status'
    /*
    array(
        "publish",
        "future",
        "draft",
        "pending",
        "private",
        "trash",
        "auto-draft",
        "inherit",
    )
     */


}, 10);

Retrieve types:

specifying a priority equal to or later than the priority used to register the status:

add_action('init', function () {
    global $wp_post_statuses;
    var_dump( $wp_post_statuses );

    # result... with 'custom_status'
    /*
    array(
        "publish",
        "future",
        "draft",
        "pending",
        "private",
        "trash",
        "auto-draft",
        "inherit",
        "custom_status" // <-- here we go
    )
     */


}, PHP_INT_MAX); // <-- high priority

Alternatively if you can use a different hook, one that fires later, for example if someone is registering a status on init and you could alternatively hook into wp_loaded to run your logic which at that point would contain the statuses you need and so on.

Find where the status is being registered and go from there. If you are using a good IDE or similar, you can search the entire code base, both core and vendor (plugins, themes) to find the particular statuses you are trying to track down quite easily. For example PHPStorm has an excellent Find In Path tool to help you with just that. Most other tools would have something similar.

Some helpful material on the loading sequence of WordPress:

Is there a flowchart for WordPress loading sequence?

How to get WordPress' hooks/actions run sequence?

@samjco 2019-03-11 07:28:38

Very detailed and precise :)

@userabuser 2019-03-11 09:00:02

I've been burnt by stuff like this many a time and I've been developing on WP for at least 10 years. When something isn't showing in a hook and you can confirm it is registered elsewhere, then it's almost always a hook precedence issue. Glad you got it sorted. Enjoy.

@samjco 2019-03-12 17:16:30

Yea I agree. The timing or priority did help PHP_INT_MAX but ultimately it was the plugin developer choice that use a function to disable the statuses a user creates... I've found this piece of code in their plugin: php global $pagenow; // Disable our custom statuses Only allow deregistering on 'edit.php', 'post.php' and post-new.php if ( ! in_array($pagenow, ['edit.php', 'post.php', 'post-new.php'])) { return false; } Now this drove me crazy... And I had to hunt it down in their code to remove it.

@userabuser 2019-03-12 18:14:24

@samjco that's interesting... Only declaring statuses for use within admin context? What plugin was it... open source or commercial or internal?

@samjco 2019-03-12 18:48:30

it was the PublishPress plugin. I am writing an addon field for ACF and needed to show all statuses.

@userabuser 2019-03-12 19:03:53

@samjco ok I see in method disable_custom_statuses_for_post_type. They should provide a filter to allow others to hook in, but maybe there is some reason I don't know about. Modifying the global $pagenow would be a nightmare.

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] How to Get All Posts with any post status?

1 Answered Questions

Creating/editing custom 'post-state'

  • 2018-11-03 01:03:07
  • RozzA
  • 232 View
  • 0 Score
  • 1 Answer
  • Tags:   post-status

1 Answered Questions

Modify loop to include all post statuses not just 'published'

2 Answered Questions

[SOLVED] Removing all post statuses from a Custom Post Type

1 Answered Questions

[SOLVED] How to Get All Posts but the Private ones?

Sponsored Content