The Problem
Add a Post Counter to WordPress Archive Pages
Archive and filtered-results pages are easier to understand when visitors can immediately see how many items match. This shortcode reads the current WordPress query and outputs the result count wherever your template accepts a shortcode.
The Fix
1. Add the PHP shortcode
Add the following with Code Snippets or a child theme. In Code Snippets, omit the opening PHP tag.
function wpbn_post_count_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'label' => '',
'post_type' => '',
),
$atts,
'wpbn_post_count'
);
global $wp_query;
// On an archive/search/filter results page, use the current query count.
if ( ! is_singular() && isset( $wp_query->found_posts ) ) {
$count = (int) $wp_query->found_posts;
} elseif ( ! empty( $atts['post_type'] ) ) {
$post_type = sanitize_key( $atts['post_type'] );
$counts = wp_count_posts( $post_type );
$count = isset( $counts->publish ) ? (int) $counts->publish : 0;
} else {
$count = 0;
}
$label = trim( sanitize_text_field( $atts['label'] ) );
if ( $label !== '' ) {
return sprintf(
'<span class="wpbn-post-count"><strong>%s</strong> %s</span>',
esc_html( number_format_i18n( $count ) ),
esc_html( $label )
);
}
return '<span class="wpbn-post-count"><strong>' .
esc_html( number_format_i18n( $count ) ) .
'</strong></span>';
}
add_shortcode( 'wpbn_post_count', 'wpbn_post_count_shortcode' );
2. Add the shortcode to the archive template
Place [wpbn_post_count] in the archive or results template where you want the number to appear.
3. Add a label when useful
For a friendlier output, add a label:
[wpbn_post_count label="Magazine Covers"]
4. Use post_type outside an archive
If you need a total on a normal page rather than the current archive query, specify a post type:
[wpbn_post_count post_type="magazine_covers" label="Magazine Covers"]
5. Style the count
The output uses the class wpbn-post-count, so you can style the number and label globally without changing the PHP.
6. Test it with filters
Apply your archive/search filters and confirm the count follows the current results. If a filtering plugin replaces the results with AJAX, make sure the shortcode area is part of the content that the plugin refreshes.
Quick Summary
Add the snippet, then place [wpbn_post_count] in the archive template. Use label for descriptive text or post_type for a site-wide published total.
Common Problem
If the number does not change after an AJAX filter, the counter may be outside the results area being refreshed. Move it into the plugin’s refreshed results wrapper or use that plugin’s update hook.

