Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active May 22, 2016 22:06
Show Gist options
  • Save harisrozak/f863b6149bf3e7129ceb to your computer and use it in GitHub Desktop.
Save harisrozak/f863b6149bf3e7129ceb to your computer and use it in GitHub Desktop.
WordPress :: Select dropdown by post type
<?php
function haris_lib__post_select($select_id, $post_type, $selected = 0, $limit = -1, $wc = false)
{
$post_type_object = get_post_type_object($post_type);
$label = is_null($post_type_object) ? 'No data available' : 'Select ' . $post_type_object->label;
$posts = get_posts(
array(
'post_type'=> $post_type,
'post_status'=> 'publish',
'suppress_filters' => false,
'posts_per_page'=> $limit
)
);
echo '<select name="'. $select_id .'" id="'.$select_id.'" class="select2">';
echo '<option value = "" >'.$label.' </option>';
foreach ($posts as $post)
{
if($wc) // if woocommerce
{
$billing_name = get_post_meta($post->ID, '_billing_first_name' , true);
$billing_email = get_post_meta($post->ID, '_billing_email', true);
$value = '#' . $post->ID . ' &#8722; ' . $billing_name . ' &#8722; ' . $billing_email;
}
else {
$value = $post->post_title;
}
echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $value, '</option>';
}
echo '</select>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment