最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Pre get posts where template is not equal to one specified?

programmeradmin0浏览0评论

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_key','_wp_page_template');
    $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?

Thanks!

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_key','_wp_page_template');
    $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?

Thanks!

Share Improve this question asked Apr 18, 2019 at 16:35 Best Dev TutorialsBest Dev Tutorials 4451 gold badge7 silver badges21 bronze badges 2
  • 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 Commented Apr 18, 2019 at 17:01
  • @jdm2112 Yes trying to hide some posts that check the template in use. – Best Dev Tutorials Commented Apr 18, 2019 at 19:02
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布评论

评论列表(0)

  1. 暂无评论