In the below query I noticed these named variables wposts, wpostmeta
and was wondering how they work and what are they called (MySQL variables) so I can find out more information about using them in the MySQL docs.
Is it a shorthand so you don't have to type $wpdb->postmeta
every time or is there more to this? Also, I don't understand the SELECT wposts.*
there is no Wordpress table called wposts so what are you selecting from?
https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'custom-key'
AND wposts.post_type = 'page'
ORDER BY wpostmeta.meta_value DESC
";
They are table aliases, and provide (generally) a short-hand way of referring to a table in a query. In your query the table whose name was $wpdb->posts
is now referred to as wposts
, and the table called $wpdb->postmeta
is now referred to as wpostmeta
. Note that once you have declared an alias, you must refer to the table using that alias, hence in your query you have references to wposts.*
, wpostmeta.meta_value
etc.
Note that you can also have column aliases, for example:
SELECT SUM(x) AS total FROM t1
Also note that the AS
keyword I have shown in the column select above is optional and can be used for table and column aliases.
You can read more about aliases here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.