# 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".

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: shortcode, counts, custom-post-types
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/post-count-shortcode/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Published Item Count Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
