Last active
May 22, 2016 22:06
-
-
Save harisrozak/f863b6149bf3e7129ceb to your computer and use it in GitHub Desktop.
WordPress :: Select dropdown by post type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 . ' − ' . $billing_name . ' − ' . $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