user3035153
user3035153

Reputation: 11

how to display custom posts types by title in a drop down menu in wordpress

i want to add a dropdown with the custom posts title, basically the code iam using shows default posts in a dropdown by title but i want to display a custom post type in a drop down any help plz here is my code

<select name="archive-dropdown"    
onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Location' ) ); ?></option> 
<?php wp_get_archives( array( 'type' => 'postbypost', 'format' => 'option',  
'show_post_count' => 1) ); ?>
 </select>  

Upvotes: 0

Views: 3674

Answers (1)

Anju Aravind
Anju Aravind

Reputation: 3362

try this

<?php
$type = 'products'; // your post type
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <option><?=the_title(); ?></option>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

Upvotes: 1

Related Questions