Status nou pentru postări de tip custom post type
Am un tip de postare personalizat (custom post type) recipes
. Folosesc un script cron pentru a agrega automat știri în baza de date.
În prezent, acestea sunt importate și salvate ca 'În așteptarea revizuirii' (Pending Review). Este posibil să creez un alt status pentru postări numit Aggregated
care să listeze toate știrile agregate ce urmează să fie publicate?
Am încercat să folosesc funcția register_post_status
, dar nu pare să funcționeze:
function custom_post_status(){
register_post_status( 'aggregated', array(
'label' => _x( 'Agregat', 'recipes' ),
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Agregat <span class="count">(%s)</span>', 'Agregate <span class="count">(%s)</span>' ),
) );
}
add_action( 'init', 'custom_post_status' );
Mulțumesc pentru ajutor.

Există o descriere excelentă pas cu pas despre cum să faci asta aici https://www.jclabs.co.uk/create-custom-post-status-in-wordpress-using-register_post_status/
Pentru a adăuga statusul personalizat al postării în meniul derulant, adaugă următorul cod în scriptul de funcții al temei tale:
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\"> Agregat</span>';
}
echo '
<script>
jQuery(document).ready(function($){
$("select#post_status").append("<option value=\"aggregated\" '.$complete.'>Agregat</option>");
$(".misc-pub-section label").append("'.$label.'");
});
</script>
';
}
}
Cu aceasta, vei avea statusul personalizat al postării funcțional în 5 minute, mi-a economisit o grămadă de timp!

Linkul este rupt. Iată cea mai recentă versiune pe archive.org.

@rinogo mulțumesc frate, am schimbat pe linkul de pe archive.org pe care l-ai furnizat

Iată un link actualizat către articolul de pe noul website al lui James Collings: https://www.jclabs.co.uk/create-custom-post-status-in-wordpress-using-register_post_status/

Codul tău ar trebui să fie corect și să adauge statutul dorit în array-ul global $wp_post_statuses.
Dacă te aștepți să apară în meniul drop-down din administrare, aceasta este o problemă în curs de rezolvare: https://core.trac.wordpress.org/ticket/12706

Înregistrează un status de postare "aggregated" pentru tipul de postare personalizat "recipes":
register_post_status( 'aggregated', array(
'label' => _x( 'Agregat', 'post status label', 'plugin-domain' ),
'public' => true,
'label_count' => _n_noop( 'Agregate <span class="count">(%s)</span>', 'Agregate <span class="count">(%s)</span>', 'plugin-domain' ),
'post_type' => array( 'recipes' ), // Definește unul sau mai multe tipuri de postări cărora li se poate aplica statusul.
'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',
) );
În ecranul de editare al postării personalizate "recipes", adaugă statusul personalizat în dropdown-ul din metabox-ul de publicare și schimbă eticheta butonului "Salvează Draft" dacă statusul selectat este "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 = 'Agregat';
}
$script = <<<SD
jQuery(document).ready(function($){
$("select#post_status").append("<option value=\"aggregated\" '.$complete.'>Agregat</option>");
if( "{$post->post_status}" == "aggregated" ){
$("span#post-status-display").html("$label");
$("input#save-post").val("Salvează Agregat");
}
var jSelect = $("select#post_status");
$("a.save-post-status").on("click", function(){
if( jSelect.val() == "aggregated" ){
$("input#save-post").val("Salvează Agregat");
}
});
});
SD;
echo '<script type="text/javascript">' . $script . '</script>';
}
});
Adaugă statusul personalizat în ecranul de editare rapidă din grila de administrare a postărilor personalizate:
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\">Agregat</option>' );
});
</script>";
}
});
Afișează totalul postărilor cu statusul personalizat în grila de administrare a postărilor personalizate:
add_filter( 'display_post_states', function( $statuses ) {
global $post;
if( $post->post_type == 'recipes') {
if ( get_query_var( 'post_status' ) != 'aggregated' ) { // nu pentru paginile cu toate postările acestui status
if ( $post->post_status == 'aggregated' ) {
return array( 'Agregat' );
}
}
}
return $statuses;
});

Iată codul funcțional pentru WordPress 6.2 fără a folosi jQuery
// 1. Înregistrează un statut de postare "agregat"
add_action('init', 'add_custom_post_status');
function add_custom_post_status()
{
register_post_status('aggregated', array(
'label' => _x('Agregat', 'post status', 'recipes'),
'public' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Agregat <span class="count">(%s)</span>', 'Agregate <span class="count">(%s)</span>'),
));
}
// 2. În ecranul de editare a postării: adaugă statutul personalizat în meniul 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 = "Agregat";';
}
echo '
<script>
document.getElementById("post_status").appendChild(new Option("Agregat", "aggregated", ' . $selected . '));
' . $setStatus . '
</script>
';
}
// 3. În ecranul de editare rapidă (pagina de listare): adaugă statutul personalizat în meniul 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("Agregat", "aggregated"));
});
</script>';
});
