0

Before you mark this as duplicate I have tried every method in all the other questions and non of them have worked.

I am trying to exclude any page with the page template page-noindex.php from the wp_list_pages(); query.

The below code does not work and when I echo out $the_query it just displays 'Array'.

    <?php 
        $the_query = array(
            'post_type'  => 'page',  /* overrides default 'post' */
            'meta_key'   => '_wp_page_template',
            'meta_value' => 'page-templates/page-noindex.php'
        );

        $args = array(
            'exclude'      => $the_query,
            'title_li'     => '',
            'sort_column'  => 'menu_order, post_title',
            'post_type'    => 'page',
                'post_status'  => 'publish' 
        ); ?>

    <?php wp_list_pages($args) ?>
1
  • 1
    Exclude does not accept an array. It only accepts a comma separated list of page IDs. Also, you can not echo an array. You should use print_r($the_query) instead.
    – Johansson
    Commented Aug 21, 2017 at 21:08

1 Answer 1

1

Daniel, exclude parameter doesn't accept array.

Use your code this way:

$exclude = [];
foreach(get_pages(['meta_key' => '_wp_page_template', 'meta_value' => 'page-templates/page-noindex.php']) as $page) {
    $exclude[] = $page->post_id;
}

$args = array(
    'exclude'      => implode(",", $exclude),
    'title_li'     => '',
    'sort_column'  => 'menu_order, post_title',
    'post_type'    => 'page',
    'post_status'  => 'publish'
); 
wp_list_pages($args);

I think you can refactor it better for your needs

1
  • Thanks ever so much! This works a charm, I was just copying what others had put on here using an array, thanks for pointing out thats why it doesn't work! Commented Aug 21, 2017 at 21:27

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.