I'm trying to not show certain posts in the admin area based on the page template. Here is what I have so far:

add_filter( 'parse_query', 'hide_some_posts' );

function hide_some_posts( $query ) {
    global $pagenow;
    $myHiddenPosts = array('10667');
    if ( is_admin() && $pagenow=='edit.php') {
       $query->set( 'post__not_in', $myHiddenPosts );

This works perfectly based on page ID, but I'm looking to do this based on the page template instead. I came across this:

    $query->set('meta_value', 'page-some-template.php'); //Change filename to yours

But how can I make it so that only works for including. I would like to not include pages based on the template.

Any way to do this?


  • Where are you using pre_get_posts? The title of the question might need adjusting. Are you trying to add a condition to hide_some_posts() that checks the template in use for that page? – jdm2112 Apr 18 at 17:01
  • @jdm2112 Yes trying to hide some posts that check the template in use. – Best Dev Tutorials Apr 18 at 19:02

You should use meta_query to pass meta query arguments.

add_action('parse_query', 'se334731_filter_admin_post_list');

function se334731_filter_admin_post_list( $query )
    $screen = get_current_screen();
    if ( is_admin()  && $screen->post_type == 'page'  && $screen->base == 'edit' )
        $query->query_vars['meta_query'][] = [
            'relation' => 'OR',
                'key' => '_wp_page_template',
                'value'    => 'page-some-template.php',
                'compare'  => '!=',
                //'value'    => array( 'page-some-template.php' ),
                //'compare'  => 'NOT IN'
                'key' => '_wp_page_template',
                'compare'  => 'NOT EXISTS'

These changes will not affect the summary of the number of posts (all, published, trashed) above table. To correct the displayed number of posts, use the wp_count_posts filter.

Here you can read more about Custom field parameters.

  • Sadly this didn't work. Not sure what is wrong. – Best Dev Tutorials Apr 18 at 19:23
  • 2
    I wrote meta_key insted of key. – nmr Apr 18 at 19:43
  • How can be be done for multiple page templates? – Best Dev Tutorials Apr 18 at 19:46
  • Comment out first lines with value and compare and uncomment the next two lines. – nmr Apr 18 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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