Skip to content

Instantly share code, notes, and snippets.

@itsmikita
Last active August 29, 2015 14:05
Show Gist options
  • Save itsmikita/11052e165a98e6c992b3 to your computer and use it in GitHub Desktop.
Save itsmikita/11052e165a98e6c992b3 to your computer and use it in GitHub Desktop.
WordPress: Query custom post_status without registering it first
<?php
add_action( 'pre_get_posts', '_custom_post_status_query' );
/**
* As of WordPress API for custom post statuses is still in developement.
* This method is a workaround to allow you to query posts by custom post
* status, without registering it with register_post_status();
*
* @param $query
*/
function _custom_post_status_query( $query ) {
global $wp_post_statuses;
$post_status = $query->get( 'post_status' );
// Didn't really check if this is needed..
if( ! $post_status )
return;
// This is the trick:
// Add $post_status if it doesn't exists
if( 'any' != $post_status && ! in_array( $post_status, array_keys( $wp_post_statuses ) ) ) {
$wp_post_statuses[ $post_status ] = array(
'name' => $post_status,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => true,
'public' => true,
'internal' => true,
'private' => true
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment