The setup_postdata() function confused me for a bit. The examples on the WordPress Codex page didn't seem to explain why using setup_postdata() could be useful. Specifically, there is no difference in the var_dump using $post in example 1.
After running a few different tests, it became clear that $authordata is a great means to demonstrate the differences. In the code below, the first loop returns null and the second returns an object.
PHP:
<?php
require_once '../connectWordPress.php';
global $post;
$args = array(
'numberposts' => 1,
'offset' => 1,
'category' => 1,
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) {
var_dump($authordata);
}
echo '<hr>';
echo '<h2>Add setup_postdata</h2>';
foreach ( $myposts as $post ) {
setup_postdata( $authordata );
var_dump($authordata);
}
wp_reset_postdata();
The var_dump returned shows the object.
Rich (BB code):
object(WP_User)[377]
public 'data' =>
object(stdClass)[373]
public 'ID' => string '1' (length=1)
public 'user_login' => string 'admin' (length=5)
public 'user_pass' => string 'nicetry' (length=34)
public 'user_nicename' => string 'admin' (length=5)
public 'user_email' => string 'layne.heiny@gmail.com' (length=21)
public 'user_url' => string '' (length=0)
public 'user_registered' => string '2016-11-12 21:27:08' (length=19)
public 'user_activation_key' => string '' (length=0)
public 'user_status' => string '0' (length=1)
public 'display_name' => string 'admin' (length=5)
public 'ID' => int 1
public 'caps' =>
array (size=1)
'administrator' => boolean true
public 'cap_key' => string 'wp_capabilities' (length=15)
public 'roles' =>
array (size=1)
0 => string 'administrator' (length=13)
public 'allcaps' =>
array (size=66)
'switch_themes' => boolean true
'edit_themes' => boolean true
'activate_plugins' => boolean true
'edit_plugins' => boolean true
'edit_users' => boolean true
'edit_files' => boolean true
'manage_options' => boolean true
'moderate_comments' => boolean true
'manage_categories' => boolean true
'manage_links' => boolean true
'upload_files' => boolean true
'import' => boolean true
'unfiltered_html' => boolean true
'edit_posts' => boolean true
'edit_others_posts' => boolean true
'edit_published_posts' => boolean true
'publish_posts' => boolean true
'edit_pages' => boolean true
'read' => boolean true
'level_10' => boolean true
'level_9' => boolean true
'level_8' => boolean true
'level_7' => boolean true
'level_6' => boolean true
'level_5' => boolean true
'level_4' => boolean true
'level_3' => boolean true
'level_2' => boolean true
'level_1' => boolean true
'level_0' => boolean true
'edit_others_pages' => boolean true
'edit_published_pages' => boolean true
'publish_pages' => boolean true
'delete_pages' => boolean true
'delete_others_pages' => boolean true
'delete_published_pages' => boolean true
'delete_posts' => boolean true
'delete_others_posts' => boolean true
'delete_published_posts' => boolean true
'delete_private_posts' => boolean true
'edit_private_posts' => boolean true
'read_private_posts' => boolean true
'delete_private_pages' => boolean true
'edit_private_pages' => boolean true
'read_private_pages' => boolean true
'delete_users' => boolean true
'create_users' => boolean true
'unfiltered_upload' => boolean true
'edit_dashboard' => boolean true
'update_plugins' => boolean true
'delete_plugins' => boolean true
'install_plugins' => boolean true
'update_themes' => boolean true
'install_themes' => boolean true
'update_core' => boolean true
'list_users' => boolean true
'remove_users' => boolean true
'promote_users' => boolean true
'edit_theme_options' => boolean true
'delete_themes' => boolean true
'export' => boolean true
'_debug_objects' => boolean true
'use_wp_admin_microblog' => boolean true
'use_wp_admin_microblog_bp' => boolean true
'use_wp_admin_microblog_sticky' => boolean true
'administrator' => boolean true
public 'filter' => null
Besides $authordata, it's possible to use $id, $currentday, $currentmonth, $page, $pages, $multipage, $more, and $numpages.