Relationships Between WordPress Post Types

  • 16,414 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
16,414
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
92
Comments
0
Likes
11

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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

Transcript

  • 1. Relationships BetweenCustom Post TypesRandy Hoyt
  • 2. @randyhoyt #orlandowordcampAbout Me Randy Hoyt randyhoyt.com @randyhoyt Presentation randyhoyt.com/wcorlando
  • 3. @randyhoyt #orlandowordcampCustom Post Types? Posts Pages
  • 4. @randyhoyt #orlandowordcampEvent Calendar Posts Pages Events
  • 5. @randyhoyt #orlandowordcampRestaurant Chain Posts Pages Locations
  • 6. @randyhoyt #orlandowordcampUniversity Course Posts Pages Assignments
  • 7. @randyhoyt #orlandowordcamp wp_posts ID post_type post_title 1 post Hello, world! 2 page Sample Page 231 rrh_assignment Final Exam
  • 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. @randyhoyt #orlandowordcampShopping Center Stores Posts Pages Sales Jobs
  • 10. @randyhoyt #orlandowordcampShopping Center Stores Posts Pages Sales Jobs
  • 11. @randyhoyt #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Parent Field
  • 12. @randyhoyt #orlandowordcamp1. Post Meta Field Talks Speakers Posts Pages
  • 13. @randyhoyt #orlandowordcamp
  • 14. @randyhoyt #orlandowordcampPost Meta Fields
  • 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. @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. @randyhoyt #orlandowordcamp
  • 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. @randyhoyt #orlandowordcamp
  • 20. @randyhoyt #orlandowordcamp
  • 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. @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. @randyhoyt #orlandowordcamp
  • 24. @randyhoyt #orlandowordcampTemplate file: single-rrh_talk.php
  • 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. @randyhoyt #orlandowordcampTemplate file: single-rrh_speaker.php
  • 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. @randyhoyt #orlandowordcamp
  • 29. @randyhoyt #orlandowordcamp2. Relationship Table Movies People Posts Pages
  • 30. @randyhoyt #orlandowordcamp2. Relationship Table Movies People Posts Pages
  • 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. @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. @randyhoyt #orlandowordcamp
  • 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. @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. @randyhoyt #orlandowordcampTemplate file: single-rrh_movie.php
  • 37. $connected = new WP_Query(array( connected_type => rrh_person_to_movie, connected_items => $post, nopaging => true));if ( $connected->have_posts() ) { ...}
  • 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. @randyhoyt #orlandowordcampTemplate file: single-rrh_person.php
  • 40. @randyhoyt #orlandowordcamp3. Post Parent Field Groups Posts Pages Meetings
  • 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. @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. @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. @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. @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. 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. WordPress Codex: register_post_type()http://codex.wordpress.org/Function_Reference/register_post_type
  • 48. WordPress Codex: add_submenu_page()Ahttp://codex.wordpress.org/Function_Reference/add_submenu_pagehttp://
  • 49. add_submenu()WordPress Codex: remove_menu_page()Ahttp://codex.wordpress.org/Function_Reference/remove_menu_pagehttp://
  • 50. Add_meta_boxes ()WordPress Codex: add_meta_box()Ahttp://codex.wordpress.org/Function_Reference/add_meta_boxhttp://
  • 51. Add_meta_boxes ()WordPress Codex: query_posts()Ahttp://codex.wordpress.org/Function_Reference/query_postshttp://
  • 52. Add_meta_boxes ()WordPress Admin and ThickboxA<a class="thickbox" … >http://
  • 53. Iframe with query string parametersPost ID parameter is left blank for adding a new meeting …
  • 54. wp_insert_post()Iframe with query string parameters… but post ID parameter is set for editing an existing meeting.
  • 55. wp_insert_post()WordPress Codex: wp_insert_post()http://codex.wordpress.org/Function_Reference/wp_insert_post
  • 56. wp_insert_post()WordPress Codex: wp_trash_post()http://codex.wordpress.org/Function_Reference/wp_trash_post
  • 57. JavaScript function in pageReturn list of meetings to the Edit Group page
  • 58. <script> function list_meetings(display) { ... } JavaScript function in page</script>Create JavaScript function on the Edit Group page
  • 59. <script> var display = ... var win = window.dialogArguments → || opener || parent || top; win.list_meetings(display);</script>Call that JavaScript function from the iframe
  • 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. JavaScript function in page
  • 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. 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. @randyhoyt #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Parent Field
  • 65. Relationships BetweenCustom Post TypesRandy Hoyt randyhoyt.com @randyhoytPresentation randyhoyt.com/wcorlando