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.

In my theme I provide an option for users to set a 'blog category' that queries and displays only posts from that category on index.php using WP_Query:

$shortname = get_option('of_shortname');
$cat_option = get_option($shortname.'_cats_in_blog');
$catid = get_cat_ID($cat_option);
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
   'cat' => $catid,
   'paged' => $paged
);

$query = new WP_Query( $args );

// Queries fine, but search stops working       
while ( $query->have_posts() ) : $query->the_post();

// Loop stuff here

endwhile;

This works well, but the search widget breaks. I have also tried using query_posts but that has the opposite effect; the queried category isn't shown but the search still functions. Here's that code:

$shortname = get_option('of_shortname');
$cat_option = get_option($shortname.'_cats_in_blog');
$catid = get_cat_ID($cat_option);

// Search is working, querying is not
global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=3&cat=$catid');

while ( $query->have_posts() ) : $query->the_post();

// Loop stuff here

endwhile;

I checked the $catid variable and it is returning what is should. Am I querying the wrong way or in the wrong place? Any help is greatly appreciated, thanks in advance.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

For your second example, I think you're initiating your Loop incorrectly. You're declaring $query, which hasn't been globalized?

// Search is working, querying is not
global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=3&cat=$catid');

while ( $query->have_posts() ) : $query->the_post();

Try omitting $query from your Loop initiation?

// Search is working, querying is not
global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=3&cat=$catid');

while ( have_posts() ) : the_post();

...and see if that makes a difference?

For the first example, what does "the search Widget breaks" mean?

EDIT

Wow, I completely overlooked this: you're passing your variable as a string:

$posts = query_posts($query_string.'&posts_per_page=3&cat=$catid');

Try this instead:

$posts = query_posts( $query_string . '&posts_per_page=3&cat=' . $catid );

...so that your $catid gets parsed properly.

share|improve this answer
    
By search Widget breaks, I mean stops functioning - it just reloads the page when I search something. Removing $query doesn't make a difference - the loop still shows posts from all categories, but the search does work. Thanks for your help. –  notlaura Oct 25 '11 at 14:39
    
Okay, may have found the problem with the second example. See edited answer. –  Chip Bennett Oct 25 '11 at 14:48
    
Whoops, derp. Thanks! Works great. –  notlaura Oct 25 '11 at 15:01
    
Came across a problem - the query stops working when I change to a static homepage. Any ideas? Thanks again. –  notlaura Oct 26 '11 at 12:54

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.