Complex ExpressionEngine Queries Revisited

Sep. 6, 2008

10:59 am

Not so long ago, I wrote about a method for running complex queries on an ExpressionEngine database using two built-in functions: exp:weblog:entries and exp:query. It's a neat and useful trick, but the example I suggested was a somewhat limited, specific use case. There are tons of ways to relate one bit of data to another in ExpressionEngine and equally as many ways to get at that data. Let's take a look at another one of those methods.

Querying on categorized data in ExpressionEngine.

Like virtually every other CMS on the planet, ExpressionEngine provides some handy tools for applying categories to data and displaying that data in a logical way. In EE, exp:weblog:categories is likely gonna be the right tool for the job. Let's say you have a "staff" weblog where each staff member is assigned to one or more categories – maybe clerical, administrative, and executive. Your template code might look something like this:

  1. <ul>
  2. {exp:weblog:categories weblog="staff" show_empty="no" style="linear"}
  3. <li><a href="{path="about/staff_list"}">{category_name}</li>
  4. {/exp:weblog:categories}
  5. <ul>

What you'll end up with is an unordered list of all your categories associated with the "staff" weblog, each linking to a template that displays the items belonging to that category. So your HTML output would look like this:

  1. <ul>
  2. <li><a href="/about/staff_list/">Administrative</a></li>
  3. <li><a href="/about/staff_list/">Managerial</a></li>
  4. <li><a href="/about/staff_list/">Executive</a></li>
  5. </ul>

But what if…

Let's say you wanna skip the middle man. That is, you don't want to make the user click through to another page to see all the staff members in a particular category. What if instead you just wanna list all staff, organized by category, on this current page and link each of those staff members to their individual bio pages? In many instance, limiting the number of times a site visitor must click to a new page will increase the time they spend on your site.

Luckily, EE lets you nest exp:weblog:entries within exp:weblog:categories. Our solution might look like this:

  1. {exp:weblog:categories weblog="staff" show_empty="no" style="linear"}
  2. <h3>{category_name}</h3>
  3. <ul>
  4. {exp:weblog:entries weblog="staff" category="{category_id}" limit="999" orderby="staff_last_name" sort="asc" status="featured|open"}
  5. <li>
  6. <h3><a href="{title_permalink="about/staff"}">{title}</a>, {staff_position}</h3>
  7. <span class="contact">{staff_phone}{if staff_email}, <a href="mailto:{staff_email}">email</a>{/if}</span>
  8. </li>
  9. {/exp:weblog:entries}
  10. </ul>
  11. {/exp:weblog:categories}

Nice, right? The trick is on line 4. EE is set up in a way that allows you to use the category_id of the current category you're iterating over as a parameter for exp:weblog:entries. ExpressionEngine doesn't always allow this kind of nesting, and when they do, it's often poorly documented. My advice? If you find yourself needing similar functionality but with other modules and tags, just give it a shot. If it doesn't work, there's always the forums. Someone's likely tried to do exactly what you're doing, and if you're lucky, they've already found a super elegant solution.


January 3, 2009

1:17 pm

awesome, this actually gave me some ideas to implement!

h&m clothing (#)

Whaddya think?