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.

I have a query which looks like this:

$args = arguments for the query
$query = new WP_Query($args);

while ($query->have_posts()): $query->the_post();
$post_id = get_the_ID();
echo my_function($post_id);
endwhile; wp_reset_query(); endif;

Then in my_function there's another query with a second function. This second function uses the post_id as input:

$args = arguments for the query, using `$post_id` as input
while ($query->have_posts()): $query->the_post();
// Do stuff
endwhile; wp_reset_query(); endif;

The problem is that the output of the first query (which uses my_function) isn't correct. The query in my_function seems to mess up the loop.

As you can see I use wp_reset_query() and I've also tried wp_reset_postdata(). my_function outputs some post information, among which the title. It displays the title of the current page (the page on which the query is) instead of the title of the post queried, so I think I'm missing something regarding resetting the query.

share|improve this question
    
You might want to add the actual arguments of you queries, just in case the problem lies there. –  ialocin Nov 4 '13 at 10:53
    
please explain what do you mean in "isn't correct". In addition I don't see anything wrong in your code, maybe you are not showing enough of it. –  Mark Kaplun Nov 4 '13 at 10:53
    
Please follow up on the questions you asked, it's an important part of the process on WPSE - see What should I do when someone answers my question? and Why is voting important? for a deeper insight. Thank you! –  ialocin Nov 7 '13 at 12:55
add comment

2 Answers

Using wp_reset_query() really just makes sense if you are using query_posts(), which you don't need and shouldn't do - see When should you use WP_Query vs query_posts() vs get_posts()? .

This means you should go with WP_Query and wp_reset_postdata() instead. Besides that you should give your custom, secondary queries a object, name differing from $query - like this:

$your_custom_query = new WP_Query($args);
while ($your_custom_query->have_posts()): $your_custom_query->the_post();
    //post stuff
endwhile; wp_reset_postdata(); endif;
share|improve this answer
    
I've simplified my question, but I should've mentioned that I am using a custom name for my second query. I ended up storing the title of each post in a variable before the second query and after the second query I can just use the variable (instead of using get_the_title() after the query). The query and functions together are pretty extensive and since my problem is solved (by this slightly hacky workaround) it not necessary to update the question right? I also don't think that I should answer my own question with 'put the values in variables before the second query'... –  eskimo Nov 4 '13 at 11:54
    
If you want others to understand what the problem was, which is kind of the point of WPSE, you should update the question. And, you absolutely can answer your own question, but if so it'd be best if you show your solution and not just describe it. @joostvanhoof –  ialocin Nov 4 '13 at 12:11
add comment

Please note: the answer below does fix the problem as described, but it doesn't correct the loop so it stops altering the queried data.

The problem was that functions used after the second query (like get_the_title()) returned titles for the current page the query is on, not the title of the post queried.

So I ended up storing all the values I want in variables, for example $title = get_the_title() BEFORE the second query. Than after the second query these variables can be used because they don't get altered by the second query.

I'm still not sure why the second query alters the data. I've tried renaming the second query and using wp_reset_postdata as suggested, but that didn't fix the problem.

share|improve this answer
    
I'm pretty certain your workaround shouldn't be necessary, but without seeing your actual code there is no way of knowing what the problem is. –  ialocin Nov 4 '13 at 14:24
add comment

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.