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 have the following code inside my single.php page:

UPDATED CODE

 <?php if (have_posts()) : the_post();
     $post_id = get_the_ID();

 if ( is_single() ) {
         $cats =  get_the_category();
         $cat = $cats[0];
 } else {
         // category archives
         $cat = get_category( get_query_var( 'cat' ) );
 }

 $cat_id = $cat->cat_ID;
 $cat_name = $cat->name;
 $cat_slug = $cat->slug;
?>
<?php query_posts('cat='.$cat_id); ?>
   <?php if (have_posts()) : ?>    
     <div id="sidebar">
       <h3><?php echo get_cat_name($cat_id); $theCount = 0; ?></h3>
         <ul class="info-list">
           <?php while (have_posts()) : the_post(); $theCount += 1; echo 'debug>' . $theCount; ?>
             <li <?php if ($post_id == get_the_ID()) {echo 'class="active"';} ?>>
                <a href="<?php the_permalink(); ?>">
                  <span><?php the_title(); ?></span>
                </a>
             </li>
           <?php endwhile; ?>
         </ul>
     </div>
   <?php endif; ?>
<?php endif; ?>

Problem being is that it only seems to grab the last 10 posts in that category where i know i have 16 posts for that sidebar to display.

Where in the code could i find where its limiting the return values??

UPDATE

 <?php if (have_posts()) : the_post();
                            $post_id = get_the_ID();

            if ( is_single() ) {
                    $cats =  get_the_category();
                    $cat = $cats[0];
            } else {
                // category archives
                    $cat = get_category( get_query_var( 'cat' ) );
            }

            $cat_id = $cat->cat_ID;
            $cat_name = $cat->name;
            $cat_slug = $cat->slug;


                    $cat_query_args = array(
// Include posts from category with ID $cat_id
'cat' => $cat_id,
// Include all posts
'posts_per_page' => -1
);

 $cat_query = new WP_Query( $cat_query_args );


 if ( $cat_query->have_posts() ) 

 ?>
 <div id="sidebar">
    <h3><?php echo get_cat_name($cat_id); $theCount = 0; ?></h3>
    <ul class="info-list">

    <?php
    while $cat_query->have_posts() : $cat_query->the_post();

        $theCount += 1; echo 'debug>' . $theCount; 
        ?>
        <li <?php if ($post_id == get_the_ID()) {echo 'class="active"';} ?>>
            <a href="<?php the_permalink(); ?>">
                <span><?php the_title(); ?></span>
            </a>
        </li>
       <?php

endwhile; 
?>

     </ul>
 </div>
<?php

 endif;

 ?>

            <?php endif; ?>

            <?php wp_reset_query(); ?>
share|improve this question

3 Answers 3

up vote 0 down vote accepted

Your problem is that you're using query_posts(), when you need to be using WP_Query(). To display all posts (by ignoring pagination, use the posts_per_page parameter:

$cat_query_args = array(
    // Include posts from category with ID $cat_id
    'cat' => $cat_id,
    // Include all posts
    'posts_per_page' => -1
);

$cat_query = new WP_Query( $cat_query_args );

if ( $cat_query->have_posts() ) : 

     ?>
     <div id="sidebar">
        <h3><?php echo get_cat_name($cat_id); $theCount = 0; ?></h3>
        <ul class="info-list">

        <?php
        while ( $cat_query->have_posts() ) : $cat_query->the_post();

            $theCount += 1; echo 'debug>' . $theCount; 
            ?>
            <li <?php if ($post_id == get_the_ID()) {echo 'class="active"';} ?>>
                <a href="<?php the_permalink(); ?>">
                    <span><?php the_title(); ?></span>
                </a>
            </li>
           <?php

    endwhile; 
    ?>

         </ul>
     </div>
    <?php

endif;

// Be kind; rewind
wp_reset_postdata();
share|improve this answer
    
Thanks for the help again, Chip. However, it does not seem to load the page now since i added that code over my old code. –  StealthRT Nov 27 '12 at 15:37
    
I've also updated my code with how i am getting the cat_id. –  StealthRT Nov 27 '12 at 15:38
    
Please note: code is example/proof-of-concept only, and is not necessarily intended to be copy-and-paste-ready. If the page stopped loading, there's probably a syntax error somewhere causing a fatal error. –  Chip Bennett Nov 27 '12 at 15:52
    
Can you use my code mixed with your code as it should be? I can not seem to get it working when i splice them together? –  StealthRT Nov 27 '12 at 15:53
    
Yes, you should be able to. The keys are to replace query_posts() with WP_Query(), to include the posts_per_page parameter, and to instantiate the loop properly. –  Chip Bennett Nov 27 '12 at 15:55

Use query_posts only when you are intentionally messing with the main query, which should be almost never.

Use get_posts or or create a new WP_Query object instead.

$my_posts = get_posts(
    'cat='.$cat_id, 
    'posts_per_page=50'
);

If your $cat_id is correct, that should work. Then loop over it like this (from the Codex):

$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
<?php endforeach; ?>

Or use WP_Query like (also from the Codex):

// The Query
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;
share|improve this answer
    
I've updated my OP code with how i am getting the cat_id. –  StealthRT Nov 27 '12 at 15:41
    
No. Never use query_posts(), even if messing with the main query. Filter pre_get_posts instead. –  Chip Bennett Nov 27 '12 at 15:51
    
That looks right but I don't know if your actually getting the value you want. Only you know that. –  s_ha_dum Nov 27 '12 at 15:53

WP codex is your best friend:

From query_posts codex page:

For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don't care about pagination), you can use query_posts() like so:

query_posts( 'posts_per_page=5' );
share|improve this answer
    
Do not use query_posts(), especially not for secondary queries. –  Chip Bennett Nov 27 '12 at 15:14
    
That doesnt seem to be working? <?php query_posts('cat='.$cat_id, 'posts_per_page=50'); ?> –  StealthRT Nov 27 '12 at 15:14
    
@ChipBennett the original question had no reference to or suggestion that this was a secondary query, and that information is clearly stated in the first paragraph of the link provided. –  totels Nov 30 '12 at 16:51
    
First, the question inherently involves a secondary loop, by virtue of being a sidebar loop. Regardless, using query_posts() is always _doing_it_wrong(), and should never be recommended. Also, any relevant information should not be left to source material via links, but should be included in the answer itself. –  Chip Bennett Nov 30 '12 at 17:17

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.