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.

EDIT: After fiddling with the loop a bit, it somehow corrected itself. At the end of the day the code remains the same, only now it's working.


http://sct.io/tt95p4 - development site. Scroll down below the slider to see the two areas in question (Featured Articles and Latest News)

I have two sections in one template that are using different queries to pull post excerpts and featured images. The odd behavior here is that even though I have the two separate queries, both are returning the same result.

In my template page I have:

<?php
global $post;
$myposts = get_posts('numberposts=2&offset=0&category=23');
foreach($myposts as $post) :
setup_postdata($post);
?>
...
<?php endforeach; ?>

and the second block:

<?php
global $post;
$myposts = get_posts('numberposts=4&offset=0&category=-23');
foreach($myposts as $post) :
setup_postdata($post);
?>
...
<?php endforeach; ?>

So basically wanting the Featured Articles to display the 2 most recent posts from category "23", and the second block should display the 4 most recent articles not in category "23".

For some reason both blocks are returning the posts from category 23.

I am hoping this is a simple fix and I am just missing some small detail.

Thanks in advance!

share|improve this question

closed as too localized by toscho Jul 27 '12 at 22:40

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Just curious why you are calling global $post. Try changing $post in your loop to another variable. Global post will pull in the current queried post which might be causing the conflict. –  Brian Fegter Dec 5 '11 at 5:17
    
Thank you for your help. That wasn't quite it, but seemingly it was enough to just fiddle with the loop to get it to snap out of it. The end result with the code is the same, but exorcized the ghost from the machine and getting the results I was looking for now. –  John Dec 5 '11 at 20:04

1 Answer 1

Use wp_reset_postdata() this may accomplish your requirement

share|improve this answer
    
good idea, thanks. but still getting the odd behavior. thanks again. –  John Dec 5 '11 at 17:23

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