Skip to content

Instantly share code, notes, and snippets.

@stephanieleary
Last active March 7, 2018 12:41
Show Gist options
  • Save stephanieleary/4091448 to your computer and use it in GitHub Desktop.
Save stephanieleary/4091448 to your computer and use it in GitHub Desktop.
Private page filters for WordPress. Related to ticket #8592.
<?php
/*
Plugin Name: Unpublished Hierarchies
Description: A tiny plugin to allow draft, private, scheduled, and password-protected pages to be selected as parents.
Author: Stephanie Leary
Version: 1.0
Author URI: http://stephanieleary.com
License: GPL2
*/
/**
* You may download the zip file at http://stephanieleary.com/downloads/plugins/unpublished-hierarchies.zip
**/
/**
* 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');
@candell
Copy link

candell commented Apr 7, 2015

Was working my way down the list of search results with all pointing to the 6 year old ticket #8592 fearing I was going to have to write something then up came this result.

Thanks for sharing this, much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment