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 some issues adding multiple loops. I have a custom post type (locations) that uses a single page template (single-locations.php). I'm trying to add additional loops to this template that will display (x) number of posts from different categories (regular post categories) depending on what custom post type id a user is on.

The issue that is occurring is that, based on the code below, all the different custom post type id's defined are loading the same 5 posts (shown in the first query) but not from the specific categories defined. Additionally the queries under each custom post type id's are not being honored. I'm not sure if I've got the if/elseif statements messed up or if the main custom post type loop is interfering.

Any advice on how to get this working would be great. :)

<div id="content" class="col-full">

            <section id="secondary-content" class="col-left">

                <?php 

                    global $post;
                    $meta_history = get_post_meta($post->ID, 'bb-history', true );
                    $meta_message = get_post_meta($post->ID, 'bb-mayors-message', true );
                    $meta_news = get_post_meta($post->ID, 'bb-current-news', true );

                ?>

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

                <?php echo $meta_history; ?>

                <h1>Mayor's Message</h1>

                <?php echo $meta_message; ?>

                <h1>Current News</h1>

                <?php echo $meta_news; ?>

                <?php

                // get_post_type (44)
                if ( 'locations' == get_post_type( 44 ) ){

                    // Display 5 Posts From Category 5
                    $args = array(

                        'posts_per_page'  => 5,
                        'category'        => '5',
                        'orderby'         => 'post_date',
                        'order'           => 'DESC');

                    $loc1 = new WP_Query($args);

                    while ( $loc1->have_posts() ) : $loc1->the_post(); global $post;

                        ?>

                       <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

                        <?php
                        the_excerpt(); 

                    endwhile; wp_reset_postdata();


                // get_post_type (46)
                } else if ( 'locations' == get_post_type( 46 ) ) {

                    // Display 3 Posts From Category 10
                    $args = array(

                        'posts_per_page'  => 3,
                        'category'        => '10',
                        'orderby'         => 'post_date',
                        'order'           => 'DESC');

                    $loc2 = new WP_Query($args);

                    while ( $loc2->have_posts() ) : $loc2->the_post(); global $post;

                        ?>

                       <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

                        <?php
                        the_excerpt(); 

                    endwhile; wp_reset_postdata();


                // get_post_type (52)
                } else if ( 'locations' == get_post_type( 52 ) ) {

                    // Display 3 Posts From Category 7
                    $args = array(

                        'posts_per_page'  => 3,
                        'category'        => '7',
                        'orderby'         => 'post_date',
                        'order'           => 'DESC');

                    $loc3 = new WP_Query($args);

                    while ( $loc3->have_posts() ) : $loc3->the_post(); global $post;

                        ?>

                       <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

                        <?php
                        the_excerpt(); 

                    endwhile; wp_reset_postdata();


                // get_post_type (55)
                } else if ( 'locations' == get_post_type( 55 ) ) {

                    // Display 3 Posts From Category
                    $args = array(

                        'posts_per_page'  => 3,
                        'category'        => '8',
                        'orderby'         => 'post_date',
                        'order'           => 'DESC');

                    $loc4 = new WP_Query($args);

                    while ( $loc4->have_posts() ) : $loc4->the_post(); global $post;

                        ?>

                       <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

                        <?php
                        the_excerpt(); 

                    endwhile; wp_reset_postdata();

                }

                // get_post_type (57)
                else if ( 'locations' == get_post_type( 57 ) ) {

                    // Display 3 Posts From Category 9
                    $args = array(

                        'posts_per_page'  => 3,
                        'category'        => '9',
                        'orderby'         => 'post_date',
                        'order'           => 'DESC');

                    $loc5 = new WP_Query($args);

                    while ( $loc5->have_posts() ) : $loc5->the_post(); global $post;

                        ?>

                       <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

                        <?php
                        the_excerpt(); 

                    endwhile; wp_reset_postdata();

                }

                ?>



            </section>

            <section id="primary-content" class="col-left">

                <!-- Front Page Articles -->

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

                <article <?php post_class(); ?> role="article">

                    <section class="entry fix">

                        <?php the_content(); ?>

                        <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:' ), 'after' => '</div>' ) ); ?>

                    </section>


                </article><!-- /.post -->

                <?php endwhile; //End WhILE Loop ?>

                <?php else : ?>

                <article <?php post_class(); ?>>
                    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
                </article><!-- .post -->

                <?php endif; //End IF Statement ?>

            </section>
            <!-- End #main -->

            <?php get_sidebar(); ?>

        </div>
        <!-- End #content -->
share|improve this question
    
You'll likely need to move the reset after each loop. Try adding this: <?php wp_reset_postdata(); ?> after closing out the <?php endwhile; ?>. I'm not sure if it makes a difference, but that's how mine is setup and seems to work. –  NW Tech Apr 26 '13 at 21:55
2  
Why would any condition other than the first one ever be met? Assuming that post ID 44 belongs to the locations CPT, then that initial if will always run, and none of the elses below it will ever run. I'm not sure what this structure is meant to achieve? –  vancoder Apr 26 '13 at 21:59
    
@vancoder Essentially I was trying to make it work so that if a post that belonged to a custom post type had an id of 44 then it would load x number of posts from category a, if a post that belonged to the same post type but had an id of 45 then load x amount of posts from category b; and so on and so forth. Hence my attempted use at if/elseif statements. @ NWTech I think the only place that I don't have a wp_reset_postdata() call is after the main loop; I believe I tried adding it there originally and it didn't make a difference but I'll try it again. –  Syrehn Apr 27 '13 at 9:01
add comment

1 Answer

up vote 1 down vote accepted

It sounds like what you are trying to do is make a kind-of "related posts" feature for your "Locations" single post display based on the category.

You don't need to be checking to see if the post is in the "Locations" post type. You are using single-locations.php unless you site is broken only "Locations" posts should show up there. You really only need to check the ID.

// Most of $args is the same every time. 
// no need to repeat it
$args = array(
    'posts_per_page'  => 5,
    'orderby'         => 'post_date',
    'order'           => 'DESC'
);

if ( 44 == $post->ID ){
    $args['cat'] = 5; // Note: the parameter is 'cat' not category    
} elseif (46 == $post->ID ) {
    $args['cat'] = 10; // Note: the parameter is 'cat' not category    
} // and so on

$loc = new WP_Query($args);
while ( $loc->have_posts() ) : $loc->the_post(); global $post; ?>
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3><?php
    the_excerpt(); 
endwhile; wp_reset_postdata();

I took the liberty of cleaning up your code.

  1. You don't need to repeat the complete $args array every time since most of it doesn't change.
  2. Note the change of category to cat
  3. You don't need to create a different variable, or a different query, for each category. You can use the same name and one query. Instead, build the query parameter array and put the query itself and the Loop afterwards.

That is a lot of code to read. Apologies if I missed something but I think that should work for you a little better.

share|improve this answer
    
Thanks! That was exactly what I was trying to do and it worked perfectly. Thank you for providing the modified code structure. –  Syrehn Apr 30 '13 at 18:15
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.