Hi everyone. Drupal newbie here.

I'm trying to get a jump start on the basics of Drupal. So far, I'm impressed with the system, but I'm having difficulty getting some of the basics down. I'm trying to figure out how to appropriately filter the content items for display. I've got a list of headlines to display based only on the content type (news, publications, etc) with the following code:

<?php
$listlength="5";
$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = 'news' AND n.status = 1 ORDER BY n.created DESC"), 0, $listlength));
print $output;
?>

I'm trying to figure out how to further filter these headlines so they can be displayed on specific areas of the website. Specifically, how do I filter the headlines to appear only when they are classified within a specific taxonomy (i.e. /taxonomy/term/2). Thanks much in advance all!

EP

Comments

bmcmurray’s picture

Hi EP, welcome to Drupal!

You'll probably have better luck creating these customized queries for content by using the Views module (http://www.drupal.org/project/views). Using views you can quickly pages and/or blocks which are populated with content which you can filter by a number of different parameters, including taxonomy terms.

--
B. McMurray

--
Brian McMurray

nevets’s picture

The views module lets you construct pages and/or blocks that display content in a number of ways. In addition they support sorting of the information and filters. All without coding or sql statements.

ep-1’s picture

Thanks bmcmurray and nevets, I'll check the view module out. I was hesitant to start installing modules before I understood the basics of this, but this one looks basic enough that I won't get overly jaded and reliant on modules to do the leg work.

Thanks again.

EP

ep-1’s picture

I got it. I'm a bit of a purist and want to avoid using modules when possible.

<?
// Number of headlines to print
$listlength="10";
// Type of headlines to print
$printedtype="biographies";
// Type of content category to print
$printedcategory="2";
$output = node_title_list(db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created, t.nid, t.tid FROM {node} n, term_node t  WHERE n.type = '$printedtype' AND n.status = 1 AND n.nid = t.nid AND t.tid = $printedcategory ORDER BY n.created DESC"), 0, $listlength));
print $output;
?>

This will pull the headline of a node that is of a specified type (biographies) within a specified type number (2). This worked perfectly for the type of website I was looking to put together.

mooffie’s picture

I like your attitude, of wanting to understand how things work instead of using a module that shields us from the neccesity to understand. Eventually you too will use these modules, but it's good to know how things work. (I'm not sure you're a purist. You may be a perfectionist, or a control-freak ;-)

Note that it's customary in this community to use JOIN instead of 'FROM table1, table2'. In fact, db_rewrite_sql() might choke on the latter.

ep-1’s picture

Yeah, purist probably wasn't the right word. I'd go with control freak. In the end, I just want to figure this system out inside and out before I start relying on modules and shortcuts.

Speaking of shortcuts (heh), could someone give me a quick primer on how I would go about changing my previously provided code into a command that utilizes a JOIN rather than a FROM?

mooffie’s picture

Change:


... FROM {node} n, {term_node} t WHERE ... AND n.nid = t.nid ...

To:


... FROM {node} n INNER JOIN {term_node} t ON n.nid = t.nid WHERE ...

You can find tutorials on the net. In a nutshell, the conditional you had in the WHERE clause described the relationship between the tables and you simply turn it into an ON thingy. And you write 'JOIN' between the table names.