On my template I have this code

<div class="blog_block">

    <?php wp_reset_postdata();  ?>
    <?php $nbmax= 9; ?>
    <?php $tarali_query = new WP_Query('posts_per_page='.$nbmax.'&ignore_sticky_posts=1&paged='.$paged); ?>
    <?php while ($tarali_query -> have_posts()) : $tarali_query -> the_post(); ?>
        <?php get_template_part( 'content-blog', get_post_format() );   ?>                                              
    <?php endwhile; ?>
    <?php tarali_paging_nav(); ?>   

</div><!-- blog_block -->

Which render ALL recent posts of my blog. How can I modify this code to query ONLY the posts which belongs to Portfolio Category?


1 Answer 1


You can simply just add the cat or category_name parameter to your query. When using cat, you should use the category ID, and should you use category_name, it should be the slug of the category, not the name.


<?php $tarali_query = new WP_Query('posts_per_page='.$nbmax.'&ignore_sticky_posts=1&paged='.$paged); ?>


<?php $tarali_query = new WP_Query('posts_per_page='.$nbmax.'&cat=CATID&ignore_sticky_posts=1&paged='.$paged); ?>

For futher reading: WP_Query

  • I did exactly as you said I changed the code to this <?php $tarali_query = new WP_Query('posts_per_page='.$nbmax.'&cat=porto&ignore_sticky_posts=1&paged='.$paged); ?> but nothing has changed at all. May 25, 2014 at 16:35
  • 1
    cat should be a numerical value May 25, 2014 at 16:38
  • 1
    If porto is the slug, change cat to category_name May 25, 2014 at 16:41
  • I did that now there is no posts at all May 25, 2014 at 16:45
  • 1
    Paste your code please May 25, 2014 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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