7

how i can count from a custom post type the entries?

<ul class="test">
<?php $args = array( 'post_type' => 'schusslersalz', 'posts_per_page' => 30, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li>';
the_title('<h3>', '</h3>');
the_content();
echo '</li>';
endwhile; ?>
</ul>

how can i count the entries from "schusslersalz" in a number and display it. I have serch and found the funciton:

$count = $loop->post_count;

how i use this?

5 Answers 5

14

The wp_count_posts function has parameter $type for post type to count, you should use this parameter if you want to count the number of schusslersalz

A snippet

$count_posts = wp_count_posts( 'schusslersalz' )->publish;
echo $count_posts;

Full snippet as follow:

$args = array(
  'post_type' => 'schusslersalz'
);
$the_query = new WP_Query( $args );
echo $the_query->found_posts;

Hope I helped

3

Simple way to count total post including pagination post

<?php  global $wp_query
 $count = $wp_query->found_posts;
echo $count; ?>
2

Try this, $loop->found_posts;

0
function wpb_total_posts() { 
    $today = date('Y-m-d');
    $args = array(
        'post_type' => 'grants',
        'meta_query' => array(
            array(
                'key' => 'application_deadline',
                'value' => $today,
                'type' => 'DATE',
                'compare' => '>'
            )),);
    $the_query = new WP_Query( $args );
    return $the_query->found_posts;
}
add_shortcode('total_posts','wpb_total_posts');
0

Try this:

$count_posts = wp_count_posts( 'custom_post_type' )->publish;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.