0

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),
));
?>

1 Answer 1

0

For 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

3
  • Sorry, this isn't the issue I'm having. The issue is that I want to omit a particular template type from the list. wordpress.stackexchange.com/questions/277627/… pointed me in the right direction, but for some reason it's not working. Apr 29, 2019 at 10:12
  • Here is what the code snippet you copied does. First, fetches all pages set to that template file. It puts their IDs into an array. Then it fetches all the children of the current page, but excluding the ones that exist in that "exclusion array". However, your first step (fetching all the pages set to that template) was returning nothing, no pages, because it wasn't able to see any "child" page, i.e. a page with a parent. It was an incomplete snippet. With hierarchical=>0, it can correctly build that exclusion array.
    – tmdesigned
    Apr 29, 2019 at 12:15
  • Ah, yes! Apologies, I was reading this completely wrong and placing the 'hierarchical' => 0 into the wp_list_pages array. Thanks so much! Apr 29, 2019 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.