0

I have a post type that is essentially a photography zine. I want each post to display in the archive page as an "issue" with a number associated, from oldest to newest. The first issue would be "issue 1" while the tenth issue would be "issue 10". I need this display both on the archive page and also on the post page itself.

Here is the code I was using

<?php 
  echo $wp_query->found_posts - $wp_query->current_post ;

But this only seems to work on the archive page.

2
  • Is there any code, that you could share, showing what you've tried yourself this far? E.g. your post type archive template or single template? Apr 14, 2019 at 12:23
  • whoops, yes. I edited my original question to include a snippet. Apr 14, 2019 at 14:50

2 Answers 2

0

You can do it like this on single post page following this answer. The code may need some tweaking to meet your exact requirements.

Put this in functions.php of your theme

 class MY_Post_Numbers {

  private $count = 0;
  private $posts = array();

  public function display_count() {
      $this->init(); // prevent unnecessary queries
      $id = get_the_ID();
      echo sprintf( '<div class="post-counter">Post number<span class="num">%s</span><span class="slash">/</span><span class="total">%s</span></div>', $this->posts[$id], $this->count );
  }

  private function init() {
      if ( $this->count )
          return;
      global $wpdb;       
      $posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date " ); // can add or change order if you want 
      $this->count = count($posts);

      foreach ( $posts as $key => $value ) {
          $this->posts[$value] = $key + 1;
      }
      unset($posts);
  }

}

$GLOBALS['my_post_numbers'] = new MY_Post_Numbers;

function my_post_number() {
  $GLOBALS['my_post_numbers']->display_count();
}

to use in template file :

<?php my_post_number(); ?>
2
  • Hey this worked! Had to alter the post type and got rid of some of the text that got printed but it works. Thank you 🙏! Apr 14, 2019 at 16:32
  • Glade to know that! stay blessed. Apr 14, 2019 at 16:37
1

Using API function is an option, and in my opinion generally preferable. As counting, numbering on the archive page isn't an issue, this concerns how to do it at the view of a single post. This is not ready to use code, just an exemplary outline:

$current_posts_id = get_the_ID();
$wp_query_obj = new WP_Query(
    [
        //other conditions to determine order
        //use the same as for the archive page, otherwise the numbering differs

        //use parameter fields to get an array of
        //keys numerically indexed, with value post id
        'fields' => 'ids'
    ]
);
//get the numerically array of post ids into its own variable
$wp_query_posts_array = $wp_query_obj->posts;
//search array by value, which is the post id, and return corresponding key, numeric index
$wp_query_posts_array_index = array_search( $current_posts_id, $wp_query_posts_array );
//the array index starts with 0, add one for the issue numbering
$current_posts_issue_number = $wp_query_posts_array_index + 1;



Note: This is of course, if you want to get the issue number on the fly. Generally speaking, if you want or have to work with the issue number more frequently, or want to do queries where you can use the issue number, I probably would introduce a post meta to store the issue number. But that's just my thought in regard to possibly necessary code, data structure design choices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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