Home / functions.php / Incrementing number on each post
  • How to Incrementing number on each post

    incrementing-number-on-each-post

    Add the first block of code to your functions.php then add the second block of code to your index.php this will display an incrementing number on each post.

    ( click code to copy )

    wordpress snippet : PHP - functions.php

    <>
    function updateNumbers() {
      global $wpdb;
      $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
      $counts = 0 ;
      if ($pageposts):
        foreach ($pageposts as $post):
          setup_postdata($post);
          $counts++;
          add_post_meta($post->ID, 'incr_number', $counts, true);
          update_post_meta($post->ID, 'incr_number', $counts);
        endforeach;
      endif;
    }
    add_action ( 'publish_post', 'updateNumbers' );
    add_action ( 'deleted_post', 'updateNumbers' );
    add_action ( 'edit_post', 'updateNumbers' );
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <?php
              echo get_post_meta($post->ID,'incr_number',true);
    ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    query, edit_post, publish_post, get_post, add_post_meta, get_post_meta, update_post_meta, add_action, _n, setup_postdata, wp,
  • Tom D.

    Hello ,
    Plugin working ! Thank you very much!
    I have a question, how to make it work with only one type of post format , for example , with only post_format standard?