Question

I am using WordPress 3.8. I want to get query post from a specific category. To do this, I have used the following code

<?php query_posts('post_type=post&category_id=3&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
            <?php if(have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>        

                  <h2><?php the_title(); ?></h2>
                  <p><?php the_content(); ?></p> 

            <?php endwhile; ?>    
            <?php endif; ?>

I am getting all post instead of a specific category. What is wrong with this code.

Was it helpful?

Solution

category_id=3 should just be cat=3

<?php query_posts('post_type=post&cat=3&post_status=publish&posts_per_page=10&paged='. get_query_var('paged')); ?>
        <?php if(have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>        

              <h2><?php the_title(); ?></h2>
              <p><?php the_content(); ?></p> 

        <?php endwhile; ?>    
        <?php endif; ?>

OTHER TIPS

In general avoid using query_posts because it is altering the globals inside the main loop. You can use get_posts():

<?php


$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>

         <h2><?php the_title(); ?></h2>
             <p><?php the_content(); ?></p> 

<?php endforeach; 
wp_reset_postdata();?>

take in acount that 1 is the id of category (look the id for the category you are interested to fetch on your DB)

Here you will find more info

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top