Skip to content

Instantly share code, notes, and snippets.

@topdown
Last active January 31, 2016 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save topdown/9036d462af465d38c29b to your computer and use it in GitHub Desktop.
Save topdown/9036d462af465d38c29b to your computer and use it in GitHub Desktop.
Custom post status workaround
<?php
/**
* Register Custom Status
*
*/
function custom_archived_post_status() {
$args = array(
'label' => _x( 'Archived', 'Status General Name', 'text_domain' ),
'label_count' => _n_noop( 'Archived (%s)', 'Archived (%s)', 'text_domain' ),
'public' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'exclude_from_search' => false,
);
register_post_status( 'archived', $args );
}
add_action( 'init', 'custom_archived_post_status', 0 );
/**
* Inject it into the edit forms
*
*/
function custom_status_into_inline_edit() { // ultra-simple example
echo "<script>
jQuery(document).ready( function() {
// quick edit
jQuery( 'select[name="_status"]' ).append( '<option value="archived">Archived</option>' );
// post page
jQuery( 'select[name="post_status"]' ).append( '<option value="archived">Archived</option>' );
});
</script>";
}
add_action( 'admin_footer-edit.php', 'custom_status_into_inline_edit' );
add_action( 'admin_footer-post.php', 'custom_status_into_inline_edit' );
/**
* Display the status in the dropdown
*/
function custom_display_status_label( $statuses ) {
global $post; // we need it to check current post status
if ( get_query_var( 'post_status' ) != 'archived' ) { // not for pages with all posts of this status
if ( $post->post_status == 'archived' ) {
return array( 'Archived' ); // returning our status label
}
}
return $statuses; // returning the array with default statuses
}
add_filter( 'display_post_states', 'custom_display_status_label' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment