Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP_Query count posts - custom Post Type

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?

like image 641
Christoph Purin Avatar asked Dec 05 '22 12:12

Christoph Purin


2 Answers

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

like image 183
zipal_ Avatar answered Dec 13 '22 22:12

zipal_


Simple way to count total post including pagination post

<?php  global $wp_query
 $count = $wp_query->found_posts;
echo $count; ?>
like image 21
Suresh Suthar Avatar answered Dec 14 '22 00:12

Suresh Suthar