Published Item Count Shortcode
Adds a [post_count type="job"] shortcode that prints how many published items a post type has, for lines like "We have 12 open positions".
Opens WPIDE on
Code
add_shortcode( 'post_count', function ( $atts ) {
$atts = shortcode_atts( array( 'type' => 'post' ), $atts, 'post_count' );
$type = sanitize_key( $atts['type'] );
$obj = get_post_type_object( $type );
if ( ! $obj || ! $obj->public ) {
return '';
}
$counts = wp_count_posts( $type );
return esc_html( number_format_i18n( isset( $counts->publish ) ? (int) $counts->publish : 0 ) );
} );
Notes
Uses WordPress's cached counts, so it adds no extra queries on most pages.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.