Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I run a video game news blog. When my staff writes a news article or a review, I would like them to be able to "assign" that post to a game (if applicable), so then I could have the single.php template call information on that game (title, boxart, publisher, release date, etc.) and display it in the sidebar. I thought this might involve a custom taxonomy so that it could be assigned to a post just like tags are.

But then I realized I also wanted those games to have full pages loaded with information. I initially thought to create a custom post type for pages, and using custom fields, I could call the game's box art, show its release date, display any posts or reviews on that game and displaying them with brief excerpts, and using custom taxonomies to display the game's publisher, developer, etc...

And THEN I realized I also wanted those publishers and developers to have their own pages as well. Description, location, links to articles involving them, lists of any games attributed to them... and so on.

None of these can be child pages of the other, since developers aren't always permanently tied to any particular publisher, games are sometimes co-developed by multiple parties, etc.

I'm in a pickle, and can't figure out how to re-organize my site. I really want to use all the new toys in WordPress to make the site I want, rather than WP limiting what I can do. Any suggestions? I've been brainstorming for days trying to figure something out. Any suggestions would be appreciated.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

There are a few choices to go with this. I've actually gone through the same though process you have been. It would be easy if Wordpress let you link posts or custom post types together but in lieu of that, the hackery of Wordpress developers must occur!

The solution I went with in the plugin I'm working on is a bit hacky, but it works for me. Basically in my plugin, I wanted to have a post type called series that holds info on a particular tv series, a post type for episodes and another post type for DVD and Blu-ray releases. I wanted both the episode and releases post types to reference the series type they come from, so if I create a single series post page, it could list the episodes and releases as well as the series info.

My solution involves a custom field that holds the object id of the series. The first way to accomplish this was to create a metabox that had a select box of all the posts of type series:

public function get_select_array($post_type) {
    global $wpdb;
    $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = '$post_type' AND post_status = 'publish' ORDER BY post_title";
    $results = $wpdb->get_results($query, OBJECT);
    $series = array();
    foreach ($results as $result) {
        $series[] = array('name' => $result->post_title, 'value' => $result->ID);
    }
    return $series;                 
}

The function get_select_array() basically takes a custom post type and returns the post titles and ID's of all the ones that are published in an array. I then use that array to populate a select box. In my plugin, I'm using the metabox creation class created by Rilwis but I'll post some code for you to use (code is adapted from Rilwis's code):

add_meta_box('parent_series', 'Series', 'show_series_metabox', 'episode', 'side', 'high');  //add a side metabox
function show_series_metabox() {
    global $post;
    echo '<input type="hidden" name="wp_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';  //nonce
    $series = get_select_array('series'); //get post titles and ID's of post type 'series
    $meta = get_post_meta($post->ID, 'parent_series', true); //get the meta value of the custom field
    echo '<select name="parent_series" id="parent_series">';//create the select box
    foreach ($series as $s) {
        echo '<option value="', $s['value'], '"', $meta == $s['value'] ? ' selected="selected"' : '', '>', $s['name'], '</option>';
    }
    echo '</select>';
}
add_action('save_post', 'save_series_metabox');
function save_series_metabox() {
    $real_post_id = isset($_POST['post_ID']) ? $_POST['post_ID'] : NULL ;   //get post id
    $old = get_post_meta($real_post_id, 'parent_series', true);             //get old stored meta value
    $new = $_POST['parent_series'];                                         //new meta value
    if ($new && $new != $old) {                                             //saving or deleting value
        update_post_meta($real_post_id, 'parent_series', $new);
    } elseif ('' == $new && $old) {
        delete_post_meta($real_post_id, 'parent_series', $old);
    }
}

This will add a metabox that has a select that lists all the series available. When the post is saved, the id of that series will be saved in a custom field called 'parent_series'. Once that is done, you can call on that id and use it to call the series info via a custom query. Or its possible to do the opposite: on the series page, you do a query for all the episode pages that have the same value in their custom field/meta data.

And there you have it. Using this code you can link posts of one type to those of another, making them work like pseudo taxonomies in a way. I edited the code up pretty quickly so if you find any errors or bugs, let me know. If there are better ways to do this, please let me know :)

share|improve this answer
    
I really like your idea of using an object ID for this. I took a look at some more notable video game sites, and they too gave individual games as well as individual hardware, companies, and people an object ID of sorts, based on what I could tell from their URLs. –  Chris Jan 3 '11 at 1:18
    
Two things I'm concerned about, though this may just be because I'm not yet familiar with the inner workings of WordPress, and don't quite understand what makes your suggestion "hacky," as you put it. First, using your example, would it be possible to write a news article (using the normal post feature), mark it with a Series' ID, and somehow manage to pull an info box of sorts into the news posts' sidebar? i.e., when viewing the post, the sidebar would show not only the series, but things like the actors it stars, the studio that produces it, etc. which could very well be objects as well? –  Chris Jan 3 '11 at 1:19
    
Second thing , if this is "hacky," how future proof would such a solution be? If WordPress added a feature like this in the future, would this be easy to port? I mentioned your idea to a friend, and he suggested I look at the way forums (such as IPB, etc.) organize posts, mapping them to forums, categories, authors, etc. I won't know how related that idea and your idea are until I look into forum code and SQL databases, but basically I'm looking for something that's potentially future-proof and portable in case WordPress adds an organizational system similar to this in the future. –  Chris Jan 3 '11 at 1:22
    
I use 'hackey' is the loosest sense. We're basically using what Wordpress is already given us ie storing meta data and using it to have one post type reference another. Its basically how MySQL works to begin with, really. –  Manny Fleurmond Jan 3 '11 at 2:35
    
The example you illustrated could be done using the technique, but it would need a lot of referencing to make it work. There is a fine line between what you you would want to be a custom post type and what you want to be a custom taxonomy but technically yes, you could do it like I suggested: the series would need the id of the studio(s) that produced it. The actors would need id's of all the shows they were ever in, etc. Then you would need multiple loops to get all that information that is linked to each other to populate the side bar. –  Manny Fleurmond Jan 3 '11 at 2:42
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.