Hi,
Here’s how you can create a custom post status "Archive" and add the necessary UI elements in the WordPress admin using Gutenberg:
1. Register the Custom Post Status:
You’ve already done this part. Here’s the code you’ve used for reference:
function register_archived_post_status() { register_post_status('archived', array( 'label' => _x('Archived', 'post'), 'public' => true, 'exclude_from_search' => true, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, 'label_count' => _n_noop('Archived <span class="count">(%s)</span>', 'Archived <span class="count">(%s)</span>'), )); } add_action('init', 'register_archived_post_status');
2. Add the Status to the Post Edit Screen:
To make the custom status available in the post edit screen in Gutenberg, you need to add it to the post status dropdown. You can do this by using JavaScript to extend the Gutenberg editor.
Add this JavaScript code to your theme or plugin:
wp.plugins.registerPlugin('custom-archive-status', { render: function() { wp.data.dispatch('core/editor').addEditorPanelStatus('archived', { label: 'Archived', value: 'archived', }); return null; } });
Then enqueue this script in your theme or plugin:
function enqueue_custom_gutenberg_script() { wp_enqueue_script( 'custom-gutenberg-archive-status', get_template_directory_uri() . '/js/custom-gutenberg-archive-status.js', // Adjust the path to your JS file array('wp-plugins', 'wp-edit-post', 'wp-data', 'wp-i18n', 'wp-element', 'wp-components', 'wp-compose') ); } add_action('enqueue_block_editor_assets', 'enqueue_custom_gutenberg_script');
3. Ensure Posts with "Archive" Status are Excluded from Front-End Queries:
To exclude archived posts from the front-end, you need to modify the main query. Add this to your theme’s functions.php
file or a custom plugin:
function exclude_archived_posts( $query ) { if ( ! is_admin() && $query->is_main_query() ) { $query->set( 'post_status', array( 'publish', 'pending', 'draft', 'future', 'private' ) ); } } add_action( 'pre_get_posts', 'exclude_archived_posts' );
Note that this function allows only standard post statuses on the front-end, thereby excluding the "Archived" status.
4. Add Custom Status to Quick Edit and Bulk Edit:
To make the custom status available in Quick Edit and Bulk Edit, add this code to your functions.php
file:
function add_custom_post_status_to_quick_edit() { global $post; if( get_post_type( $post->ID ) == 'post' ) { ?> <script> jQuery(document).ready(function($){ $('.inline-edit-status select').append('<option value="archived"><?php _e('Archived') ?></option>'); }); </script> <?php } } add_action('admin_footer-edit.php', 'add_custom_post_status_to_quick_edit');
5. Update the Post Edit Screen to Include the Custom Status:
Use this code to add your custom post status to the post status dropdown in the post edit screen:
function display_custom_post_status_in_admin( $post_statuses ) { $post_statuses['archived'] = _x( 'Archived', 'post' ); return $post_statuses; } add_filter( 'display_post_states', 'display_custom_post_status_in_admin' );
By following these steps, you should be able to create a custom post status "Archive" and add the necessary UI elements to manage it within the WordPress admin using Gutenberg.
Please login or Register to submit your answer