Nuovo stato dei post per un tipo di post personalizzato
Ho un tipo di post personalizzato recipes
. Sto utilizzando uno script cron per aggregare automaticamente le notizie nel database.
Attualmente viene importato e salvato come 'In attesa di revisione'. È possibile creare un altro stato del post chiamato Aggregated
che elencherà tutte le notizie aggregate da pubblicare?
Ho provato a utilizzare la funzione register_post_status
, tuttavia non sembra funzionare:
function custom_post_status(){
register_post_status( 'aggregated', array(
'label' => _x( 'Aggregato', 'recipes' ),
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Aggregato <span class="count">(%s)</span>', 'Aggregati <span class="count">(%s)</span>' ),
) );
}
add_action( 'init', 'custom_post_status' );
Grazie per l'aiuto.

C'è una fantastica descrizione passo passo su come farlo qui https://www.jclabs.co.uk/create-custom-post-status-in-wordpress-using-register_post_status/
Per aggiungere il tuo stato personalizzato al menu a discesa, aggiungi semplicemente quanto segue al file functions.php del tuo tema:
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>
';
}
}
Con questo avrai il tuo stato personalizzato attivo e funzionante in 5 minuti, mi ha fatto risparmiare un sacco di tempo!

Il link è rotto. Ecco la versione più recente su archive.org.

@rinogo grazie mille, ho sostituito con il link archive.org che hai fornito

Ecco un link aggiornato all'articolo sul nuovo sito di James Collings: https://www.jclabs.co.uk/create-custom-post-status-in-wordpress-using-register_post_status/

Il tuo codice dovrebbe essere corretto e dovrebbe aggiungere lo stato desiderato all'array globale $wp_post_statuses.
Se però ti aspetti che venga visualizzato nel menu a discesa dell'amministrazione, questo è un problema noto: https://core.trac.wordpress.org/ticket/12706

Registra uno stato del post "aggregated" per il tipo di post personalizzato "recipes":
register_post_status( 'aggregated', array(
'label' => _x( 'Aggregated', 'etichetta stato del post', 'plugin-domain' ),
'public' => true,
'label_count' => _n_noop( 'Aggregated <span class="count">(%s)</span>', 'Aggregated <span class="count">(%s)</span>', 'plugin-domain' ),
'post_type' => array( 'recipes' ), // Definisci uno o più tipi di post a cui lo stato può essere applicato.
'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',
) );
Nella schermata di modifica del post personalizzato "recipes", aggiungi lo stato personalizzato nel dropdown del metabox "Pubblica" e modifica l'etichetta del pulsante "Salva Bozza" se lo stato selezionato è "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("Salva Aggregated");
}
var jSelect = $("select#post_status");
$("a.save-post-status").on("click", function(){
if( jSelect.val() == "aggregated" ){
$("input#save-post").val("Salva Aggregated");
}
});
});
SD;
echo '<script type="text/javascript">' . $script . '</script>';
}
});
Aggiungi lo stato del post personalizzato nella schermata di modifica rapida della griglia di amministrazione del post personalizzato:
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>";
}
});
Mostra il totale dello stato del post personalizzato nella griglia di amministrazione del post personalizzato:
add_filter( 'display_post_states', function( $statuses ) {
global $post;
if( $post->post_type == 'recipes') {
if ( get_query_var( 'post_status' ) != 'aggregated' ) { // non per le pagine con tutti i post di questo stato
if ( $post->post_status == 'aggregated' ) {
return array( 'Aggregated' );
}
}
}
return $statuses;
});

Ecco il codice funzionante per WP 6.2 senza utilizzare JQuery
// 1. Registra uno stato personalizzato "aggregato" per i post
add_action('init', 'add_custom_post_status');
function add_custom_post_status()
{
register_post_status('aggregated', array(
'label' => _x('Aggregato', 'ricette'),
'public' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Aggregato<span class="count">(%s)</span>', 'Aggregato <span class="count">(%s)</span>'),
));
}
// 2. Nella schermata di modifica del post: aggiungi lo stato personalizzato al dropdown
add_action('admin_footer-post.php', 'append_post_status_list');
function append_post_status_list()
{
global $post;
if ($post->post_type != 'recipes') {
return;
}
$selected = 'false';
$setStatus = '';
if ($post->post_status == 'aggregated') {
$selected = 'true';
$setStatus = 'document.getElementById("post-status-display").innerHTML = "Aggregato";';
}
echo '
<script>
document.getElementById("post_status").appendChild(new Option("Aggregato", "aggregated", ' . $selected . '));
' . $setStatus . '
</script>
';
}
// 3. Nella schermata di modifica rapida (pagina elenco): aggiungi lo stato personalizzato al dropdown
add_action('admin_footer-edit.php', function () {
global $post;
if ($post->post_type != 'recipes') {
return;
}
echo '
<script>
document.querySelectorAll("select[name=\"_status\"]").forEach((s) => {
s.appendChild(new Option("Aggregato", "aggregated"));
});
</script>';
});
