Listing Private Pages

One odd little quirk of WordPress is that private pages aren't included in the page lists and navigation menus, even if the user is logged in and has permission to read them. The filter function in Listing 9-21 adds the private pages only if the user has the appropriate capability. This is not a perfect function by any means. It adds the private pages in a flat list, ignoring the parent/child hierarchy. However, the code necessary to create a true hierarchical private page menu is well beyond the scope of this book.

Listing 9-21. Adding private pages to the page lists and nav menus function private_suite_add_menu($pagelist) {

if (current_user_can('read_private_pages')) { global $wpdb; $list = '';

$pageresults = $wpdb->get_results(wpdb_prepare("SELECT id, post_title, post_parent FROM $wpdb->posts WHERE post_status=%s AND post_type=%s" ,'private','page'));

foreach ($pageresults as $privatepage) {

$list .= '<li class="page_item private"><a href="'.get_permalink($privatepage->id).'" title="'.esc_attr($privatepage->post_title).'">'

.$privatepage->post_title.'</a></li>';

return $pagelist.$list;

else return $pagelist;

add_filter('wp_list_pages', 'private_suite_add_menu'); add_filter('nav_menu', 'private_suite_add_menu');

As you can see, if the user doesn't have permission to read private pages, the rest of the function is bypassed entirely and the filter returns the unchanged list of pages.

Was this article helpful?

0 0

Post a comment