griever;10935590 wrote:
(...) because it's just outputting the code as text.
Reason
<?php
get_header ();
// drops out of php parsing mode here
?>
<!-- this is html and should not be parsed -->
<div id="box">
<!-- still not in php parse mode, but this is php code. add php opening tag... -->
$qry = ...
I've no idea if you know any SQL at all, so bear with me if I'm explaining things you know.
This part tells the db what fields your interested in. Think of these as columns in a spreadsheet, and you're telling the db which of these columns should be retrieved.
SELECT field1, field 2, ..., fieldN
Table in which to find the columns
FROM table1
One table is not enough. Inner join tells the db to combine the first table with the second table. Every post where table1.someColumn is equal to table2.aColumn will be joined together. If there is no matching value in table2, then no row is retrieved from table1. (Compare this to LEFT JOIN where every row in table1 is retrieved, and if someColumn has no matching value in aColumn, the columns from table2 are filled with null values for this row).
INNER JOIN table2 ON table1.someColumn = table2.aColumn
users
id name
--- -------
1 tony
2 eric
posts
id author title
-- -------- ------------------------
1 1 Tony's first post
2 2 Eric's first post
3 2 Eric's second post
.... (for which author is neither 1 or 2) ...
115 2 Eric's third post
SELECT name, title
FROM users
INNER JOIN posts ON posts.author = author.id
This would link the user row for 'tony', id = 1, to the post with title "Tony's first post", author = 1, since id = author.
It would do the same for user 'eric', but get the posts with id 2, 3 and 115, where author = 2, since that matches eric's user id.
And the result is:
name title
------ ------------------
tony Tony's first post
eric Eric's first post
eric Eric's second post
eric Eric's third post
(just never assume this comes in any specific order, unless you tell the db to order the result in some way)
The where clause tells the DB that we only want posts where a certain field or fields matches certain criteria
WHERE somefield = somevalue
WHERE nicename = 'eric' OR nicename like 'tony%'
Only include rows from users where the user's name is eric, or where the user's name starts with tony (tony_the_tiger would be ok, but not tiger_tony)
Order by tells the db how to order the result. ASC stands for ascending, DESC for descending, and ordering is done depending on the data type of the field. An int field will be sorted as number, where 9 comes before 10, while a text field like nicename will be ordered as a string, where 10 comes before 9.
First order by field2 from lowest to highest, then for any rows having the same field2 value, also order by field1 in descending orer.
ORDER BY field2 ASC, field1 DESC
And no, you do not have to use the instance of word press db class. There are some things I don't like about it at all. For example, it has a member method called prepare, which "prepares" a query, except that it has nothing to do with prepared statements. In other words, it has no correlation to functions like mysqli->prepare(). Moreover, it does some unnecessary things, such as replacing '%s' with %s, "%s" with %s, and then replacing %s with '%s'. The result is that whatever the user (you) uses, it will always turn out to be '%s' in the end. Why not just assume that the coder deals with writing correct code in the first place, or at least learn to do so when he recognizes his error?
Also, the code is riddled with the error supression operator @. And there are word press specific settings for how to deal with errors. Perhaps it's useful if you have no control over this for some reason.
It would be interesting to hear what others think, but I'd skip wpdb and do this with mysqli.
griever;10935590 wrote:
I was wondering where I should put the "Loop" that wordpress uses to output the posts. My old loops, one for each author, was:
I bet a lot of this is wordpress specific but any help is really appreciated
With my solution, you wouldn't. You'd use the loop I suggested in my first post.
Not really. Apart from the db schema they use, for which info is here, this is pretty much generic.