Last active
November 1, 2017 21:34
-
-
Save manishsongirkar/8188a1b241b4d2adc83d to your computer and use it in GitHub Desktop.
WordPress custom Post Status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add custom post status | |
*/ | |
/** | |
* Register Custom Status | |
* Add custom post status to Product custom post type | |
*/ | |
function rtp_custom_post_status() { | |
register_post_status( | |
'sold-out', array( | |
'label' => _x( 'Sold Out', 'Status General Name', 'fab' ), | |
'label_count' => _n_noop( 'Sold Out (%s)', 'Sold Outs (%s)', 'fab' ), | |
'public' => true, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => true, | |
'exclude_from_search' => false | |
) | |
); | |
} | |
// add_action( 'init', 'rtp_custom_post_status', 0 ); | |
/** | |
* Add Sold Out status to WordPress status dropdown | |
*/ | |
function rtp_append_post_status_list() { | |
global $post; | |
$complete = $label = ''; | |
if( $post->post_type == 'product' ) { | |
if( $post->post_status == 'sold-out' ) { | |
$complete = ' selected=\"selected\"'; | |
$label = '<span id=\"post-status-display\"> Sold Out</span>'; | |
} | |
echo ' | |
<script> | |
jQuery(document).ready(function($){ | |
$("select#post_status").append("<option value=\"sold-out\" ' . $complete . '>Sold Out</option>"); | |
$(".misc-pub-section label").append( "' . $label . '" ); | |
}); | |
</script> | |
'; | |
} | |
} | |
add_action( 'admin_footer-post.php', 'rtp_append_post_status_list' ); | |
/** | |
* Add Sold Out to your custom post status | |
*/ | |
function rtp_display_sold_out_state( $states ) { | |
global $post; | |
$arg = get_query_var( 'post_status' ); | |
if( $arg != 'sold-out' ) { | |
if( $post->post_status == 'sold-out' ) { | |
return array( 'Sold Out' ); | |
} | |
} | |
return $states; | |
} | |
add_filter( 'display_post_states', 'rtp_display_sold_out_state' ); | |
/** | |
* Append Sold Out status to bulk edit and quick edit list | |
*/ | |
function rtp_append_post_status_bulk_edit() { | |
global $post; | |
if( isset( $post->post_type ) && $post->post_type == 'product' ) { | |
echo '<script> | |
jQuery( document ).ready( function( $ ) { | |
$( ".inline-edit-status select " ).append( "<option value=\"sold-out\">Sold Out</option>" ); | |
} ); | |
</script>'; | |
} | |
} | |
add_action( 'admin_footer-edit.php', 'rtp_append_post_status_bulk_edit' ); | |
/** | |
* Exclude Sold Out post from Blog | |
*/ | |
function rtp_exclude_sold_out_post( $query ) { | |
if( $query->is_main_query() && $query->is_archive() ) { | |
$query->set( 'post_status', 'publish' ); | |
} | |
} | |
// add_action( 'pre_get_posts', 'rtp_exclude_sold_out_post' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment