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.
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