1

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.

1 Answer 1

1

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.

3
  • 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.
    – laras126
    Commented Oct 25, 2011 at 14:39
  • Okay, may have found the problem with the second example. See edited answer. Commented Oct 25, 2011 at 14:48
  • Came across a problem - the query stops working when I change to a static homepage. Any ideas? Thanks again.
    – laras126
    Commented Oct 26, 2011 at 12:54

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.