Tharindu Thisarasinghe
Tharindu Thisarasinghe

Reputation: 3998

Show a list of titles of all the custom posts in backend

In my Wordpress theme, there's a custom post type called "slider".

So, I need to get all the sliders to a select box in my Theme Options page. So, that the user can select which slider he wants to display.

How to get this done ?

Upvotes: 0

Views: 28

Answers (1)

Tharindu Thisarasinghe
Tharindu Thisarasinghe

Reputation: 3998

Never mind, I found it myself

<?php 
    $type = 'sliders'; // Whatever the 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);

//Check if the there are posts                                    
if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
       <select>
         <option><?php the_title(); ?></option>
       </select>
<?php
      endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().

?>

Upvotes: 1

Related Questions