72
Relationships Between Custom Post Types Randy Hoyt

Relationships Between WordPress Post Types

Embed Size (px)

Citation preview

  • 1.Relationships BetweenCustom Post TypesRandy Hoyt

2. @randyhoyt #orlandowordcampAbout MeRandy Hoytrandyhoyt.com@randyhoyt Presentation randyhoyt.com/wcorlando 3. @randyhoyt #orlandowordcampCustom Post Types?PostsPages 4. @randyhoyt #orlandowordcampEvent CalendarPostsPagesEvents 5. @randyhoyt #orlandowordcampRestaurant ChainPostsPages Locations 6. @randyhoyt #orlandowordcampUniversity CoursePostsPagesAssignments 7. @randyhoyt#orlandowordcamp wp_posts ID post_type post_title1 postHello, world!2page Sample Page 231 rrh_assignment Final Exam 8. @randyhoyt#orlandowordcamp wp_posts ID post_type post_title1 postHello, world!2page Sample Page 231 rrh_assignment Final ExamStructured Data in WordPresshttp://randyhoyt.com/wcto 9. @randyhoyt #orlandowordcampShopping CenterStoresPostsPagesSales Jobs 10. @randyhoyt #orlandowordcampShopping CenterStoresPostsPagesSales Jobs 11. @randyhoyt #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Parent Field 12. @randyhoyt #orlandowordcamp1. Post Meta Field TalksSpeakersPostsPages 13. @randyhoyt #orlandowordcamp 14. @randyhoyt #orlandowordcampPost Meta Fields 15. @randyhoyt#orlandowordcampPost Meta Fields wp_postswp_postmeta ID post_type post_title post_id meta_keymeta_value 37 rrh_speaker Randy Hoyt 37 38job_title speaker Teacher 37 38 rrh_talkCustom Post Type 37company TreehouseRelationships 37twitter randyhoyt 16. @randyhoyt#orlandowordcampPost Meta Fields wp_postswp_postmeta ID post_type post_title post_id meta_keymeta_value 37 rrh_speaker Randy Hoyt 37 38job_title speaker Teacher 37 38 rrh_talkCustom Post Type 37company TreehouseRelationships 37twitter randyhoyt 38speaker 37 17. @randyhoyt #orlandowordcamp 18. @randyhoyt #orlandowordcampLibrary: Custom Meta Boxeshttp://r2h.me/wpcmbCustom Meta Boxes allow you to build clean, understandableinterfaces for entering metadata. Theres 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 => -1post_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 titleecho $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 titleforeach($talks as $talk) { echo $talk->post_title;} 28. @randyhoyt #orlandowordcamp 29. @randyhoyt#orlandowordcamp2. Relationship Table MoviesPeoplePostsPages 30. @randyhoyt#orlandowordcamp2. Relationship Table MoviesPeoplePostsPages 31. @randyhoyt #orlandowordcampCustom Relationship Table wp_posts wp_postmeta ID post_type post_titleto_idpost_idfrom_id meta_key charactermeta_value 37 movie Star Wars 3738 38 speakerHan37 Solo 38 personHarrison 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 personHarrison Ford 38 movie Star Wars wp_p2p wp_p2pmeta p2p_id to_id from_id typep2p_id meta_keymeta_value 13738person_to_movie 1roleLead Actor1character 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 ;the_title();echo ;endwhile;} 39. @randyhoyt #orlandowordcampTemplate file: single-rrh_person.php 40. @randyhoyt #orlandowordcamp3. Post Parent Field GroupsPostsPages Meetings 41. @randyhoyt#orlandowordcamp wp_postsID post_type post_titlepost_parent 2 pageAbout0231pageLocation & Venue 2256pageDeveloper Hack Day 2347pageContact2 42. @randyhoyt #orlandowordcamp wp_postsID post_typepost_titlepost_parent 2pageAbout0156revision About2157revision About2158revision About2 43. @randyhoyt #orlandowordcamp wp_postsID post_typepost_titlepost_parent15 forumBook Discussions 078 topicFavorite Tolkien Characters? 15123replyReply 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 ThickboxAhttp:// 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. Create JavaScript function on the Edit Group page 59. Call that JavaScript function from the iframe 60. 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 titleforeach($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