Relationships Between WordPress Post Types
Upcoming SlideShare
Loading in...5
×
 

Relationships Between WordPress Post Types

on

  • 6,654 views

 

Statistics

Views

Total Views
6,654
Views on SlideShare
6,645
Embed Views
9

Actions

Likes
8
Downloads
86
Comments
0

2 Embeds 9

http://bundlr.com 6
https://twitter.com 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 Relationships Between WordPress Post Types Presentation Transcript

  • Relationships BetweenCustom Post TypesRandy Hoyt
  • @randyhoyt #orlandowordcampAbout Me Randy Hoyt randyhoyt.com @randyhoyt Presentation randyhoyt.com/wcorlando
  • @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 post Hello, world! 2 page Sample Page 231 rrh_assignment Final Exam
  • @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
  • @randyhoyt #orlandowordcampShopping Center Stores Posts Pages Sales Jobs
  • @randyhoyt #orlandowordcampShopping Center Stores Posts Pages Sales Jobs
  • @randyhoyt #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Parent Field
  • @randyhoyt #orlandowordcamp1. Post Meta Field Talks Speakers Posts Pages
  • @randyhoyt #orlandowordcamp
  • @randyhoyt #orlandowordcampPost Meta Fields
  • @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
  • @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
  • @randyhoyt #orlandowordcamp
  • @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.
  • @randyhoyt #orlandowordcamp
  • @randyhoyt #orlandowordcamp
  • @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 ), ),);
  • @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();
  • @randyhoyt #orlandowordcamp
  • @randyhoyt #orlandowordcampTemplate file: single-rrh_talk.php
  • 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);
  • @randyhoyt #orlandowordcampTemplate file: single-rrh_speaker.php
  • 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; }
  • @randyhoyt #orlandowordcamp
  • @randyhoyt #orlandowordcamp2. Relationship Table Movies People Posts Pages
  • @randyhoyt #orlandowordcamp2. Relationship Table Movies People Posts Pages
  • @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
  • @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.
  • @randyhoyt #orlandowordcamp
  • 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 ) ),));
  • @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
  • @randyhoyt #orlandowordcampTemplate file: single-rrh_movie.php
  • $connected = new WP_Query(array( connected_type => rrh_person_to_movie, connected_items => $post, nopaging => true));if ( $connected->have_posts() ) { ...}
  • if ( $connected->have_posts() ) { while ( $connected->have_posts() ) : $connected->the_post(); echo <li><a href="; the_permalink(); echo ">; the_title(); echo </a></li>; endwhile;}
  • @randyhoyt #orlandowordcampTemplate file: single-rrh_person.php
  • @randyhoyt #orlandowordcamp3. Post Parent Field Groups Posts Pages Meetings
  • @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
  • @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
  • @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/
  • @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.
  • @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) ;
  • Register meetings as a regular post typeregister_post_type(meeting, $args)Register meetings as a subordinate post typeregister_sub_post_type(meeting, $args, group)
  • 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 JavaScript function on the Edit Group page
  • <script> var display = ... var win = window.dialogArguments → || opener || parent || top; win.list_meetings(display);</script>Call that JavaScript function from the iframe
  • <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
  • JavaScript function in page
  • Register meetings as a regular post typeregister_post_type(meeting, $args)Register meetings as a subordinate post typeregister_sub_post_type(meeting, $args, group)
  • 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; }
  • @randyhoyt #orlandowordcampStoring Relationships1. Post Meta Field2. Custom Relationship Table3. Post Parent Field
  • Relationships BetweenCustom Post TypesRandy Hoyt randyhoyt.com @randyhoytPresentation randyhoyt.com/wcorlando