How to count Number of posts from wordpress post list

There are various way to get the total number of posts from wordpress post list

i will show you two method:


 
First:
 
 
<?php
            $args = array(
                'post_type'  => 'apps',
                'post_status' => 'publish',
                'tag' => $tagname,
                'posts_per_page' => 18,
            );
            $query = new WP_Query($args);
            // echo "<pre>";
            // var_dump($query);
            $posts = $query->get_posts();
            $i = 0;
            foreach ($posts as $post) :
                $i++;
            ?>
                <div class="flex-item">
                    <div class="item-card">
                        <div class="user-top">
                            <span class="img-span"><a href="<?php echo get_permalink($post?>"><img class="item-card-img" width="42" height="42" src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'thumbnail'); ?>"></a></span>

                            <div class="item-card-body">
                                <h3 class="user-title"><a href="<?php echo get_permalink($post?>"><?php echo $post->post_title?></a></h3>
                                <p>
                                    <?php $excerpt =  get_the_excerpt($post->ID);
                                    if ($excerpt) {
                                        echo get_the_excerpt($post->ID);
                                    } ?>
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach?>
 
echo $i; // this is the total number of post

 Second:
 
 You can also load it with like below default wp code
 
$args = array(
                'post_type'  => 'apps',
                'post_status' => 'publish',
                'tag' => $tagname,
                'posts_per_page' => 18,
            );
            $query = new WP_Query($args);
 
echo $query->post_count
 



 

Post a Comment

Previous Post Next Post