Taxonomy showing in db_query????
feeper - April 26, 2009 - 02:39
Hi all,
I set up this function to grab the last 6 articles that have been entered:
function recent_articles() {
$recent_query = db_query("SELECT `nid`, `title` FROM {node} ORDER BY `created` DESC LIMIT 6");
return $recent_query;
}Now, when I check the node table, none of the taxonomy stuff is there. Which makes me wonder why the above query is not only pulling article titles & nid's, but it is also grabbing taxonomy titles.
Has anybody come across this before or maybe have a solution???? :)

Maybe you have nodes with
Maybe you have nodes with titles the same as taxonomy terms?
You only show the query but I would guess you are building links to the nodes. Where do the links that you think are taxonomy terms take you?
Taxonmy Content
They take me to the taxonomy content.
This suggests you have a
This suggests you have a content type that represents taxonomy terms (core does not provide taxonomy content, only taxonomy terms).
So you would want to filter the type out in your query, something like
$unwanted_type = 'page'; // Change to the actual type you do not want listed$recent_query = db_query("SELECT `nid`, `title` FROM {node} WHERE type != '%s' ORDER BY `created` DESC LIMIT 6". $unwanted_type);
Drupal 6 query??
Thanks for the suggestion!
I couldn't get that to work....even leaving the code the way it is...the query turned up blank.
function recent_articles() {$unwanted_type = 'page'; // Change to the actual type you do not want listed
$recent_query = db_query("SELECT `nid`, `title` FROM {node} WHERE type != '%s' ORDER BY `created` DESC LIMIT 6". $unwanted_type);
//$recent_query = db_query("SELECT `nid`, `title` FROM {node} ORDER BY `created` DESC LIMIT 6");
return $recent_query;
}
Do I have it correct?
I am sure there is an
I am sure there is an explanation, if you just show us your unexpected result and how you are displaying it. What do you do with the result of this function?
Recent Article Titles/Links
The unexpected result is taxonomy content is showing when I only want node titles to show up. (IE - Titles/NID from the nodes table in the Drupal database). I am not sure why Taxonomy content is showing up with Node Content ... I would have to assume (correctly) that they are not even in the same database tables.
$results = recent_articles();$count = 0;
echo '<div class="recent_separator"></div>';
while($article_results = db_fetch_array($results))
{
$div_select_open = "";
$div_select_close = "";
if($article_results['nid'] == $node->nid || $_GET['page'] == $article_results['nid'] || $count == 0 && !isset($_GET['page']) && !$node->nid) { $div_select_open = '<div class="div_select_open">'; $div_select_close = '</div>'; }
echo '<div class="recent_articles">' . $div_select_open . '<a href="http://' . $_SERVER['HTTP_HOST'] . $base_path . '?q=node/' . $article_results['nid'] . '">' . $article_results['title'] . '</a>' . $div_select_close . '</div>';
echo '<div class="recent_separator"></div>';
}
That is what displays it.
A suggestion
To make your code more "Drupal like" and support aliases I would suggest replacing
echo '<div class="recent_articles">' . $div_select_open . '<a href="http://' . $_SERVER['HTTP_HOST'] . $base_path . '?q=node/' . $article_results['nid'] . '">' . $article_results['title'] . '</a>' . $div_select_close . '</div>';with
echo '<div class="recent_articles">' . $div_select_open . l($article_results['title'] , 'node/' . $article_results['nid']) . $div_select_close . '</div>';Updated to be more 'Drupal Like'
Thanks for the suggestion! I was trying to find something like this because I didn't feel my code was 'Drupal Friendly'.
I gave your code a run and it
I gave your code a run and it seems to work fine. I don't see any taxonomy content.
Provide more detail
Here are the results that show up from the original query:
*Message from the CAJ national conference chair <- Article title in Node table
*Speaking out <--- Part of the "Message from the CAJ national conference chair" -> it appears it is the caption to an image.
*Reminder of Edmonton National Conference, May 23-25 <- Article title in Node table
*What we do <-- Content in a taxonomy_block (titled "Who we are") First item in block
*Aims and Objectives <-- Content in a taxonomy_block (titled "Who we are") Second item in block
*CAJ National Board of Directors <- Article title in Node table
Now I am not sure why the "Caption" from the first article shows up in this list - Not to mention the taxonomy_block content.
Do these things have
Do these things have "node/nid" links? If so, what is at the destination?
Node links. ?q=node/### My
Node links.
?q=node/###My assumption, which may be wrong, is that is should pull the node that has the nid of ###. If not, how is that controlled? I literally only want what is in the Node table in the database. I would assume that is the point of 'node/###'.
We are all confused because
We are all confused because you think your query is pulling non-nodes. But since you are only querying against the node table you will only get nodes back. A suggestion, currently you query for nid and title, add type to the list of values returned and have your code print that out. What is the value of type for what you think aren't nodes?
This is why I am confused by
This is why I am confused by node types.
If I look at the raw data in the database. Looking through the node table. They are all 'story' as type.
I mentioned earlier that I was confused by the fact that I am pulling nodes....and I look at the raw data in the database (node table) and I see nothing but story type. I did what you suggested:
Message from the CAJ national conference chair
story
Speaking out
image
Reminder of Edmonton National Conference, May 23-25
story
What we do
story
Aims and Objectives
story
CAJ National Board of Directors
story
Now, with this information at hand - if it is a node I am pulling then I should see the above corresponding information in the raw data of the node table in the database....I do not see it for Speaking Out, What we do, & Aims and Objectives. Am I simply not looking at this correctly? Is there a simple way of telling Drupal to only look in the node (table in the database) and only look at type 'story'?
To help match up with
To help match up with database add the vid to query and print the nid, vid, type and title. When looking at the database look at both the node table and node_revisions table (the later will contain the title, teaser and body).
To restrict the types of nodes return add a WHERE clause like
WHERE type = 'story'orWHERE type IN ('story', 'page')A good reason to use views is to avoid the need to understand the tables (or worry about changes on upgrades) and it makes filtering easy.
hmm
Nevets...
I tried that too, with story only - in fact my query is like that now...because of a suggestion above.
I found out that the list is coming up properly....my client was playing around and I have spotted those items that were not showing up before (the ones I have pointed out).
Now the problem is...there are some taxonomy blocks that these stories are part of and they don't want them showing up in the recent articles. (still same problem - but expanded).
Now these are all described as 'type' story. I suppose there is no way to filter out those that are part of these taxonomy blocks is there?
Once again views would be
Once again views would be useful in this regard. If you to produce both list the duplicates can be handled by the Views exclude previous module.
Just thought of it, but I am
Just thought of it, but I am not sure of the filter....how do you filter for front page articles...there is a check in the create and edit story forms to place the story on the front page - these are the only stories to be contained in the recent articles.
One of the views filters is
One of the views filters is "Node: Promoted to front page" which can be set to Yes or No.
Sorry if this is a dumb
Sorry if this is a dumb question..it's almost 1am and I am getting a bit 'groggy'....
How could I incorporate that in my db_query? A regular mysql query can coordinate data from two tables...can that be done with a db_query?
Another question...if it is the view_filter...shouldn't there be something in that table? Right now there is 0 rows.
I checked the first article on the recent articles list (one that is supposed to be there) and it has a check to say promoted to frontpage. I just can't seem to find where that would be stored.
So, all those items displayed
So, all those items displayed by your query *are* nodes. The "image" type item is a node probably created by image module. It just happens that some of them are also displayed by another query which runs in that taxonomy block of yours.
Your db_query can display anything just like any mysql_query, but it is unaware of any other query which runs in the page, so you get duplicates. You could try to do what nevets suggested, using http://drupal.org/project/views and the http://drupal.org/project/views_exclude_previous filter instead of your query, to make your node list aware of what else is displayed in the page.