Number of view Post in WordPress

I have to show Post Number which is being view by the Visitor
e.g

  Post 4 of 13 [Previous] [Next]

i have counted the total posts in the Category

Read More
$post_cat = get_the_category( $post->ID );
if($post_cat) {

    $post_cat_id = $post_cat[0]->term_id;
    $cat_posts = get_category($post_cat_id);
    $total_posts = $cat_posts->category_count;
}

but problem is i don’t get the post number being view

Related posts

Leave a Reply

2 comments

  1. You need to grab all posts ( with the same orderby parameter as used in your main query ) and locate the correct position.

    $all_posts = get_posts( array(
        'category' => $post_cat_id,
        'posts_per_page' => -1
    ) );
    
    $pos = 0;
    if ( $all_posts ) foreach ( $all_posts as $all_post ) {
        $pos++;
        if ( $all_post->ID == $post->ID ) break;
    }
    echo 'position is: ' . $pos;
    
  2. Here is another version that I used if there are more than one category in a post. This code will try to use the parent category, if not found, it will use the first child category.

    You can also use this with any taxonomy by just setting the taxonomy in the function like this

    echo get_post_number( $taxonomy = 'my_taxonomy' );
    

    Here is the function

    function get_post_number( $taxonomy = 'category' ) {
    
            $query_object   = get_queried_object();
    
            $terms =  wp_get_post_terms( $query_object->ID, $taxonomy ); 
    
            if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    
                foreach ( $terms as $term ) {
                    if( $term->parent === 0 ) {
                        $parent[] = $term;
                    }else{
                        $child[] = $term;
                    }
                }       
    
                if( $parent ) {
                    $term_name = $parent[0]->name;
                    $term_id   = $parent[0]->term_id;
                }else{
                    $term_name = $child[0]->name;
                    $term_id   = $child[0]->term_id;
                } 
    
                $post_args = [ 
                    'post_type'         => $query_object->post_type,
                    'fields'            => 'ids',
                    'posts_per_page'    => -1,
                    'order'             => 'ASC',
                    'tax_query'         => [
                        [
                            'taxonomy'  => $taxonomy,
                            'field'     => 'term_id',
                            'terms'     => $term_id,
                        ],
                    ],
                ];
    
                $q = get_posts( $post_args );
    
                $total_posts = count( $q );
                $post_number = array_search( $query_object->ID, $q ) + 1;
    
                $text = 'This is currently post #' . $post_number . ' of ' . $total_posts . ' posted in ' . $term_name;
    
            }
    
        return $text;
    
    }