Down and Dirty WordPress Pods Related Content

Update: Looking at this now, I realized there are some advantages to taking a slightly different approach.

This thread at asks how to relate posts to Pods content. This is something that will get easier in future versions of pods. But the beauty of Pods is that it’s already pretty damn easy if you are familiar with how WordPress manages custom fields.

Getting Down!

So let’s say you have a pod called “books” and you want to be able to relate blog posts to the books you are organizing via pods. I’m going to assume for the sake of this tutorial that you already know how to create Pods content and so you’ll have no problem with the first step:

1. Books pod:

2. Posts pod.

Next you’ll want to set up a pod to relate to posts, you might call it “posts”, or “post_relations”, or “post_meta”:

Again, if you’re familiar with Pods this should be pretty straight-forward. Though you might ask why are we creating a pod called “posts” when it’s the books we want to relate to posts. Well we could just relate the books to posts. But the trouble is relating the other way, posts -> books, gets difficult without a pod to store all the relationships. Plus, we might want to add more “meta” fields to our posts pod that we can also associate with a post. Basically, if we’re using pods, we might as well take advantage of the flexibility.

Let’s also create a pod field called books and make it a pick field related to the books pod.

So in our posts pod, we have a pick column relating to posts and one relating to books.

3. Multidirectional?

We can now return to the book pod and set up a “posts” field there to relate to the posts pod as a bidirectional field.

Why not relate to the posts directly? Again, we want to leverage pods to give us as many options as possible. As long as one pod as is related to posts, we’re “plugged” in so to speak.

4. Create the metabox function:

So here’s where it gets a little more difficult … but still not too bad. We’ll need to create a function that will build the content of our meta box. Add this function to your functions.php.

function books_related_pods() {
global $post;
echo '
'; echo ''; echo '
'; }

Notice that we’re creating one Pods object as ‘$old’ so that we can access the existing values. Then we’re creating a second as “options” which we use to build our select menu options.

5. Create a meta box callback function.
Now we need a function to build the metabox itself. This function will be hooked into the WordPress API. This is an easy one. If you want to know more about meta boxes, read the WordPress Codex article.

function books_related_mb() {
add_meta_box( 'pods_related_mb', "Related Books", 'books_related_pods', 'post', 'normal', 'high', '' );

Note that the third parameter sets the “callback” function for the meta box. In this case we are using function we created above. Also note that the fourth parameter is where you specify the post_type on which you wish to use the meta box.

6. Hook into the WordPress API:

Just add a line in your functions.php:


So at this point you should be able to go to your post screen and see the new meta box. If you can’t your doing something wrong. But we still need to save the meta box.

6. Save the pods!

Now you’ll see the advantage of the seemingly complicated pods set up. All we have to do is find the post’s related pod and add the values using the Pods API.

function books_related_save_pod($post_id) {
if( !wp_is_post_revision( $post_id ) && !wp_is_post_autosave( $post_id ) ) {
        if(isset($_POST['related_books'])) {
$vals = implode(',',$_POST['related_books']);
$pod = new Pod('posts');
$pod->findRecords('id DESC',1,"post.ID = '$post_id'" );
while($pod->fetchRecord()) { 
$pid = $pod->get_pod_id();
if($pid) {

//if a pod exists, save the values
$api = new PodAPI();
$params = array(); 
$params['datatype'] = 'posts';
$params['pod_id'] = $pid;
$params['columns']['books'] = $vals;
$params = pods_sanitize($params);

} else {

$new = new PodAPI(); 
$params = array();
$params['datatype'] = 'posts';
$params['columns']['name'] = $_POST['post_title']; // You must set a name when saving a new pod
$params['columns']['post'] = $post_id;
$params['columns']['books'] = $vals;
$params = pods_sanitize($params);


So there you have it. Down and dirty.