Skip to content

Instantly share code, notes, and snippets.

@rohankhudedev
Last active June 18, 2022 17:21
Show Gist options
  • Save rohankhudedev/a10e77646e5ea2790440f62e78a0cc9a to your computer and use it in GitHub Desktop.
Save rohankhudedev/a10e77646e5ea2790440f62e78a0cc9a to your computer and use it in GitHub Desktop.
Wordpress Custom Post Status/State - Archive
<?php
// Tested in WP Version - 5.9.3
// This file is part of functions.php
// Manipulate robots based on post status.
add_filter( 'wpseo_robots', 'yoast_seo_update_archive_robots_meta' );
function yoast_seo_update_archive_robots_meta( $robots ) {
if ( get_post_status() == 'archived') {
return 'noindex,nofollow';
} else {
return $robots;
}
}
// Display post status in title
add_filter('display_post_states', 'archive_title', 10, 2);
function archive_title( $states, $post ) {
if('event' == get_post_type($post->ID) && $post->post_status == 'archived')
return ['Archived'];
return $states;
}
function register_archive_post_status(){
register_post_status( 'archived', array(
'label' => _x( 'Archived', 'post' ),
'label_count' => _n_noop( 'Archived <span class="count">(%s)</span>', 'Archived <span class="count">(%s)</span>'),
'public' => true,
'exclude_from_search' => false
));
}
add_action( 'init', 'register_archive_post_status' );
add_action('admin_footer-post.php', 'append_archive_post_status');
function append_archive_post_status() {
global $post;
$complete = '';
$label = '';
if($post->post_type == 'event'){
if($post->post_status == 'archived'){
$complete = ' selected="selected"';
$label = '<span id="post-status-display"> Archived</span>';
}
echo '<script>jQuery(document).ready(function($){
$("select#post_status").append("<option value=\"archived\" '.$complete.'>Archived</option>");
$(".misc-pub-section label").append("'.$label.'");
});</script>';
}
}
add_action('admin_footer-edit.php', 'append_archive_post_status_to_quick_edit');
function append_archive_post_status_to_quick_edit() {
echo '<script>jQuery(document).ready( function() {
jQuery( "select[name=\"_status\"]" ).append( "<option value=\"archived\">Archived</option>" );
});</script>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment