| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
if( ! function_exists( 'wp_dropdown_posts' ) ) { |
| 7 |
|
| 8 |
/** |
| 9 |
* Create dropdown HTML content of posts |
| 10 |
* |
| 11 |
* The content can either be displayed, which it is by default or retrieved by |
| 12 |
* setting the 'echo' argument. The 'include' and 'exclude' arguments do not |
| 13 |
* need to be used; all published posts will be displayed in that case. |
| 14 |
* |
| 15 |
* Supports all WP_Query arguments |
| 16 |
* @see https://codex.wordpress.org/Class_Reference/WP_Query |
| 17 |
* |
| 18 |
* The available arguments are as follows: |
| 19 |
* |
| 20 |
* @author Myles McNamara |
| 21 |
* @website https://smyl.es |
| 22 |
* @updated March 29, 2016 |
| 23 |
* |
| 24 |
* @since 1.0.0 |
| 25 |
* |
| 26 |
* @param array|string $args { |
| 27 |
* Optional. Array or string of arguments to generate a drop-down of posts. |
| 28 |
* {@see WP_Query for additional available arguments. |
| 29 |
* |
| 30 |
* @type string $show_option_all Text to show as the drop-down default (all). |
| 31 |
* Default empty. |
| 32 |
* @type string $show_option_none Text to show as the drop-down default when no |
| 33 |
* posts were found. Default empty. |
| 34 |
* @type int|string $option_none_value Value to use for $show_option_non when no posts |
| 35 |
* were found. Default -1. |
| 36 |
* @type array|string $show_callback Function or method to filter display value (label) |
| 37 |
* |
| 38 |
* @type string $orderby Field to order found posts by. |
| 39 |
* Default 'post_title'. |
| 40 |
* @type string $order Whether to order posts in ascending or descending |
| 41 |
* order. Accepts 'ASC' (ascending) or 'DESC' (descending). |
| 42 |
* Default 'ASC'. |
| 43 |
* @type array|string $include Array or comma-separated list of post IDs to include. |
| 44 |
* Default empty. |
| 45 |
* @type array|string $exclude Array or comma-separated list of post IDs to exclude. |
| 46 |
* Default empty. |
| 47 |
* @type bool|int $multi Whether to skip the ID attribute on the 'select' element. |
| 48 |
* Accepts 1|true or 0|false. Default 0|false. |
| 49 |
* @type string $show Post table column to display. If the selected item is empty |
| 50 |
* then the Post ID will be displayed in parentheses. |
| 51 |
* Accepts post fields. Default 'post_title'. |
| 52 |
* @type int|bool $echo Whether to echo or return the drop-down. Accepts 1|true (echo) |
| 53 |
* or 0|false (return). Default 1|true. |
| 54 |
* @type int $selected Which post ID should be selected. Default 0. |
| 55 |
* @type string $select_name Name attribute of select element. Default 'post_id'. |
| 56 |
* @type string $id ID attribute of the select element. Default is the value of $select_name. |
| 57 |
* @type string $class Class attribute of the select element. Default empty. |
| 58 |
* @type array|string $post_status Post status' to include, default publish |
| 59 |
* @type string $who Which type of posts to query. Accepts only an empty string or |
| 60 |
* 'authors'. Default empty. |
| 61 |
* } |
| 62 |
* @return string String of HTML content. |
| 63 |
*/ |
| 64 |
function wp_dropdown_posts( $args = '' ) { |
| 65 |
|
| 66 |
$defaults = array( |
| 67 |
'selected' => FALSE, |
| 68 |
'pagination' => FALSE, |
| 69 |
'posts_per_page' => - 1, |
| 70 |
'post_status' => 'publish', |
| 71 |
'cache_results' => TRUE, |
| 72 |
'cache_post_meta_cache' => TRUE, |
| 73 |
'echo' => 1, |
| 74 |
'select_name' => 'post_id', |
| 75 |
'id' => '', |
| 76 |
'class' => '', |
| 77 |
'show' => 'post_title', |
| 78 |
'show_callback' => NULL, |
| 79 |
'show_option_all' => NULL, |
| 80 |
'show_option_none' => NULL, |
| 81 |
'option_none_value' => '', |
| 82 |
'multi' => FALSE, |
| 83 |
'value_field' => 'ID', |
| 84 |
'order' => 'ASC', |
| 85 |
'orderby' => 'post_title', |
| 86 |
); |
| 87 |
|
| 88 |
$r = wp_parse_args( $args, $defaults ); |
| 89 |
|
| 90 |
$posts = get_posts( $r ); |
| 91 |
$output = ''; |
| 92 |
|
| 93 |
$show = $r['show']; |
| 94 |
|
| 95 |
if( ! empty($posts) ) { |
| 96 |
|
| 97 |
$name = esc_attr( $r['select_name'] ); |
| 98 |
|
| 99 |
if( $r['multi'] && ! $r['id'] ) { |
| 100 |
$id = ''; |
| 101 |
} else { |
| 102 |
$id = $r['id'] ? " id='" . esc_attr( $r['id'] ) . "'" : " id='$name'"; |
| 103 |
} |
| 104 |
|
| 105 |
$output = "<select name='{$name}'{$id} class='" . esc_attr( $r['class'] ) . "'>\n"; |
| 106 |
|
| 107 |
if( $r['show_option_all'] ) { |
| 108 |
$output .= "\t<option value='0'>{$r['show_option_all']}</option>\n"; |
| 109 |
} |
| 110 |
|
| 111 |
if( $r['show_option_none'] ) { |
| 112 |
$_selected = selected( $r['show_option_none'], $r['selected'], FALSE ); |
| 113 |
$output .= "\t<option value='" . esc_attr( $r['option_none_value'] ) . "'$_selected>{$r['show_option_none']}</option>\n"; |
| 114 |
} |
| 115 |
|
| 116 |
foreach( (array) $posts as $post ) { |
| 117 |
|
| 118 |
$value = ! isset($r['value_field']) || ! isset($post->{$r['value_field']}) ? $post->ID : $post->{$r['value_field']}; |
| 119 |
$_selected = selected( $value, $r['selected'], FALSE ); |
| 120 |
|
| 121 |
$display = ! empty($post->$show) ? $post->$show : sprintf( __( '#%d (no title)' ), $post->ID ); |
| 122 |
|
| 123 |
if( $r['show_callback'] ) $display = call_user_func( $r['show_callback'], $display, $post->ID ); |
| 124 |
|
| 125 |
$output .= "\t<option value='{$value}'{$_selected}>" . esc_html( $display ) . "</option>\n"; |
| 126 |
} |
| 127 |
|
| 128 |
$output .= "</select>"; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Filter the HTML output of a list of pages as a drop down. |
| 133 |
* |
| 134 |
* @since 1.0.0 |
| 135 |
* |
| 136 |
* @param string $output HTML output for drop down list of posts. |
| 137 |
* @param array $r The parsed arguments array. |
| 138 |
* @param array $posts List of WP_Post objects returned by `get_posts()` |
| 139 |
*/ |
| 140 |
$html = apply_filters( 'wp_dropdown_posts', $output, $r, $posts ); |
| 141 |
|
| 142 |
if( $r['echo'] ) { |
| 143 |
echo $html; |
| 144 |
} |
| 145 |
|
| 146 |
return $html; |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
|