I use get_posts() to generate a menu of posts having the current page as the parent. This causes the post_id to be set to the first such post, instead of the original page's post_id. I.e. I want Wordpress to continue as if nothing happened after the query.
This is the code I use in sidebar.php:
$args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => '',
'orderby' => 'title',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => $slug,
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$pt=get_the_title();
$posts=get_posts($args);
//loop children custom posts.
if ($posts) {
echo '<div class="generated-menu">';
echo '<ul class="left-navigation"><li class="active"><a href="/'.$slug.'">'.$m.'</a>';
// Start the Loop.
foreach ($posts as $p) {
$t=get_the_title($p->ID);
$l=get_permalink($p->ID);
$a='';if ($t==$pt) $a=' class="active"';
echo '<ul><li'.$a.'>';
echo '<a href="'.$l.'">'.$t.'</a>';
echo '</li></ul>';
} //foreach
echo '</li></ul></div>';
} //if has posts
All available information says get_posts() doesn't affect anything. Well, that's confirmed to be a lie, unless you can spot an error in my code.
If I comment out the single line $posts=get_posts($args);
, main content is OK.
I have tried using wp_reset_postdata(), no difference.
Thankful for any help by the pros here :)
$posts
might be interfering; try to change that to something else like$menu_posts
or similar, and try what @Shellbot suggested.