• Hi

    Does anyone know how to sequentially order custom posts by number such as 01, 02, 03, 04 etc and also be able to order these by menu_order?

    I found a great post here:-

    https://www.ads-software.com/support/topic/display-sequential-post-numbercount-not-post-id?replies=10

    and the following looks like it would have worked for posts:-

    function updateNumbers() {
    /* numbering the published posts, starting with 1 for oldest;
    / creates and updates custom field 'incr_number';
    / to show in post (within the loop) use <?php echo get_post_meta($post->ID,'incr_number',true); ?>
    / alchymyth 2010 */
    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):
    $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', 11 );
    add_action ( 'deleted_post', 'updateNumbers' );
    add_action ( 'edit_post', 'updateNumbers' );

    But it doesn’t seem to work for custom posts?

    Can anyone advise / help please?

    Thanks

    Glennyboy

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter glennyboy

    (@glennyboy)

    I worked out my own problem….

    Of course the post type needs to be added in so this line:-

    posts.post_type = ‘custom_post_tpe_name‘ “

    and this line

    foreach ($pageposts as $custom_post_tpe_name):

    seems to work ??

    although if anyone knows how to put a leading 0 to the numbers under 10 that would be amazing….

    Thread Starter glennyboy

    (@glennyboy)

    and add the zeros if less than 10:-

    <?php
       $x = get_post_meta($post->ID,'incr_number',true);;
       if($x > 10){
           echo "$x";
       }else{
           echo "0$x";
       }
    ?>

    I hope others find this useful

    Thread Starter glennyboy

    (@glennyboy)

    sorry ignore the foreach ($pageposts as $custom_post_tpe_name):, just the first line for the post type

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sequential Custom Post Numbering’ is closed to new replies.