The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How to Display Current Posts "Number" (not ID) within it's Custom Post Type

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #263112

    Hey! I’ve been searching high and low for weeks and still haven’t been able to get this to work properly.

    I’m building an archive page template that needs to display the displayed Posts “Number” within it’s Custom Post Type (not the number within the category or term). So, if there are 41 posts in my CPT, then publishing a new Post would make it Post #42. I need to get that number “42” in a variable for use in my template – outside of the loop. I’m displaying these posts with Ajax Load More using their “repeater template” where my PHP to display the post is. But my code isn’t working properly.

    I was able to make it work using the Stack Overflow question below, within the Loop directly on my Archive page template:

    However, I’m now forced to use the Ajax Load More plugin, which is forcing me to use their “repeater templates”, where that snippet no longer works:

    $podcast_number = $wp_query->current_post + 1;

    I’m assuming it’s because they aren’t using $wp_query as the variable to query the posts.

    Another approach I thought about – Since I’m using ACF, I thought I’d create a custom field and populate it via PHP (in functions.php) automatically, but again, that’s not working properly. Here’s my code there:

    function my_acf_load_field( $field ) {
         global $wp_query;
         $podcast_number = $wp_query->current_post + 1;
         $field['value'] = $podcast_number;// Value
         $field['readonly'] = true;
         return $field;
    add_filter('acf/load_field/name=podcast_number', 'my_acf_load_field');

    In my post for that custom field, it just displays “0”. I want it to display “42” (based on my example above for this specific new post). I’m guessing it has something to do with not being in the loop. I also couldn’t get a clear idea of what the Ajax Load More plugin uses as their search variable, since the repeater template should be in the loop, but I’m unsure what to use to hook into it.

    Thank you for any help!


    Thank you for this post it helped me too.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.