Search code examples
wordpresscustom-post-type

How to get number of a post over the total numbers of posts within a category in wordpress


I need to show a counter within my single page post (I use it as a shortcode).

I have a custom post type (slug = 'portfolio') that has different categories.

When I am in the single post, I want to display a counter such as "n/tot" where

n = number of post (first post within the category = 1; second post= 2 etc)
tot = total number of posts within that category

I found the way to show the total number of posts, but not within a category

add_shortcode( 'tot_posts', 'number_factsheets' );
function number_factsheets () {

    $total = wp_count_posts('portfolio')->publish;
    $output = '<div class="count">';
    $output .= $total;
    $output .='</div>';
  return $output; 
  
}

Is it possible to obtain the data above when I am in single page post? And how?

WORKING CODE FOR RETRIEVING NUMBER OF SINGLE CUSTOM POST TYPE OVER TOTAL NUMBER OF CPT WITHIN A SPECIFIC CATEGORY

add_shortcode('post-counter', 'get_current_post_num');
function get_current_post_num() {
  $url = $_SERVER['REQUEST_URI'];
  
  $curr_post_id = url_to_postid( "https://sharkrayareas.org".$url ); //Too early for $post object
  $taxonomy = 'portfolio_entries';
  
  $term_objs = get_the_terms( $curr_post_id->ID, $taxonomy );

  foreach ($term_objs as $term_obj)
               $term_ids = $term_obj->term_id; // get the id from the WP_Term object

  
  $args = array(
      'post_type' => 'portfolio',
      'post_status' => 'publish',
      'tax_query' => array(
        array(
          'taxonomy' => 'portfolio_entries',
          'terms'    => $term_ids,
        ),
      ),      
      'order'=> 'ASC',
      'posts_per_page' => -1
  );
  $portfolio_posts = new WP_Query($args);
  
  if ($portfolio_posts->have_posts()):
    echo 'true';
      $total_posts = $portfolio_posts->found_posts;
      
      $num = 1;
     
      while ($portfolio_posts->have_posts()) : $portfolio_posts->the_post();
      $post_id = get_the_ID();

      if ($post_id === $curr_post_id) {
          $output= '<div class="count">'. $num . '/' . $total_posts .'</div>';
      } else {
          $num++;
      }
    endwhile;
  endif;
return $output;
}

Solution

  • I recommend going about that by looping through the posts and checking whether the the page you are on is that point in the loop. I'm including an example that prints it on-page.

    add_action('init', 'get_current_post_num');
    function get_current_post_num() {
        $url = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $curr_post_id = url_to_postid( "https://".$url ); //Too early for $post object so get $curr_post_id from url_to_postid
    
        $args = array(
            'post_type' => 'portfolio',
            'post_status' => 'publish',
            'posts_per_page' => -1
        );
        $portfolio_posts = new WP_Query($args);
        if ($portfolio_posts->have_posts()):
            $total_posts = $portfolio_posts->found_posts;
            $num = 1;
    
            while ($portfolio_posts->have_posts()) : $portfolio_posts->the_post();
                $post_id = get_the_ID();
    
                if ($post_id === $curr_post_id) {
                    echo $num . '/' . $total_posts;
                } else {
                    $num++;
                }
            endwhile;
        endif;
    }