Question

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) ?>
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top