3

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;
}
  • 2
    What are "the other create statuses"? Where are you running this code? Are you sure you're running it after they're registered? – Jacob Peattie Mar 9 '19 at 4:25
  • That is what I not sure of. I have a plugin installed that adds some statuses, and they do show up...however, my custom registered statuses do not. Is there something in the args to make them work like the defaults/builtins? – samjco Mar 9 '19 at 4:36
  • How are you registering your custom statuses? What's the code? – Jacob Peattie Mar 9 '19 at 4:41
  • register_post_status( $status->label, [ 'label' => $status->name, 'protected' => true, '_builtin' => false, 'label_count' => _n_noop("{$status->name} <span class='count'>(%s)</span>", "{$status->name} <span class='count'>(%s)</span>"), ] ); – samjco Mar 9 '19 at 4:50
  • This may help. – Qaisar Feroz Mar 9 '19 at 5:31
5

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?

|improve this answer|||||
  • 1
    Very detailed and precise :) – samjco Mar 11 '19 at 7:28
  • 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. – Adam Mar 11 '19 at 9:00
  • 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. – samjco Mar 12 '19 at 17:16
  • @samjco that's interesting... Only declaring statuses for use within admin context? What plugin was it... open source or commercial or internal? – Adam Mar 12 '19 at 18:14
  • 1
    @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. – Adam Mar 12 '19 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.