Ask your WordPress questions! Pay money and get answers fast! (more info)

Select Custom Post Type in Metabox

  • SOLVED

I need to complete this script


echo '<select id="corso_id" name="corso_id" >';
echo '<option value="1"> Corso 1 </option>';
echo '<option value="2"> Corso 2 </option>';
echo '</select>';



The Option value should be dinamic by post type 'corsi'

Answers (2)

2016-04-24

Rempty answers:

Hello Manlio Ma

Do you want get all posts of the post type corsi? you can use this code:


echo '<select id="corso_id" name="corso_id" >';
$args=array(
'post_type'=>'corsi',
'post_per_page'=>-1
);
$corsi=new WP_Query($args);
if($corsi->have_posts()):
while($corsi->have_posts()):
$corsi->the_post();
echo '<option value="'.get_the_ID().'">'.get_the_title().'</option>';
endwhile;
endif;
echo '</select>';


Manlio Ma comments:

perfect!, and how if I have to upload an image ?

2016-04-24

Arnav Joy answers:


<?php
$posts = get_posts( array( 'post_type' => 'corsi' , 'posts_per_page' => -1) );




echo '<select id="corso_id" name="corso_id" >';

foreach( $posts as $pd ){
?>
<option value="<?php echo $pd->ID; ?>"> <?php echo $pd->post_title; ?> </option>
<?php } ?>

</select>