Manipulating prewritten MySQL query - PHP
I have the following query which returns a list of 8 post ids for wordpress, i want to then take those 8 ids and match them to the id's from the wp_posts table and get * details so i can echo post titles and all that, is there a way i can do that in one query? .. here is what i currently have
SELECT postid FROM wp_popularpo开发者_如何学编程stsdata WHERE DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day
AND wp_popularpostsdata.postid NOT IN (
SELECT object_id
FROM wp_term_relationships AS r
JOIN wp_term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
JOIN wp_terms AS t ON t.term_id = x.term_id
WHERE x.taxonomy = 'category'
AND t.term_id IN (3,1))
GROUP BY postid
ORDER BY pageviews DESC LIMIT 8;
Cheeers,
Have you tried joining the tables?
SELECT postid, wp_posts.* FROM wp_popularpostsdata
LEFT JOIN wp_posts ON wp_posts.id = wp_popularpostsdata.postid
WHERE
DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day AND
wp_popularpostsdata.postid NOT IN (
SELECT object_id
FROM wp_term_relationships AS r
JOIN wp_term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
JOIN wp_terms AS t ON t.term_id = x.term_id
WHERE
x.taxonomy = 'category' AND
t.term_id IN (3,1))
GROUP BY postid
ORDER BY pageviews DESC LIMIT 8;
Replace wp_posts.id
with the key field in wp_posts
Or you could so another subselect to get just the wp_posts data.
SELECT * FROM wp_posts WHERE id IN (
SELECT postid FROM wp_popularpostsdata
WHERE DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day
AND wp_popularpostsdata.postid
NOT IN (
SELECT object_id
FROM wp_term_relationships AS r
JOIN wp_term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
JOIN wp_terms AS t ON t.term_id = x.term_id
WHERE x.taxonomy = 'category'
AND t.term_id IN (3,1)
)
GROUP BY postid
ORDER BY pageviews DESC LIMIT 8;
)
You should put the id's array ( '155', '312', '18','22',... ) in
$posts = new WP_Query(array(
'post__in' => array( '155', '312', '18','22',.. ) /* YOUR ARRAY */
));
It will give you the posts with filters applied. More information for WP_query on http://codex.wordpress.org/Function_Reference/WP_Query
?>
精彩评论