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.
For your second example, I think you’re initiating your Loop incorrectly. You’re declaring
$query
, which hasn’t been globalized?Try omitting
$query
from your Loop initiation?…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:
Try this instead:
…so that your
$catid
gets parsed properly.