I'm trying to loop over my custom post type 'services' in a custom archive-services.php file. My loop is:

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <?php the_title(); ?>
        <?php echo $post->ID; ?>
    <?php endwhile; ?>
<?php endif; ?>

The first title will return the correct title; but the ID being returned is 'cpt_services,' which is not the ID I need to get the correct feature image.

Any help would be greatly appreciated, thanks.

  • What if you echo get_the_ID() instead of $post->ID – czerspalace Dec 7 '16 at 1:19
  • I tried that to no avail but did realize how to get it to work by creating a custom query. Not sure if there's a way to do this without, though. – cstls Dec 7 '16 at 1:27

Solution in my case:

I created a custom query and prepended that query to the loop. E.g.:

$args = array(
     'post_type' => 'services'

$services_query = new WP_Query( $args );

        <?php if ( $services_query->have_posts() ) : ?>
            <?php while ( $services_query->have_posts() ) : ?>

This ended up returning me the ID that I needed to get my first feature image correctly. Unsure if there is a better way to approach this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.