最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display all page which have not a certain template

matteradmin10PV0评论

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',
        )

    )
);
Post a comment

comment list (0)

  1. No comments so far