Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having an issue with a loop pulling every custom post in my template and not the one that has been requested. This can be seen here: http://i.imgur.com/1YKeLtV.png

Here is the code I'm using to display the custom post:

<?php /*Template Name: Game Listing Page */ ?>
<?php get_header(); ?>

<div id="content">
    <?php
    $mypost = array( 'post_type' => 'games_database', );
    $loop = new WP_Query( $mypost );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">

                <!-- Display featured image in right-aligned floating div -->
                <div style="float: right; margin: 10px">
                    <?php the_post_thumbnail( array( 100, 100 ) ); ?>
                </div>

                <!-- Display game information and review score -->
                <strong>Title: </strong><?php the_title(); ?><br />
                <strong>Platform(s): </strong>
                <?php the_terms( $post->ID, 'games_database_game_platform' ,  ' ' ); ?>
                <br />
                <strong>Genre: </strong>
                <?php the_terms( $post->ID, 'games_database_game_genre' ,  ' ' ); ?>
                <br />
                <strong>Publisher: </strong>
                <?php echo esc_html( get_post_meta( get_the_ID(), 'game_publisher', true ) ); ?>
                <br />
                <strong>Developer: </strong>
                <?php echo esc_html( get_post_meta( get_the_ID(), 'game_developer', true ) ); ?>
                <br />
                <strong>Review Score: </strong>
                <?php echo esc_html( get_post_meta( get_the_ID(), 'game_rating', true ) ); ?>
                <br />

            </header>

            <!-- Display movie review contents -->
            <div class="entry-content"><?php the_content(); ?></div>
        </article>

    <?php endwhile; ?>
    </div>
</div>
<?php wp_reset_query(); ?>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

I have no idea where I have gone wrong :( Any help would be hugely appreciated.

Cheers.

share|improve this question
    
I must add this is in single-games_database.php –  Dave Burns Aug 12 '13 at 12:34

2 Answers 2

up vote 2 down vote accepted

You are within the loop already. You don't need to recreate it (by making another WP_Query). Here is your code, with the $loop you created removed. This should work perfectly.

<?php /*Template Name: Game Listing Page */ ?>
<?php get_header(); ?>

<div id="content">
    <?php while ( have_posts() ) : the_post();?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">

                <!-- Display featured image in right-aligned floating div -->
                <div style="float: right; margin: 10px">
                    <?php the_post_thumbnail( array( 100, 100 ) ); ?>
                </div>

                <!-- Display game information and review score -->
                <strong>Title: </strong><?php the_title(); ?><br />
                <strong>Platform(s): </strong>
                <?php the_terms( $post->ID, 'games_database_game_platform' ,  ' ' ); ?>
                <br />
                <strong>Genre: </strong>
                <?php the_terms( $post->ID, 'games_database_game_genre' ,  ' ' ); ?>
                <br />
                <strong>Publisher: </strong>
                <?php echo esc_html( get_post_meta( get_the_ID(), 'game_publisher', true ) ); ?>
                <br />
                <strong>Developer: </strong>
                <?php echo esc_html( get_post_meta( get_the_ID(), 'game_developer', true ) ); ?>
                <br />
                <strong>Review Score: </strong>
                <?php echo esc_html( get_post_meta( get_the_ID(), 'game_rating', true ) ); ?>
                <br />

            </header>

            <!-- Display movie review contents -->
            <div class="entry-content"><?php the_content(); ?></div>
        </article>

    <?php endwhile; ?>
    </div>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>
share|improve this answer

I'm having an issue with a loop pulling every custom post in my template and not the one that has been requested.

Your query is requesting _every post of the games_database type.

$mypost = array( 'post_type' => 'games_database', );

There is no argument that would restrict that to a single post. You've literally asked for all of them.

However, take a good look at the Template Hierarchy. If this is in single-games_database.php you don't need the query at all. The data you need is in the main query. All you need is the Loop, this part:

if (have_posts()) {
  while (have_posts()) {
    the_post();
    // echo post data
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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