2

I'm working for a WordPress Plugin. The plugins is for library management, for my university. I created custom post types with taxonomies for keeping books information stored. Now I want to create another post type which will keep records for books and and users. For instance, I'll create new post type called "Issue". There should have three fields. One would be title. Another two will be dropdown, one will show all book (I've already created custom post type called books, I want to use those here, automatically) lists as dropdown. Another dropdown field will show list of students (for this I've created a new user role called student, I want to use those user who are registered as student).

So my questions are

  1. How can I show all users (selected role) as dropdown in custom meta box and
  2. How can I show all books (Custom post type I created) as dropdown in custom meta box

How can these be done? I can code, need instruction ;)

3
  • 2
    Can you separate things out a little more so it isn't a single giant paragraph? Use bullet points for extra points
    – Tom J Nowell
    Apr 16, 2012 at 15:45
  • 1
    This sounds like several questions in one, as I think will become evident, once you implement @TomJNowell's suggestion. Apr 16, 2012 at 16:01
  • Chip, just tried to do :)
    – IFightCode
    Apr 16, 2012 at 17:07

2 Answers 2

4
  • To get the users who have the role called student, use the WP_User_Query object
  • For storing the fields, use post meta just as you would on normal posts, remember to declare that your custom post type supports custom fields when being registered
  • For your dropdown/etc you will need to register metaboxes

More info on WP_User_Query.

For how to add metaboxes see the Codex entry and examples for this function.

3
  • Thanks Tom. Now need idea how to get custom post type list as dropdown :)
    – IFightCode
    Apr 16, 2012 at 15:54
  • Just use WP_Query like you would on the frontend, there are plenty of questions on this site asking how to grab custom post types, I advise you use the searchbox!
    – Tom J Nowell
    Apr 16, 2012 at 16:16
  • I know customizing loop, custom post type can be displayed, but have no idea how to use than in a meta box. Anyway, I'll try now definitely. Thanks a lot Tom.
    – IFightCode
    Apr 16, 2012 at 17:01
0

Here is the code I used to create a dropdown of my Custom Post Type "Issues" to display the title of the "Issue" and have the value link to the single issue.

  <label>Published In:</label>

  <p><select name="issue-date">
  <?php 
  $args = array( 'post_type' => 'issues');
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post();

  echo '<option value="';
  the_permalink();
  echo '">';
  the_title();
  echo '</option>';
  endwhile;
  ?>

  </p>

Hope it helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.