Display all page which have not a certain template

admin2025-06-04  16

I'm trying to display all pages of my website which have not the following template: template-rubrique.php.
It works great but it doesn't output the blog page as well, since it doesn't have a template at all.

How should I proceed?

$args = array(
  'post_type'      => 'page',
  'posts_per_page' => -1,
  'order'          => 'ASC',
  'orderby'        => 'title',
  'meta_query'     => array(
    array(
      'key'       => '_wp_page_template',
      'value'     => 'template-rubrique.php',
      'compare'   => '!=',
    )
  )
);

I'm trying to display all pages of my website which have not the following template: template-rubrique.php.
It works great but it doesn't output the blog page as well, since it doesn't have a template at all.

How should I proceed?

$args = array(
  'post_type'      => 'page',
  'posts_per_page' => -1,
  'order'          => 'ASC',
  'orderby'        => 'title',
  'meta_query'     => array(
    array(
      'key'       => '_wp_page_template',
      'value'     => 'template-rubrique.php',
      'compare'   => '!=',
    )
  )
);
Share Improve this question edited Jan 5, 2019 at 22:49 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jan 5, 2019 at 22:40 QuentinQuentin 158 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can add an OR relation to the meta query and also get pages with no _wp_page_template meta key:

$args = array(
    'post_type' => 'page',
    'posts_per_page' => -1,
    'order'  => 'ASC',
    'orderby' => 'title',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => '_wp_page_template',
            'value' => 'template-rubrique.php',
            'compare' => '!=',
        ),
        array(
            'key' => '_wp_page_template',
            'compare' => 'NOT EXISTS',
        )

    )
);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749040554a315852.html

最新回复(0)