1

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:

  1. 738
  2. Resources
  3. Another test post? I THINK SO!
  4. TEEEEESSSSSSTTTTT POST!!!
  5. 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

2 Answers 2

1

The $wp_query object is used to store the main page query, so when you're using

$wp_query = new WP_Query( $args );

you'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:

if( $mr_custom_query->have_posts() ) : while( $mr_custom_query->have_posts() ) : $mr_custom_query-> the_post();

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 use query_posts(), or better yet, the pre_get_posts hook.

0

that is really weird, I would start for the basis, maybe the query is carry something else, try adding

  <?php wp_reset_query() ?>

Before your query and see what's displaying.

1
  • Hi Andrew, thanks for your response, I appreciate it. I gave it a go, but unfortunately it didn't work. What did work, however, was excluding the other categories, as opposed to specifying only one category. I don't know if this is a bug, or if it's just a quirk I don't understand, but it certainly was odd.
    – MattR
    Oct 6, 2011 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.