Last active
August 29, 2015 13:56
-
-
Save toddsby/9276990 to your computer and use it in GitHub Desktop.
Exclude pages from wordpress 'at a glance' aka right now widget using wp_count_posts filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Function to retrive page id by slug | |
* @param string - page slug name | |
* @return int - page id | |
*/ | |
function toddsby_get_page_by_slug($page_slug) { | |
$page = get_page_by_path($page_slug); | |
if ($page) { | |
return $page->ID; | |
} else { | |
return null; | |
} | |
} | |
/** | |
* Function to retrive children page id's of provided page id (workaround for missing post_parent__not_in in wp_query pre wp 3.6) | |
* @param int - page id | |
* @return array (page id and children id's) | |
*/ | |
function toddsby_get_page_children_by_id($id) { | |
$ids = array(); | |
$ids[] = $id; | |
$args = array ( | |
'child_of' => $id | |
); | |
$childs = get_pages($args); | |
foreach ($childs as $child) { | |
$ids[] = $child->ID; | |
} | |
return $ids; | |
} | |
/** | |
* Function to filter page count on admin dashboard 'at a glance' widget | |
* Exclude given pages from wp_count_posts | |
* @param object (wordpress $counts object), string ($type of post), string ($perm not using) | |
* @return object $counts | |
*/ | |
function toddsby_exclude_pages_from_counts( $counts,$type,$perm ) { | |
if ($type != 'page') return $counts; // if $type isn't page return $counts unchanged | |
$id = toddsby_get_page_by_slug('pageslug'); // get page id for page slug to exclude | |
$children = toddsby_get_page_children_by_id($id); //find any children of page id | |
$subtract = count($children); //count array | |
$counts->publish = $counts->publish - $subtract; //subtract from publish count and store changed value | |
return $counts; | |
} | |
add_filter( 'wp_count_posts', 'toddsby_exclude_pages_from_counts', 10, 3); // hook that returns 3 parameters of wp_count_post function (wp-includes/post.php) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment