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.
2 Answers
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 . '"> ' . get_the_title( $subpage->ID ) . '</option>';
}
}
echo '</select>';