What is the most efficient way to manage multiple level relationships using WordPress Pods CMS

This is my first time using Pods in WordPress, so forgive me if this is a bit of an easy question to answer. I’ve done a bit of searching for an answer to this problem, but haven’t found anything that answers my question specifically.

I have two different kinds of pods created.

Read More
  1. Areas of Study: Such as Physics, Sociology or Computing
  2. Case Studies: Details on case studies performed in various areas of study

Each Case Study Pod has a relationship field linking it to a specific Area of Study Pod. (In this example, assume that a specific case study can only be related to a single area of study. This may change in the future, but for now, this should suffice.)

Each Area of Study has a relationship field linking it to a specific WP Page (e.g. a detailed page that describes the details for the Physics Area of Study).

What I want to do, is while looping through various Case Studies, be able to obtain the link for the WP Page, it’s specific area of focus is related to.

I technically know HOW to do it, but I’m not sure it’s the most efficient way of doing it.
This is what I have done, and it seems to be working:

<?php 
$case_studies = new Pod('case_studies');
$case_studies->findRecords(null, 200);
$total_rows = $case_studies->getTotalRows();

if ($total_rows > 0) {
        while ($case_studies->fetchRecord()) {
                // RETRIEVE AREA OF FOCUS DETAILS
                $aof = $case_studies->get_field('area_of_focus');
                $aof = $aof[0];

                // GET AREA OF FOCUS POD
                $aofs = new Pod('areas_of_focus');
                $params = array('where' => "t.id like '".intval($aof['id'])."'");
                $aofs->findRecords($params);
                if ($aofs->fetchRecord()) {
                        $page = $aofs->get_field('focus_page');
                        $area_of_focus_url = get_permalink($page[0]['ID']);
                        echo '<a href="'.$area_of_focus_url.'">'. $aof['name'].'</a>';
                } else {
                        echo $aof['name'];
                }

                // CASE STUDY DETAILS GO HERE
        }
}

?>

This seems like it’s at least an O(n²) algorithm, if not more. I really don’t know the internals of the Pod class, so it could be even more expensive than that. Also this will be executing several MySQL queries instead of a single query.

Would it be better if I used some MySQL joins in the initial case study query so that I don’t have to have it run a bunch of other queries? Or are there any other functions within Pods itself, that can allow me to retrieve more details on relationship fields?

Any tips would be greatly appreciated.

Related posts

Leave a Reply

1 comment

  1. You can actually go very deep with get_field by traversing relationships.

    Try this out for size:

    $page_ID = $case_studies->get_field('area_of_focus.focus_page.ID');
    

    Pods will do all the work for you, you can go a ton of levels deep, and you can even do the same with findRecords (as of Pods 1.12)