In our training site we have an Artist post type and the Event post type we have seen in previous steps.

We want to create a many-to-many relationship between Artists and Events. An artist can appear at many events, and an event may feature many artists.

Learn by doing

Recall from our conceptual discussion of many-to-many relationships that we need an intermediate post type to connect them. In this case “Appearances” will serve as the intermediary. An artist can appear at various events, and an event features appearances by various artists.

Create a many-to-many relationship via Appearances

  • The appearance post type is already registered. Go to the Toolset -> Post Types page and edit it
  • Scroll down to the Posts Relationships meta box
  • Specify that both Artist and Venue post types will be parents of appearances and save

We have connected the post types, now we need to join the individual posts themselves.

Use appearance posts to connect artists and events

Well-known pop idol Harry Weaver will appear at the Music on the Streets event.

  • Create a new appearance post at Appearances -> Add New
  • Give the appearance a title. It could be anything, but a title such as “Harry Weaver at Music on the Streets” means you can later see at a glance what the post is for
  • Save the post. You cannot set post relationships until the post has been saved
  • In the Post Relationship meta box specify the parent artist (Harry Weaver) and the parent event (Music on the Streets), and save again
Joining an artist and event with intermediate post

Artists can appear at many events and events can host many artists, so let’s add a few more appearances. Repeat the above steps to create appearances for:

  • Harold Turner at Music on the Streets
  • Harold Turner at Rock Retrospective
  • Harry Weaver at Annual All-Stars

Now that we have joined the posts together, we might want to display those connections by showing the artists that appear at an event, and the events where an artist is playing.

Display artists appearing at an event

We use Views to create lists of content, and we will need to add a View that outputs artists to our template for events. The View will actually retrieve appearance posts, but we’ll use the id attribute in the Loop Output section to display content from the parent artist post rather than the appearance post itself. (This is the same technique used to show the venue name on event posts in the previous section.)

  • From the Toolset -> Dashboard page edit the Layout for Events (or edit it from the list of layouts at Toolset -> Layouts)
  • Add a new row
  • Insert a Visual Editor cell with an h2 heading “Artists appearing at this event”
  • Add a new row
  • Insert a View cell which creates a new View that displays all results
  • It is helpful to give the View a name (such as “Artists appearing at an Event”)
  • In Content Selection choose Appearances
  • Add a Query Filter to filter by Post relationship
  • From the options choose Select posts that are children of the Post where this View is shown
  • In the Loop Output Editor use the Loop Wizard to add the fields from the artist we want to display
  • We could display artists in a grid with their thumbnails, but for the purposes of this tutorial we will simply list the artist names as links to their own page.

  • Choose Unformatted for the loop display
  • Add the field Post title with a link
  • Before finishing the wizard use the Edit button to bring up the options dialog for this field
  • Switch to the Post selection tab and specify we want to display data for the parent of the current post in another post type, set by Types relationship, namely Artist
  • Finish the wizard
  • This will insert the wpv-post-link shortcode with the id attribute that specifies we want to display the title of the parent artist post for this appearance, like so:

    [wpv-post-link id="$artist"]

  • Add p tags around this shortcode so that artists appear on their own line
  • Save and close the View
  • Save and close the Template Layout

Now if you View the Music on the Streets event on the front end you should see Harry Weaver and Harold Turner listed as performers.

Artists appearing at an event

You would probably want to make the output visually more appealing with a thumbnail, at least, for example.

Display events at which an artist appears

The process for displaying the events where they appear on an artist page is the same as above, we just approach the relationship from the other direction.

You would

  • Create a Template Layout for Artists
  • Add content about the artist, such as their artist image, as desired
  • Add a View cell which queries appearances posts using a Query Filter to show appearances that are child posts of the post where this View is shown (namely, the artist being shown)
  • Add the attribute id="$event" to the shortcodes used to output fields from the parent event of these appearances

Need help?

Go to the Beyond the Basics support page and leave a comment. Nigel, the author of these pages, or one of the other Toolset team members will reply to you.

What’s next?

Step 6Leverage theme styles and add new CSS