Sitemap is showing hidden pages

My sitemap is showing hidden folders. I am currently using this xmlsitemap.php:

<?php

$ignore = array('sitemap', 'error');

// send the right header
header('Content-type: text/xml; charset="utf-8"');

// echo the doctype
echo '<?xml version="1.0" encoding="utf-8"?>';

?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<?php foreach($pages->visible()->index() as $p): ?>
		<?php if(in_array($p->uri(), $ignore)) continue ?>
		<url>
			<loc><?php echo html($p->url()) ?></loc>
			<lastmod><?php echo $p->modified('c') ?></lastmod>
			<priority><?php echo ($p->isHomePage()) ? 1 : number_format(0.5/$p->depth(), 1) ?></priority>
		</url>
	<?php endforeach ?>
</urlset>

What am I doing wrong? When I load http://localhost:8888/mypage/sitemap still shows me hidden pages:

<url>
	<loc>
		http://localhost:8888/mypage/companies/giant-electronic-brand
	</loc>
	<lastmod>2015-11-30T16:28:44+00:00</lastmod>
	<priority>0.3</priority>
</url>
1 Like

If you want to show only visible pages, no matter on what level:

<?php 
  foreach($pages->index()->visible() as $p) {
    //do stuff
  } 
?>
3 Likes

Worked great! Thank you!