WordPress Hacking II

privatepagesfixI have a few private pages that I use to store random stuff and ideas and private pages (for a reason I don’t understand) don’t show up in the pages widget.

It turns out that WordPress’s get_pages function always filters them out, while get_posts doesn’t (it actually has some logic to figure out whether to show private pages or not).

I’ve decided to fix this. A real fix would probably be merging get_pages and get_posts because both seem to do pretty much the same except that get_posts is a tad bit more advanced, but I’m all for quick fixes at the moment, because I don’t have much time and I don’t want to end up considering myself a PHP developer ;-)

To make get_pages return private pages, too, you have to open the file wp-includes/post.php and change the following lines near the bottom of the get_pages function:

to:

If you also want to make it easier to distinguish private pages from normal ones, you can also change the following bits in wp-includes/classes.php in the method Walker_Page::start_el:

to:

9 thoughts on “WordPress Hacking II

  1. Hi Andreas

    I’m looking for something simular for my site gvej.dk, but it should be in my top navigation.

    Any ideas on how to manage that?

    Any hints are appriciated!

    BR
    Jan J.

  2. Hello Jan,
    I just tried it on my blog and you can do it as follows:
    instead of changing the code in the wordpress source directory (which also gets overwritten when you update WP – I just noticed that my changes are gone), you can instead open the Theme editor (Theme/Editor) and then edit header.php.

    You’ll find a query similar to the one described above and can change it the same way:

    $menupages = $wpdb->get_results(“SELECT ID, post_title FROM $wpdb->posts WHERE post_type = ‘page’ AND (post_status = ‘publish’ ” . $private_pages_inclusion_where . “) AND post_parent = 0 ORDER BY menu_order ASC”);

    to:

    $private_pages_inclusion_where = “”;
    if ( is_user_logged_in() ) {
    $private_pages_inclusion_where = current_user_can( “read_private_pages” ) ? ” OR post_status = ‘private’” : ” OR post_author = $user_ID AND post_status = ‘private’”;
    }

    $menupages = $wpdb->get_results(“SELECT ID, post_title FROM $wpdb->posts WHERE post_type = ‘page’ AND (post_status = ‘publish’ ” . $private_pages_inclusion_where . “) AND post_parent = 0 ORDER BY menu_order ASC”);

    Since you are using a different theme, the code is probably different but I hope the idea comes across.

    Greetings,
    Andreas

  3. Hi Andreas

    Appriciate your answer, thanks!

    Unfortunately, I dont have such query in my theme. I have a head.php which has the following code to generate the menu:

    <li class=”first current_page_item”><a href=”/” title=”">

    Basicly it used the wp_list_pages only.

    Is it possible to squezze in a query like the one you suggested above?

    If you can point me in the right direction, I would be very thankful :-)

    Greetings,
    Jan J., Denmark

  4. Sorry for the long delay.
    I’ve finally found time to look at the code.
    The way I describe above (changing the get_pages function in wp-includes/post.php is probably the easiest way since your theme doesn’t use a custom query but simply uses wp_list_pages.
    If you apply the changes described above in the original post it should work.

    Cheers,
    Andreas

Leave a Reply