[wp-hackers] Resetting after multiple loops

Otto otto at ottodestruct.com
Fri Oct 24 17:52:33 GMT 2008


Creating a new WP_Query() seems like the better approach to me, as
that never touches the default query in any way.

Whereas get_posts() can be confusing and difficult to use.



On Fri, Oct 24, 2008 at 8:46 AM, Viper007Bond <viper at viper007bond.com> wrote:
> Always use code like this if possible:
>
> $var_name_other_than_posts = get_posts( 'some=parameters' );
>
> foreach ( $var_name_other_than_posts as $post ) {
> // do stuff
> }
>
> That way you never touch the query. Infact get_posts() now internally uses
> query_posts() or whatever so there is very, very little you can't do with
> get_posts().
>
> http://codex.wordpress.org/Template_Tags/get_posts
>
> On Fri, Oct 24, 2008 at 1:40 AM, Nigel Dunn <nigel at redefine.co.uk> wrote:
>
>> Hi all,
>>
>> I've been writing a number of widgets based on the style of the recent
>> posts widget that comes with WP - e.g. listing the most recent posts by an
>> author, for a category, etc. But I've come unstuck with side-effects on
>> other globals even though I'm using what seems to be the suggested pattern
>> of using 'new WP_Query(...)' followed by wp_reset_query().
>>
>> I think the problem is caused by the_post(), but I can't see anything
>> documented about resetting globals after you've used that.
>>
>> If you try the following on a fresh install of WP:
>>
>> * create a new post
>> * add the recent posts widget to the sidebar
>> * change sidebar.php in the default theme to display the value of $id
>> before & after the sidebar contents
>> * change single.php in the default theme to  use the sidebar
>>
>> When I view the new post on the website I have different values for $id. If
>> I comment out the loop that's used in the widget then they're the same. If I
>> stick something like 'if (have_posts()) the_post()' at the end of the
>> sidebar then I also get the same values.
>>
>> So the sort of problem I've been having is that if I have another widget
>> that's supposed to display the details of the post author then it could
>> change if I place that before or after the recent posts widget.
>>
>> Is this a bug or am I missing out on something else? (And should I be
>> posting this elsewhere?)
>>
>> Best wishes
>>
>> Nigel
>>
>>
>> --
>> Nigel Dunn
>> Redefine
>>
>> 0845 838 2720
>> http://www.redefine.co.uk
>>
>>
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>
>
>
> --
> Viper007Bond | http://www.viper007bond.com/ | http://www.finalgear.com/
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list