I'm trying to do something similar to the question in Exclude pages with certain template from wp_list_pages, but the $exclude
variable doesn't seem to be returning the _wp_page_template
value to then filter out the relevant pages with the template guidance-note-template.php
from the list. Any tips, greatly appreciated.
<?php
global $post;
$exclude = [];
foreach(get_pages(['meta_key' => '_wp_page_template', 'meta_value' => 'guidance-note-template.php']) as $page) {
$exclude[] = $page->post_id;
}
$children = get_pages( array( 'child_of' => $post->ID ) );
$hasChild = (count( $children ) > 0 );
$page_id = ($hasChild) ? $post->ID : wp_get_post_parent_id( $post->ID );
wp_list_pages( array(
'title_li' => '',
'child_of' => $page_id,
'exclude' => implode(",", $exclude),
));
?>
I'm trying to do something similar to the question in Exclude pages with certain template from wp_list_pages, but the $exclude
variable doesn't seem to be returning the _wp_page_template
value to then filter out the relevant pages with the template guidance-note-template.php
from the list. Any tips, greatly appreciated.
<?php
global $post;
$exclude = [];
foreach(get_pages(['meta_key' => '_wp_page_template', 'meta_value' => 'guidance-note-template.php']) as $page) {
$exclude[] = $page->post_id;
}
$children = get_pages( array( 'child_of' => $post->ID ) );
$hasChild = (count( $children ) > 0 );
$page_id = ($hasChild) ? $post->ID : wp_get_post_parent_id( $post->ID );
wp_list_pages( array(
'title_li' => '',
'child_of' => $page_id,
'exclude' => implode(",", $exclude),
));
?>
Share
Improve this question
asked Apr 26, 2019 at 11:44
SketchybearSketchybear
233 bronze badges
1 Answer
Reset to default 0For children pages to show, the 'hierarchical' => 0
option must also be set in your get_pages() function.
I copied your code and initially had the same results (it returned no pages). Adding in the hierarchical setting set to 0 made it return the 2 pages I had set to that template, which could then be excluded. It then output all the other children except for those 2, as I think you wanted.
Credit for solution: Older Stack Overflow answer