WordPress: ottenere il numero totale di un determinato tipo di custom post type

WordPress: ottenere il numero totale di un determinato tipo di custom post type

In WordPress possiamo ottenere il numero totale di un determinato tipo di custom post type.

La soluzione รจ la seguente:


function my_get_cpt_total( $post_type = 'post' ) {
    global $wpdb;
    $total = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = '$post_type' AND post_status = 'publish'" );
    return $total;
}

Torna su