Your WP_Query loop is incomplete. Your not checking if any posts were actually found before looping, nor are you presenting a message to show none were found, nor are you cleaning up afterwards. You’re also using the ‘other’ syntax that breaks brace matching in IDEs, making your life harder.
Try adding global $post;
like this:
global $post;
$the_query = new WP_Query( array(
'post_type' => 'custompost'
) );
if($the_query->have_posts()){
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo $post->ID;
}
wp_reset_postdata();
}else {
echo 'no posts found';
}
When writing queries it’s important to be consistent and to get things right. So I recommend reading these slides by Andrew Nacin, a WordPress Core developer at Auttomatic:
http://www.slideshare.net/andrewnacin/you-dont-know-query-wordcamp-netherlands-2012
This will tell you where each type of query is appropriate, how they should be used, and why.
In the code above, I added an if statement to check if any posts were returned, and I added wp_reset_postdata which would allow you to continue using the main query by cleaning up after the custom loop.