AnswerBun.com

New post status for custom post type

WordPress Development Asked by dclawson on November 30, 2020

I have a custom post type recipes. I am using a cron script to automatically aggregate news into the database.

It is currently being imported and saved as ‘Pending Review’. Is it possible to create another post status called Aggregated which will list all of the aggregated news to be published?

I tried using the register_post_status function, however this didn’t seem to work:

function custom_post_status(){
    register_post_status( 'aggregated', array(
        'label'                     => _x( 'Aggregated', 'recipes' ),
        'public'                    => false,
        'exclude_from_search'       => true,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Aggregated <span class="count">(%s)</span>', 'Aggregated <span class="count">(%s)</span>' ),
    ) );
}
add_action( 'init', 'custom_post_status' );

Thanks for help with this.

3 Answers

Register a post status "aggregated" for custom post type "recipes" :

register_post_status( 'aggregated', array(
                    'label'                     => _x( 'Aggregated ', 'post status label', 'plugin-domain' ),
                    'public'                    => true,
                    'label_count'               => _n_noop( 'Aggregated s <span class="count">(%s)</span>', 'Aggregated s <span class="count">(%s)</span>', 'plugin-domain' ),
                    'post_type'                 => array( 'recipes' ), // Define one or more post types the status can be applied to.
                    'show_in_admin_all_list'    => true,
                    'show_in_admin_status_list' => true,
                    'show_in_metabox_dropdown'  => true,
                    'show_in_inline_dropdown'   => true,
                    'dashicon'                  => 'dashicons-businessman',
                ) );

In the "recipes" custom post edit screen's publish metabox, adding the custom post status in the dropdown and change the "Save Draft" button label if the selected post status is "aggregated" :

add_action('admin_footer-post.php',function(){

    global $post;
    $complete = '';
    $label = '';

    if($post->post_type == 'recipes') {

        if ( $post->post_status == 'aggregated' ) {
            $complete = ' selected="selected"';
            $label    = 'Aggregated';
        }

        $script = <<<SD

 
       jQuery(document).ready(function($){
           $("select#post_status").append("<option value="aggregated" '.$complete.'>Aggregated</option>");
           
           if( "{$post->post_status}" == "aggregated" ){
                $("span#post-status-display").html("$label");
                $("input#save-post").val("Save Aggregated");
           }
           var jSelect = $("select#post_status");
                
           $("a.save-post-status").on("click", function(){
                
                if( jSelect.val() == "aggregated" ){
                    
                    $("input#save-post").val("Save Aggregated");
                }
           });
      });
     

SD;

        echo '<script type="text/javascript">' . $script . '</script>';
    }

});

Add the custom post status in the quick edit screen of the custom post admin grid :

add_action('admin_footer-edit.php',function() {
    global $post;
    if( $post->post_status == 'recipes' ) {
        echo "<script>
    jQuery(document).ready( function() {
        jQuery( 'select[name="_status"]' ).append( '<option value="aggregated">Aggregated</option>' );
    });
    </script>";
    }
});

Display the custom post status total in the custom post admin grid :

add_filter( 'display_post_states', function( $statuses ) {
    global $post;

    if( $post->post_type == 'recipes') {
        if ( get_query_var( 'post_status' ) != 'aggregated' ) { // not for pages with all posts of this status
            if ( $post->post_status == 'aggregated' ) {
                return array( 'Aggregated' );
            }
        }
    }
    return $statuses;
});

Answered by sudip on November 30, 2020

There is a great Step by Step description on how to do that here http://jamescollings.co.uk/blog/wordpress-create-custom-post-status/

To add your custom post status to the drop-down menue, just add the following to your themes function script:

add_action('admin_footer-post.php', 'jc_append_post_status_list');
function jc_append_post_status_list(){
 global $post;
 $complete = '';
 $label = '';
 if($post->post_type == 'recipes'){
      if($post->post_status == 'aggregated'){
           $complete = ' selected="selected"';
           $label = '<span id="post-status-display"> Aggregated</span>';
      }
      echo '
      <script>
      jQuery(document).ready(function($){
           $("select#post_status").append("<option value="aggregated" '.$complete.'>Aggregated</option>");
           $(".misc-pub-section label").append("'.$label.'");
      });
      </script>
      ';
  }
}

With this you have your custom post status up and running in 5 min, saved me a bunch of time!

Answered by Larzan on November 30, 2020

Your code should be sound, and should add the desired status to the $wp_post_statuses global array.

If you are expecting it to show up in the admin drop-down, however, this is an ongoing issue: https://core.trac.wordpress.org/ticket/12706

Answered by vancoder on November 30, 2020

Add your own answers!

Related Questions

CREATE TABLE with dbDelta does not create table

1  Asked on October 30, 2021 by fcd

     

how to add shortcode widget at end of main blog page

1  Asked on October 30, 2021 by shubhamk

 

Placing background image over section divider

1  Asked on October 30, 2021 by user191925

   

jQuery Click event is not fired at all

1  Asked on October 30, 2021 by islam-hanafi

   

Make the product page as homepage WooCommerce

0  Asked on October 30, 2021 by gabriel-borges

     

How to be sure to get last page version without ANY cache?

2  Asked on October 30, 2021 by j-bizmai

   

How to grab posts in Gutenberg Block?

1  Asked on October 30, 2021 by stephen-sabatini

 

Custom post type permalink returns bad url

1  Asked on October 30, 2021 by ncti

     

Menus don’t appear in admin area / WordPress Multisite

0  Asked on October 30, 2021 by user191642

   

Client side HTTP parameter pollution (reflected)

0  Asked on October 30, 2021 by divided

 

WP Ajax Function Always Returning undefined

1  Asked on October 30, 2021 by brian-bruman

     

Group Posts by Category in Monthly Archive

2  Asked on October 30, 2021 by zero-goki

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir