--- UPDATED ---
Ok now that I understand more about what you're doing (querying for a CPT and displaying it on a Page using a template file), here is how I handle a similar function:
On one of my sites I have a Page that describes hiking in a particular area, below which I query for all custom posts of the CPT "hike" that have a custom field that holds a (future) date for the hike (it's a guided hike that has a scheduled date and a form for folks to signup for the hike).
The Template file for the Hiking Page starts out as previously mentioned with
if (have_posts()) : while (have_posts()) : the_post();
followed by some HTML, within which is
the_content();
in order to display the generic verbiage on the page about hiking in that area.
Below that I use get_posts();
to fetch all of the CPT posts for upcoming hikes, so older (past) hikes are not displayed
global $post;
$args = array (
'post_type' => 'hike',
'meta_key' => 'hike_date_start',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1);
$myposts = get_posts($args);
foreach ( $myposts as $post ) : setup_postdata( $post );
$exp_date = strtotime(get_post_meta($post->ID,'hike_date_start',true));
$today = time();
if ($today <= $exp_date) {
$hikedate = get_post_meta($post->ID,'hike_date_start',true);
$hikedate2 = get_post_meta($post->ID,'hike_date_end',true);
$hikedateend = strtotime(get_post_meta($post->ID,'hike_date_end',true));
$hikerating = get_post_meta($post->ID,'hike_rating',true);
$hikeleader = get_post_meta($post->ID,'hike_leader',true);
$hikecontactph = get_post_meta($post->ID,'hike_contact_phone',true);
$hikecontactem = get_post_meta($post->ID,'hike_contact_email',true);
$hikedirections = get_post_meta($post->ID,'hike_directions',true);
Below that is some more HTML to display the data in the format I want, like so:
<div class="hikeentry">
<div class="time">
<div class="month"><?php echo date('M', $exp_date); ?></div>
<div class="wkday"><?php echo date('l', $exp_date); ?></div>
<div class="day"><?php echo date('d', $exp_date); ?></div>
<div class="year"><?php echo date('Y', $exp_date); ?></div>
</div>
<div class="hikedata">
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<div class="hikemeta">
<div class="left">
<span>Rating:</span> <?php echo $hikerating; ?><br />
<span>Leader:</span> <?php echo $hikeleader; ?><br />
<span>Contact:</span> <a href="mailto:<?php echo antispambot($hikecontactem); ?>"><?php echo antispambot($hikecontactem); ?></a> <?php echo $hikecontactph; ?>
</div>
<div class="left bordered rounded bkgd"><a href="<?php the_permalink() ?>">Click here to read full<br />description and Sign Up</a>
</div>
</div>
<?php the_excerpt(); ?>
</div>
</div><!-- end .hikeentry -->
ALSO when you're done with the foreach loop, it's important to reset the postdata, like so
} endforeach; wp_reset_postdata();
Because I use get_posts($args);
and not wp_query();
then you don't need to reset the query, just the postdata.
I'm not specifically using the_ID();
but you can, wherever you need it WITHIN the foreach loop just call the_ID();
as it's part of each individual CPTs data retrieved with setup_postdata();
I hope this helps, ask more questions in the comments and I'm happy to explain more about how/why I use this......I prefer to use the WP built-in methods for retrieving and using data instead of ACFs but I love ACF for making it easy for my users to enter custom data. :-)
--- prior answer, updated above --
It's difficult to know exactly how to advise you without knowing more about your specific use, but if I understand your question correctly, you're creating a Template file for a specific Page, yes?
In that case in your Pages>Add New where you've added your page and specified your new template to use, you still need at the top of your Template file:
if (have_posts()) : while (have_posts()) : the_post();
then where you need the ID you use the_ID();
I use this to assign the page's ID (which for all intents and purposes in current versions of WP is the same as a 'post' - pretty much everything is a 'post' nowadays) to a specific element on the page.
If you can post more details about what you're trying to accomplish it will help us to give you better suggestions.