![]() |
WP tbl joins.. DBA'rs help
Am looking to return term_id, name and slug from wp_terms. These term_id's need to be associated to post_id's that reside in wp_post_meta where wp_postmeta.meta_key='wpb_post_views_count'
I also need those results ordered by : order by cast(wp_postmeta.meta_value as signed) desc; just having problems associating back out... here are the tbls possibly involved mysql> describe wp_postmeta; +------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------------+------+-----+---------+----------------+ | meta_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment | | post_id | bigint(20) unsigned | NO | MUL | 0 | | | meta_key | varchar(255) | YES | MUL | NULL | | | meta_value | longtext | YES | | NULL | | +------------+---------------------+------+-----+---------+----------------+ mysql> describe wp_terms; +------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------------+------+-----+---------+----------------+ | term_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(200) | NO | MUL | | | | slug | varchar(200) | NO | UNI | | | | term_group | bigint(10) | NO | | 0 | | +------------+---------------------+------+-----+---------+----------------+ mysql> describe wp_posts -> ; +-----------------------+---------------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------------+---------------------+------+-----+---------------------+----------------+ | ID | bigint(20) unsigned | NO | PRI | NULL | auto_increment | | post_author | bigint(20) unsigned | NO | MUL | 0 | | | post_date | datetime | NO | | 0000-00-00 00:00:00 | | | post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | | | post_content | longtext | NO | | NULL | | | post_title | text | NO | | NULL | | | post_excerpt | text | NO | | NULL | | | post_status | varchar(20) | NO | | publish | | | comment_status | varchar(20) | NO | | open | | | ping_status | varchar(20) | NO | | open | | | post_password | varchar(20) | NO | | | | | post_name | varchar(200) | NO | MUL | | | | to_ping | text | NO | | NULL | | | pinged | text | NO | | NULL | | | post_modified | datetime | NO | | 0000-00-00 00:00:00 | | | post_modified_gmt | datetime | NO | | 0000-00-00 00:00:00 | | | post_content_filtered | longtext | NO | | NULL | | | post_parent | bigint(20) unsigned | NO | MUL | 0 | | | guid | varchar(255) | NO | | | | | menu_order | int(11) | NO | | 0 | | | post_type | varchar(20) | NO | MUL | post | | | post_mime_type | varchar(100) | NO | | | | | comment_count | bigint(20) | NO | | 0 | | +-----------------------+---------------------+------+-----+---------------------+----------------+ mysql> describe wp_term_relationships; +------------------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+---------------------+------+-----+---------+-------+ | object_id | bigint(20) unsigned | NO | PRI | 0 | | | term_taxonomy_id | bigint(20) unsigned | NO | PRI | 0 | | | term_order | int(11) | NO | | 0 | | +------------------+---------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> describe wp_term_taxonomy; +------------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+---------------------+------+-----+---------+----------------+ | term_taxonomy_id | bigint(20) unsigned | NO | PRI | NULL | auto_increment | | term_id | bigint(20) unsigned | NO | MUL | 0 | | | taxonomy | varchar(32) | NO | MUL | | | | description | longtext | NO | | NULL | | | parent | bigint(20) unsigned | NO | | 0 | | | count | bigint(20) | NO | | 0 | | +------------------+---------------------+------+-----+---------+----------------+ |
try here
www.stackoverflow.com |
Put this together real quick so don't know if it is 100% correct or not:
select c.term_id, c.name as term_name, c.slug as term_slug from wp_posts a inner join wp_term_relationships b on a.id=b.object_id inner join wp_term_taxonomy c on b.term_taxonomy_id=c.term_taxonomy_id inner join wp_terms d on c.term_id=d.term_id inner join wp_post_meta e on a.id=e.post_id where e.meta_key='wpb_post_views_count' order by cast(e.meta_value as signed) desc |
Quote:
:thumbsup |
curious why you are using a sql call rather than a wp query?
|
Quote:
Quote:
If you are asking the OP, I will let him answer ;p . |
Quote:
|
All times are GMT -7. The time now is 02:50 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123