Mike Glendinning
Forum Replies Created
-
@safiayonker: in this case, the “&” is probably a typo.
I’m not a PHP expert, but this is supposed to be how you return a reference rather than a copy of a variable (see the PHP manual for more information).
At least in WordPress 3.0 it seems that “get_posts” is not declared this way, but “get_children” is (see source file “wp-includes/post.php”), so you should probably use a “&” for the latter but not for the former.
In practice, I’m not sure how picky the PHP compiler/runtime is about this sort of thing, so you may be able to get away with or without the “&” in many situations.
Hi,
I had a similar issue and believe the problem here is a bug within function “setup_postdata()” (found in query.php line 2710 for WP 2.9.1) and its handling of global variables.
This function sets the global variable “$page” based on the instance variables of the global query object, i.e. “$page = get_query_var(‘page’)”.
Of course if you have done a separate query (with “get_posts()” or whatever) and are trying to make use of the template functions by calling “setup_postdata()”, this is incorrect as the value of “$page” should be related to your separate query.
The problem is further compounded because “setup_postdata()” only overwrites the first entry in the global “$pages[]” array if your separate query has only a single page. Hence, the other array entries still contain data from the main page and can be inadvertently accessed by function “get_the_content()” using the incorrect value of “$page”.
My solution for handling nested loops in this way is as follows:
$myposts = &get_posts(...) /* Or new WP_Query or whatever. */ global $post, $page; $mysavedpost = $post; foreach ($myposts as $post) { setup_postdata($post); $page = 1; the_title(); the_content(); /* And so on. */ } $post = $mysavedpost; setup_postdata($post);But it would still be nice if the problems with “setup_postdata()” were fixed!