Good Afternoon,
In my theme I have two separate pages for blogs. The main blog page, “news.php”, holds our standard news, thoughts, events type of posts. The second blog, “questions.php”, holds blog postings of only the “Ask a Question” category. In my sidebar I’ve included the default “Recent Posts” widget.
On the main blog page, the query is set up like so to exclude the “Ask a Question” category:
$args = array (
'posts_per_page' => 7,
'cat' => -16,
'paged' => $paged,
);
$wp_query = new WP_Query( $args );
On the Ask a Question blog page, the query is set up like so, to only display the “Ask a Question” category:
$args = array (
'posts_per_page' => 7,
'cat' => 16,
'paged' => $paged,
);
$wp_query = new WP_Query( $args );
Everything seems to work nicely until you get to the “Recent Posts” widget. In the standard news page, the widget displays just fine, showing the proper listing of blog posts. However, on the Ask a Question page, the widget is showing pages, as well as postings, some of which do not exist. For example this is on the list:
- 738
- Resources
- Another test post? I THINK SO!
- TEEEEESSSSSSTTTTT POST!!!
- pump up the jam, pump it up!
If I comment out the category listing from questions.php in the query, everything works fine. So the line that causes a problem is:
'cat' => 16,
I was hoping that someone may be able to help me understand why this is occurring, or possibly how to fix it. Thank you for your time and attention, it is greatly appreciated.
Matt
The
$wp_query
object is used to store the main page query, so when you’re usingyou’re wiping out the defaults. Hence, the
wp_reset_query()
has nothing to reset.To fix the problem, save your custom query object with a different name, maybe
$mr_custom_query
, and then when looping through it, use the format:The WP_Query page on the codex has a ton of great code snippets if you’re still stuck.
I should also add that if you were intentionally using
$wp_query
to override the main page query, you should instead usequery_posts()
, or better yet, thepre_get_posts
hook.that is really weird, I would start for the basis, maybe the query is carry something else, try adding
Before your query and see what’s displaying.