Ottenere la lista degli slug di tutti i tipi di post registrati

21 nov 2011, 21:18:55
Visualizzazioni: 20.7K
Voti: 6

Vorrei ottenere una lista (array) di tutti i tipi di post che ho registrato.

In particolare, vorrei recuperare i loro slug.

Qualcuno potrebbe aiutarmi? grazie!

0
Tutte le risposte alla domanda 4
1
11

La risposta di @EAMann è corretta, ma WordPress ha già una funzione integrata per recuperare tutti i tipi di post registrati: get_post_types

<?php
// agganciarsi a init in ritardo, così tutto è registrato
// puoi anche usare get_post_types ovunque. Di solito va bene qualsiasi momento dopo init.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init() 
{
    $types = get_post_types( [], 'objects' );
    foreach ( $types as $type ) {
        if ( isset( $type->rewrite->slug ) ) {
            // probabilmente vorrai fare qualcos'altro.
            echo $type->rewrite->slug;
        }
    }
}
21 nov 2011 21:53:22
Commenti

SUGGERIMENTO: Non è necessario chiamare get_post_type_object se imposti la chiamata get_post_types in modo che restituisca gli oggetti direttamente, ad es. $types = get_post_types( '', 'objects' );

t31os t31os
23 nov 2011 11:53:46
1

Il modo più semplice è il seguente utilizzando la funzione WordPress get_post_types();

<?php
$get_cpt_args = array(
    'public'   => true,
    '_builtin' => false
);
$post_types = get_post_types( $get_cpt_args, 'objects' ); // usa 'names' se vuoi ottenere solo il nome del post type.

// visualizza i post type registrati
echo '<pre>';
print_r($post_types);
echo '</pre>';

// fai qualcosa con l'array
if ( $post_types ) {
    foreach ( $post_types as $cpt_key => $cpt_val ) {
       // fai qualcosa.
    }
}
?>
23 gen 2017 08:28:55
Commenti

'object' in $post_types = get_post_types( $get_cpt_args, 'object' ) dovrebbe essere corretto al plurale 'objects': $post_types = get_post_types( $get_cpt_args, 'objects' ) rif: https://developer.wordpress.org/reference/functions/get_post_types/

Pea Pea
25 set 2024 06:27:06
1

Quando chiami register_post_type(), questo aggiunge il tuo nuovo post type a una variabile globale chiamata $wp_post_types. Quindi puoi ottenere un elenco di tutti i tuoi post type da quella variabile:

function get_registered_post_types() {
    global $wp_post_types;

    return array_keys( $wp_post_types );
}

La variabile $wp_post_types è un array che contiene le definizioni dei tuoi CPT, con ogni insieme di argomenti del CPT (etichette, capacità, ecc.) mappato allo slug del CPT. Chiamando array_keys() otterrai un array degli slug dei tuoi CPT.

21 nov 2011 21:38:12
Commenti

se guardi in wp-includes/post.php la funzione integrata get_post_types() restituisce lo stesso array globale `$wp_post_types`. perché creiamo una nuova funzione quando ne abbiamo già una integrata?

Aamer Shahzad Aamer Shahzad
3 nov 2021 09:19:32
0
-1

Una soluzione più elegante:

<?php
$cpt_args = [
    'public'   => true,
    '_builtin' => false
];

$type_slugs = array_map( function( $type ) {
    return $type->slug;
}, get_post_types( $cpt_args, 'objects' ) );
8 giu 2017 12:31:15