3

I am using wp_dropdown_pages in the backend of my site to generate a list of pages according to a specific set of criteria.

The list only shows published posts where I would like it to show both published posts and drafts.

I have found other Stack Exchange threads which show how to accomplish this in the standard wordpress dropdowns, but this does not seem to extend to wp_dropdown_pages.

What I am using to show drafts in the standard dropdowns is this:

<?php
    /* Show drafts in dropdown lists */
    add_filter('page_attributes_dropdown_pages_args', 'my_attributes_dropdown_pages_args', 1, 1);
    add_filter('quick_edit_dropdown_pages_args', 'my_attributes_dropdown_pages_args', 1, 1);

    function my_attributes_dropdown_pages_args($dropdown_args) {
        $dropdown_args['post_status'] = array('publish','draft');
        return $dropdown_args;
    }
?>

The above works great. I would like to do exactly the same thing for custom fields generated using wp_dropdown_pages. To clarify, in the backend, I am generating a dropdown list using the following code:

<?php
    $dropdown_args_food = array(
        'depth'         => '2',
        'selected'      => $selectedFoodType,
        'post_type'     => 'page',
        'name'          => 'selected-food-type',
        'id'            => 'selected-food-type',
        'echo'          => 1,
        'meta_key'      => 'category',
        'meta_value'    => 'food',
        'hierarchical'  => 1,
        'show_option_none'      => ' ',
    );
    wp_dropdown_pages( $dropdown_args_food );
?>

The above generates a dropdown list of pages exactly as I want it to be, only without drafts.

  • You can't pass post_status as argument to wp_dropdown_pages() your options would be to generate the posts from get_posts() or WP_Query custom query in that case. – Ahmed Fouad Sep 22 '16 at 20:27
1

Update

It seems that you can do this directly with WP function wp_dropdown_pages() as birgire points out in his response below: https://wordpress.stackexchange.com/a/240275/102371


This solution is longer, and uses get_posts() to fetch specific post statuses.

$pages = get_posts( array( 'post_type' => 'page', 'post_parent' => 0, 'post_status' => array( 'draft', 'publish' ) ) );

echo '<select name="selected-food-type" id="selected-food-type">';
foreach( $pages as $page ) {
    echo '<option value="' . $page->ID . '">' . get_the_title( $page->ID ) . '</option>';
    $children = get_children( 'post_parent='. $page->ID );
    foreach( $children as $subpage ) {
        echo '<option value="' . $subpage->ID . '">&nbsp;&nbsp;&nbsp;' . get_the_title( $subpage->ID ) . '</option>';
    }
}
echo '</select>';
|improve this answer|||||
  • Thanks for this - it's fantastic. I hadn't considered using something besides wp_dropdown_pages and had been wrestling with that rather than looking for other solutions! It works perfectly. – yyyyyyyyyyyyyyyyy Sep 22 '16 at 22:48
  • birgire's answer below should be the accepted answer, uses less code and direct WP API : wordpress.stackexchange.com/a/240275/102371 – Ahmed Fouad Sep 22 '16 at 23:37
  • @AhmedMahdi Your approach has few benefits though, to be fair. It can e.g. support multiple post types and handle non-hierarchical post types. So that's why I already upvoted your answer as an alternative. There are therefore pros/cons to both methods, depending on what the user wants ;-) – birgire Sep 22 '16 at 23:59
1

You should be able to use

'post_status' => 'publish,draft',

in wp_dropdown_pages() since it's a wrapper for get_pages() that supports the post_status argument with comma separated list of post status types.

About the documentation of get_pages():

It looks like the documentation for the post_status argument of get_pages() isn't accurate enough. It says:

* @type string  $post_status  A comma-separated list of post statuses types to include.

But it does actually support an array as well, so we can write:

'post_status' => [ 'publish', 'draft' ],

I filed ticket #38136 to address that.

|improve this answer|||||
  • am little confused excuse me, so does it currently work? (just not documented) – Ahmed Fouad Sep 22 '16 at 23:28
  • 1
    it should work, but isn't documented. Did you test it? @AhmedMahdi – birgire Sep 22 '16 at 23:35
  • yup that's much cleaner / quicker. i was not aware of this, though the documentation needs to be updated really for issues like these. – Ahmed Fouad Sep 22 '16 at 23:37
  • yes, we should keep in mind that the documentation isn't always accurate, but it's getting better @AhmedMahdi – birgire Sep 22 '16 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.