Relationships BetweenCustom Post TypesRandy Hoyt
@randyhoyt                               #orlandowordcampAbout Me              Randy Hoyt    randyhoyt.com                ...
@randyhoyt           #orlandowordcampCustom Post Types?    Posts    Pages
@randyhoyt           #orlandowordcampEvent Calendar    Posts    Pages          Events
@randyhoyt           #orlandowordcampRestaurant Chain    Posts    Pages         Locations
@randyhoyt           #orlandowordcampUniversity Course    Posts    Pages        Assignments
@randyhoyt                                    #orlandowordcamp wp_posts   ID         post_type       post_title    1      ...
@randyhoyt                                    #orlandowordcamp wp_posts   ID         post_type       post_title    1      ...
@randyhoyt           #orlandowordcampShopping Center                              Stores    Posts    Pages                ...
@randyhoyt           #orlandowordcampShopping Center                              Stores    Posts    Pages                ...
@randyhoyt                 #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Pare...
@randyhoyt           #orlandowordcamp1. Post Meta Field                                 Talks                        Speak...
@randyhoyt   #orlandowordcamp
@randyhoyt         #orlandowordcampPost Meta Fields
@randyhoyt                                            #orlandowordcampPost Meta Fields wp_posts                           ...
@randyhoyt                                            #orlandowordcampPost Meta Fields wp_posts                           ...
@randyhoyt   #orlandowordcamp
@randyhoyt                                         #orlandowordcampLibrary: Custom Meta Boxeshttp://r2h.me/wpcmbCustom Met...
@randyhoyt   #orlandowordcamp
@randyhoyt   #orlandowordcamp
@randyhoyt                        #orlandowordcamp$meta_boxes[] = array(    id => rrh_talk_data,    title => Talk Informat...
@randyhoyt                         #orlandowordcamp$posts = query_posts( array(    posts_per_page => -1    post_type => rr...
@randyhoyt   #orlandowordcamp
@randyhoyt                           #orlandowordcampTemplate file: single-rrh_talk.php
Retrieve speaker ID from custom field  $speaker_id = get_post_meta($post->ID,                                   "speaker",...
@randyhoyt                              #orlandowordcampTemplate file: single-rrh_speaker.php
Retrieve talks with speaker ID in custom field    $talks = get_posts( array(            post_type => rrh_talk,            ...
@randyhoyt   #orlandowordcamp
@randyhoyt              #orlandowordcamp2. Relationship Table                                   Movies                    ...
@randyhoyt              #orlandowordcamp2. Relationship Table                                   Movies                    ...
@randyhoyt                                         #orlandowordcampCustom Relationship Table wp_posts                     ...
@randyhoyt                                       #orlandowordcampPlugin: Posts 2 Postshttp://r2h.me/p2pThis plugin provide...
@randyhoyt   #orlandowordcamp
p2p_register_connection_type( array(        name => rrh_person_to_movie’,        from => rrh_person,        to => rrh_movi...
@randyhoyt                                                       #orlandowordcampCustom Relationship Table wp_posts ID    ...
@randyhoyt                            #orlandowordcampTemplate file: single-rrh_movie.php
$connected = new WP_Query(array(    connected_type => rrh_person_to_movie,    connected_items => $post,    nopaging => tru...
if ( $connected->have_posts() ) {    while ( $connected->have_posts() ) :        $connected->the_post();        echo <li><...
@randyhoyt                             #orlandowordcampTemplate file: single-rrh_person.php
@randyhoyt             #orlandowordcamp3. Post Parent Field                           Groups    Posts    Pages            ...
@randyhoyt                                #orlandowordcamp wp_posts  ID     post_type   post_title                post_par...
@randyhoyt                           #orlandowordcamp wp_posts  ID     post_type      post_title        post_parent   2   ...
@randyhoyt                                           #orlandowordcamp wp_posts  ID     post_type    post_title            ...
@randyhoyt                                       #orlandowordcampSubordinate Post Type Helpershttp://wordpress.org/extend/...
@randyhoyt                                       #orlandowordcampSubordinate Post Type Helpershttp://wordpress.org/extend/...
Register meetings as a regular post typeregister_post_type(meeting, $args)Register meetings as a subordinate post typeregi...
WordPress Codex: register_post_type()http://codex.wordpress.org/Function_Reference/register_post_type
WordPress Codex: add_submenu_page()Ahttp://codex.wordpress.org/Function_Reference/add_submenu_pagehttp://
add_submenu()WordPress Codex: remove_menu_page()Ahttp://codex.wordpress.org/Function_Reference/remove_menu_pagehttp://
Add_meta_boxes ()WordPress Codex: add_meta_box()Ahttp://codex.wordpress.org/Function_Reference/add_meta_boxhttp://
Add_meta_boxes ()WordPress Codex: query_posts()Ahttp://codex.wordpress.org/Function_Reference/query_postshttp://
Add_meta_boxes ()WordPress Admin and ThickboxA<a class="thickbox" … >http://
Iframe with query string parametersPost ID parameter is left blank for adding a new meeting …
wp_insert_post()Iframe with query string parameters… but post ID parameter is set for editing an existing meeting.
wp_insert_post()WordPress Codex: wp_insert_post()http://codex.wordpress.org/Function_Reference/wp_insert_post
wp_insert_post()WordPress Codex: wp_trash_post()http://codex.wordpress.org/Function_Reference/wp_trash_post
JavaScript function in pageReturn list of meetings to the Edit Group page
<script>     function list_meetings(display)     {         ...     }         JavaScript function in page</script>Create Ja...
<script>     var display = ...     var win = window.dialogArguments           →                   || opener || parent || t...
<script>     function list_meetings(display)     {          jQuery(#list_meetings).empty()          →          JavaScript ...
JavaScript function in page
Register meetings as a regular post typeregister_post_type(meeting, $args)Register meetings as a subordinate post typeregi...
Retrieve meetings for a group    $meetings = get_posts( array(            post_type => mythsoc_meeting,            post_pa...
@randyhoyt                 #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Pare...
Relationships BetweenCustom Post TypesRandy Hoyt     randyhoyt.com               @randyhoytPresentation   randyhoyt.com/wc...
Relationships Between WordPress Post Types
Relationships Between WordPress Post Types
Relationships Between WordPress Post Types
Relationships Between WordPress Post Types
Relationships Between WordPress Post Types
Relationships Between WordPress Post Types
Relationships Between WordPress Post Types
Upcoming SlideShare
Loading in …5
×

Relationships Between WordPress Post Types

19,785 views

Published on

0 Comments
13 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
19,785
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
129
Comments
0
Likes
13
Embeds 0
No embeds

No notes for slide
  • Finish 2:00
  • NEXT: Man with phone
  • NEXT: Man with phone
  • NEXT: Man with phone
  • Mention prefix
  • Finish 21:00
  • Finish 21:00
  • Mention prefix
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • NEXT:WordPress 0.7
  • The WordPress API doesn’t give you the ability to do this. It requires some custom code in a plugin.
  • NEXT:WordPress 0.7
  • Finish 21:00
  • Finish 21:00
  • Finish 21:00
  • Relationships Between WordPress Post Types

    1. 1. Relationships BetweenCustom Post TypesRandy Hoyt
    2. 2. @randyhoyt #orlandowordcampAbout Me Randy Hoyt randyhoyt.com @randyhoyt Presentation randyhoyt.com/wcorlando
    3. 3. @randyhoyt #orlandowordcampCustom Post Types? Posts Pages
    4. 4. @randyhoyt #orlandowordcampEvent Calendar Posts Pages Events
    5. 5. @randyhoyt #orlandowordcampRestaurant Chain Posts Pages Locations
    6. 6. @randyhoyt #orlandowordcampUniversity Course Posts Pages Assignments
    7. 7. @randyhoyt #orlandowordcamp wp_posts ID post_type post_title 1 post Hello, world! 2 page Sample Page 231 rrh_assignment Final Exam
    8. 8. @randyhoyt #orlandowordcamp wp_posts ID post_type post_title 1 post Hello, world! 2 page Sample Page 231 rrh_assignment Final ExamStructured Data in WordPresshttp://randyhoyt.com/wcto
    9. 9. @randyhoyt #orlandowordcampShopping Center Stores Posts Pages Sales Jobs
    10. 10. @randyhoyt #orlandowordcampShopping Center Stores Posts Pages Sales Jobs
    11. 11. @randyhoyt #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Parent Field
    12. 12. @randyhoyt #orlandowordcamp1. Post Meta Field Talks Speakers Posts Pages
    13. 13. @randyhoyt #orlandowordcamp
    14. 14. @randyhoyt #orlandowordcampPost Meta Fields
    15. 15. @randyhoyt #orlandowordcampPost Meta Fields wp_posts wp_postmeta ID post_type post_title post_id meta_key meta_value 37 rrh_speaker Randy Hoyt 37 38 job_title speaker Teacher 37 38 rrh_talk Custom Post Type 37 company Treehouse Relationships 37 twitter randyhoyt
    16. 16. @randyhoyt #orlandowordcampPost Meta Fields wp_posts wp_postmeta ID post_type post_title post_id meta_key meta_value 37 rrh_speaker Randy Hoyt 37 38 job_title speaker Teacher 37 38 rrh_talk Custom Post Type 37 company Treehouse Relationships 37 twitter randyhoyt 38 speaker 37
    17. 17. @randyhoyt #orlandowordcamp
    18. 18. @randyhoyt #orlandowordcampLibrary: Custom Meta Boxeshttp://r2h.me/wpcmbCustom Meta Boxes allow you to build clean, understandableinterfaces for entering metadata. There’s a ton of field options:text, textarea, checkbox, dropdown list, WYSIWYG, and more.
    19. 19. @randyhoyt #orlandowordcamp
    20. 20. @randyhoyt #orlandowordcamp
    21. 21. @randyhoyt #orlandowordcamp$meta_boxes[] = array( id => rrh_talk_data, title => Talk Information, pages => array(rrh_talk), context => normal, priority => high, show_names => true, fields => array( array( name => Speaker, id => speaker, type => select, options => $speakers ), ),);
    22. 22. @randyhoyt #orlandowordcamp$posts = query_posts( array( posts_per_page => -1 post_type => rrh_speaker));foreach ($posts as $post) { $speakers[] = array( name => $post->post_title, value => $post->ID );}wp_reset_query();
    23. 23. @randyhoyt #orlandowordcamp
    24. 24. @randyhoyt #orlandowordcampTemplate file: single-rrh_talk.php
    25. 25. Retrieve speaker ID from custom field $speaker_id = get_post_meta($post->ID, "speaker", true);Retrieve speaker info from ID $speaker = get_post($speaker_id);Display speaker title echo $speaker->post_title;Retrieve link to speaker from ID $speaker_link = get_permalink($speaker->ID);
    26. 26. @randyhoyt #orlandowordcampTemplate file: single-rrh_speaker.php
    27. 27. Retrieve talks with speaker ID in custom field $talks = get_posts( array( post_type => rrh_talk, meta_key => speaker, meta_value => $post->ID ));Loop through all the talks; display talk title foreach($talks as $talk) { echo $talk->post_title; }
    28. 28. @randyhoyt #orlandowordcamp
    29. 29. @randyhoyt #orlandowordcamp2. Relationship Table Movies People Posts Pages
    30. 30. @randyhoyt #orlandowordcamp2. Relationship Table Movies People Posts Pages
    31. 31. @randyhoyt #orlandowordcampCustom Relationship Table wp_posts <relationship> wp_postmeta ID post_type post_title to_id post_id from_id meta_key character meta_value 37 movie Star Wars 37 38 38 speaker Han 37 Solo 38 person Harrison Ford
    32. 32. @randyhoyt #orlandowordcampPlugin: Posts 2 Postshttp://r2h.me/p2pThis plugin provides functions developers can use to createmany-to-many relationships between posts of any type:posts, pages, and custom post types.
    33. 33. @randyhoyt #orlandowordcamp
    34. 34. p2p_register_connection_type( array( name => rrh_person_to_movie’, from => rrh_person, to => rrh_movie, fields => array( role => array( title => Role, type => select, values => array(Director ...) ), character => array( title => Character, type => text ) ),));
    35. 35. @randyhoyt #orlandowordcampCustom Relationship Table wp_posts ID post_type post_title 37 person Harrison Ford 38 movie Star Wars wp_p2p wp_p2pmeta p2p_id to_id from_id type p2p_id meta_key meta_value 1 37 38 person_to_movie 1 role Lead Actor 1 character Han Solo
    36. 36. @randyhoyt #orlandowordcampTemplate file: single-rrh_movie.php
    37. 37. $connected = new WP_Query(array( connected_type => rrh_person_to_movie, connected_items => $post, nopaging => true));if ( $connected->have_posts() ) { ...}
    38. 38. if ( $connected->have_posts() ) { while ( $connected->have_posts() ) : $connected->the_post(); echo <li><a href="; the_permalink(); echo ">; the_title(); echo </a></li>; endwhile;}
    39. 39. @randyhoyt #orlandowordcampTemplate file: single-rrh_person.php
    40. 40. @randyhoyt #orlandowordcamp3. Post Parent Field Groups Posts Pages Meetings
    41. 41. @randyhoyt #orlandowordcamp wp_posts ID post_type post_title post_parent 2 page About 0 231 page Location & Venue 2 256 page Developer Hack Day 2 347 page Contact 2
    42. 42. @randyhoyt #orlandowordcamp wp_posts ID post_type post_title post_parent 2 page About 0 156 revision About 2 157 revision About 2 158 revision About 2
    43. 43. @randyhoyt #orlandowordcamp wp_posts ID post_type post_title post_parent 15 forum Book Discussions 0 78 topic Favorite Tolkien Characters? 15 123 reply Reply To: Favorite Tolkien … 78bbPresshttp://bbpress.org/
    44. 44. @randyhoyt #orlandowordcampSubordinate Post Type Helpershttp://wordpress.org/extend/plugins/subordinate-post-type-helpers/This plugin provides a number of helpers functions to aid in thecreation of subordinate relationships between two post types,one-to-many relationships in which posts of one type are childrenof another.
    45. 45. @randyhoyt #orlandowordcampSubordinate Post Type Helpershttp://wordpress.org/extend/plugins/subordinate-post-type-helpers/This plugin provides a number of helpers functions to aid in thecreation of subordinate relationships between two post types, one-to-many relationships in which posts of one type are children ofanother.register_sub_post_type(meeting, $args, group) ;
    46. 46. Register meetings as a regular post typeregister_post_type(meeting, $args)Register meetings as a subordinate post typeregister_sub_post_type(meeting, $args, group)
    47. 47. WordPress Codex: register_post_type()http://codex.wordpress.org/Function_Reference/register_post_type
    48. 48. WordPress Codex: add_submenu_page()Ahttp://codex.wordpress.org/Function_Reference/add_submenu_pagehttp://
    49. 49. add_submenu()WordPress Codex: remove_menu_page()Ahttp://codex.wordpress.org/Function_Reference/remove_menu_pagehttp://
    50. 50. Add_meta_boxes ()WordPress Codex: add_meta_box()Ahttp://codex.wordpress.org/Function_Reference/add_meta_boxhttp://
    51. 51. Add_meta_boxes ()WordPress Codex: query_posts()Ahttp://codex.wordpress.org/Function_Reference/query_postshttp://
    52. 52. Add_meta_boxes ()WordPress Admin and ThickboxA<a class="thickbox" … >http://
    53. 53. Iframe with query string parametersPost ID parameter is left blank for adding a new meeting …
    54. 54. wp_insert_post()Iframe with query string parameters… but post ID parameter is set for editing an existing meeting.
    55. 55. wp_insert_post()WordPress Codex: wp_insert_post()http://codex.wordpress.org/Function_Reference/wp_insert_post
    56. 56. wp_insert_post()WordPress Codex: wp_trash_post()http://codex.wordpress.org/Function_Reference/wp_trash_post
    57. 57. JavaScript function in pageReturn list of meetings to the Edit Group page
    58. 58. <script> function list_meetings(display) { ... } JavaScript function in page</script>Create JavaScript function on the Edit Group page
    59. 59. <script> var display = ... var win = window.dialogArguments → || opener || parent || top; win.list_meetings(display);</script>Call that JavaScript function from the iframe
    60. 60. <script> function list_meetings(display) { jQuery(#list_meetings).empty() → JavaScript function .append(display); in page tb_remove(); }</script>That function inserts the list into the Edit Group page
    61. 61. JavaScript function in page
    62. 62. Register meetings as a regular post typeregister_post_type(meeting, $args)Register meetings as a subordinate post typeregister_sub_post_type(meeting, $args, group)
    63. 63. Retrieve meetings for a group $meetings = get_posts( array( post_type => mythsoc_meeting, post_parent => $post->ID ));Loop through all the meetings; display meeting title foreach($meetings as $meeting) { echo $meeting->post_title; }
    64. 64. @randyhoyt #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Parent Field
    65. 65. Relationships BetweenCustom Post TypesRandy Hoyt randyhoyt.com @randyhoytPresentation randyhoyt.com/wcorlando

    ×