public
Last active

Private page filters for WordPress. Related to ticket #8592.

  • Download Gist
wp_private_page_filters.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/**
* Enable unpublished pages in page lists and dropdowns
*
* @param array $args
* @return array $args
*/
function scl_list_pages_args( $args ) {
$args['post_status'] = array( 'publish', 'private' );
return $args;
}
 
add_filter( 'wp_list_pages_args', 'scl_list_pages_args' );
add_filter( 'wp_dropdown_pages_args', 'scl_list_pages_args' );
 
/**
* Add private/draft/future/pending pages to parent dropdown in page attributes metabox and Quick Edit
*
* @param array $dropdown_args
* @param object $post (Optional)
* @return array $dropdown_args
*/
function page_attributes_metabox_add_parents( $dropdown_args, $post = NULL ) {
$dropdown_args['post_status'] = array('publish', 'draft', 'pending', 'future', 'private');
return $dropdown_args;
}
 
add_filter( 'page_attributes_dropdown_pages_args', 'page_attributes_metabox_add_parents', 10, 2 );
add_filter( 'quick_edit_dropdown_pages_args', 'page_attributes_metabox_add_parents', 10);
 
/**
* Add (status) to titles in page parent dropdowns
*
* @param string $title
* @param object $page
* @return string $title
*/
function page_parent_status_filter( $title, $page ) {
$status = $page->post_status;
if ($status !== __('publish'))
$title .= " ($status)";
return $title;
}
 
add_filter( 'list_pages', 'page_parent_status_filter', 10, 2);
 
/**
* Filter public page queries to include privately published ones.
* Filter pages metabox on menu admin screen to include all built-in statuses.
*
* @param object $query
* @return object $query
*/
function private_page_query_filter($query) {
if ( is_admin() ) {
$screen = get_current_screen();
if ( 'nav-menus' == $screen['base'] )
$query->set( 'post_status', 'publish,private,future,pending,draft' );
}
else {
$query->set( 'post_status', 'publish,private' );
}
return $query;
}
 
add_filter('pre_get_posts', 'private_page_query_filter');

get_current_screen returns an object so $screen['base'] should be $screen->base

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.