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 am trying to show a list of custom post types on a page template. When I view the page, instead of seeing the different posts, I see the actual page repeated. For example, If I have 5 posts, the actual page (head content footer) will be display five times. It seems like the query is working...in some way but I am not sure what is wrong. Here is the template code:

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    <div id="post-<?php the_ID(); ?>" <?php post_class('page'); ?>>

        <h1><?php the_title(); ?></h1>

        <article>

            <div class="post-content page-content">
                <?php the_content(); ?>
                <?php wp_link_pages('before=<div class="pagination">&after=</div>'); ?>
            </div><!--.post-content .page-content -->

        </article>

    </div><!--#post-# .post-->

<?php endwhile; ?>

<!-- Start search result code -->

<?php
//query_posts('post_type=listing&orderby=title&order=ASC&posts_per_page=9999');
?>

<?php
$querystr = "
SELECT * 
FROM $wpdb->posts, $wpdb->postsmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'listing_open' 
AND wp_postmeta.meta_value = 'YES'
AND wp_posts.post_status = 'publish' 
AND wp_posts.post_type = 'listing'
ORDER BY post_title
";                              
$pageposts = $wpdb->get_results($querystr, OBJECT);                         
if ($pageposts):                    
foreach ($pageposts as $postdata):
setup_postdata($postdata);
?>

<?php //if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php include_once(get_bloginfo('template_url')."/layout-listings.php?id=".$postdata->ID); ?>       

<?php //endwhile; endif; rewind_posts(); wp_reset_query(); ?>

<?php
endforeach;
endif;
?>

<?php
$querystr = "
SELECT * 
FROM $wpdb->posts, $wpdb->postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'listing_open' 
AND wp_postmeta.meta_value = 'NO'
AND wp_posts.post_status = 'publish' 
AND wp_posts.post_type = 'listing'
ORDER BY post_title
";                              
$pageposts2 = $wpdb->get_results($querystr, OBJECT);                            
if ($pageposts2):                    
foreach ($pageposts2 as $postdata2):
setup_postdata($postdata2);
?>

<?php //if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php include_once(get_bloginfo('template_url')."/layout-listings.php?id=".$postdata2->ID); ?>      

<?php //endwhile; endif; rewind_posts(); wp_reset_query(); ?>

<?php
endforeach;
endif;
?>

<!-- End search result code -->

share|improve this question
add comment

2 Answers

Refer to the Codex page for setup_postdata():

Important: You must make use of the global $post variable to pass the post details into this function, otherwise functions like the_title() don't work properly...

Also note that you can use WP_Query to do these queries, rather than writing raw SQL.

share|improve this answer
add comment

That is a lot of code to digest, but it looks like you are including the entire template with include_once(get_bloginfo('template_url')."/layout-listings.php?id=".$postdata2->ID);. If that is the case, your probably are loading the whole page multiple times, though that include_once should be causing trouble. I am not sure why.

I am pretty sure you should be using get_template_part but it hard to say exactly how without more information.

share|improve this answer
add comment

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.