admin管理员组文章数量:1022726
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 |1 Answer
Reset to default 1You 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.
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 tohide_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
1 Answer
Reset to default 1You 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.
本文标签: Pre get posts where template is not equal to one specified
版权声明:本文标题:Pre get posts where template is not equal to one specified? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745574520a2156936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pre_get_posts
? The title of the question might need adjusting. Are you trying to add a condition tohide_some_posts()
that checks the template in use for that page? – jdm2112 Commented Apr 18, 2019 at 17:01