Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

I was reading this post: When should you use WP_Query vs query_posts() vs get_posts()?When should you use WP_Query vs query_posts() vs get_posts()?, which seems to have become the go-to post for understanding the difference between the different functions for making custom loops.

In the article, it says that

[get_posts] doesn't modify global variables and is safe to use anywhere

Nonetheless, when I inserted into within another loop (I wanted to have a home page showing 3 columns, 2 done with custom fields, one with latest blog posts), I had to preserve the original $post, like so:

<?php global $post; // required
$tmp_post = $post; // save original post
args = array('numberposts' => 2); // Retrieve just 2 posts
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
?>
    //My posts code here (removed to simplify)
<?php
endforeach;
$post = $tmp_post; //go back to original post
?>

Before I saved the original post, my third column, with the custom field, was blank, but after resetting, as above, it works fine.

Did I misunderstand what that line in the post meant? Did I do something I wasn't supposed to?

I was reading this post: When should you use WP_Query vs query_posts() vs get_posts()?, which seems to have become the go-to post for understanding the difference between the different functions for making custom loops.

In the article, it says that

[get_posts] doesn't modify global variables and is safe to use anywhere

Nonetheless, when I inserted into within another loop (I wanted to have a home page showing 3 columns, 2 done with custom fields, one with latest blog posts), I had to preserve the original $post, like so:

<?php global $post; // required
$tmp_post = $post; // save original post
args = array('numberposts' => 2); // Retrieve just 2 posts
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
?>
    //My posts code here (removed to simplify)
<?php
endforeach;
$post = $tmp_post; //go back to original post
?>

Before I saved the original post, my third column, with the custom field, was blank, but after resetting, as above, it works fine.

Did I misunderstand what that line in the post meant? Did I do something I wasn't supposed to?

I was reading this post: When should you use WP_Query vs query_posts() vs get_posts()?, which seems to have become the go-to post for understanding the difference between the different functions for making custom loops.

In the article, it says that

[get_posts] doesn't modify global variables and is safe to use anywhere

Nonetheless, when I inserted into within another loop (I wanted to have a home page showing 3 columns, 2 done with custom fields, one with latest blog posts), I had to preserve the original $post, like so:

<?php global $post; // required
$tmp_post = $post; // save original post
args = array('numberposts' => 2); // Retrieve just 2 posts
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
?>
    //My posts code here (removed to simplify)
<?php
endforeach;
$post = $tmp_post; //go back to original post
?>

Before I saved the original post, my third column, with the custom field, was blank, but after resetting, as above, it works fine.

Did I misunderstand what that line in the post meant? Did I do something I wasn't supposed to?

Source Link
coopersita
  • 526
  • 1
  • 4
  • 17

get_posts() and global variables

I was reading this post: When should you use WP_Query vs query_posts() vs get_posts()?, which seems to have become the go-to post for understanding the difference between the different functions for making custom loops.

In the article, it says that

[get_posts] doesn't modify global variables and is safe to use anywhere

Nonetheless, when I inserted into within another loop (I wanted to have a home page showing 3 columns, 2 done with custom fields, one with latest blog posts), I had to preserve the original $post, like so:

<?php global $post; // required
$tmp_post = $post; // save original post
args = array('numberposts' => 2); // Retrieve just 2 posts
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
?>
    //My posts code here (removed to simplify)
<?php
endforeach;
$post = $tmp_post; //go back to original post
?>

Before I saved the original post, my third column, with the custom field, was blank, but after resetting, as above, it works fine.

Did I misunderstand what that line in the post meant? Did I do something I wasn't supposed to?