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 originally had this query on a posts category page:

        <?php $page_query = new WP_Query('post_type=post&cat=145'); ?>

Which worked, but it only displayed the first 4 posts. So I wanted to display all the posts from the category, as well as change the posts per page limit, as well as set the orderby for good measure. However, this query returns EVERY post on my site, regardless of category:

        <?php $page_query = new WP_Query('post_type=post&cat=145'. '&posts_per_page=-1&cat' . '&orderby=date&order=asc'); ?>

Seems like it should work in theory. Any ideas what I'm doing wrong?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You are passing the cat parameter twice-- the second time is empty.

$page_query = new WP_Query(
    'post_type=post&cat=145'. 
    '&posts_per_page=-1&cat' . // <-- here
    '&orderby=date&order=asc'
);

Maybe it is just me, but I find those query-string-like parameters hard to read and hard to keep straight. I'd advise you to create a proper array.

$page_query = new WP_Query(
    array (
        'post_type' => 'post',
        'cat' => 145, 
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'asc'
    )
);
share|improve this answer
1  
Thanks! Should it be 'cat' => 145? –  miles Apr 25 '13 at 20:59
    
Yes. Corrected it. –  s_ha_dum Apr 25 '13 at 21:00

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.