我试图获取标记为标记为特定类别的投资组合的帖子列表。对于该类别,我需要限制每个类别的每个结果集,并且只获得每个帖子的最新更新帖子。
我目前的疑问是:
SELECT $wpdb->posts.*, $wpdb->terms.slug
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'portfolio'
AND $wpdb->posts.post_date < NOW()
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->terms.slug IN ('".implode("','", $category_array)."')
ORDER BY
Case When $wpdb->terms.slug = 'residential' Then 1
When $wpdb->terms.Slug = 'recreational' Then 1
When $wpdb->terms.Slug = 'civic' Then 1
When $wpdb->terms.Slug = 'contact' Then 2
When $wpdb->terms.Slug = 'blog' Then 3
When $wpdb->terms.Slug = 'services' Then 4
When $wpdb->terms.Slug = 'team' Then 5
Else 6
End
我将使用类似这样的内容(MySQL limit results per category)来获取每个类别的设置限制。
但是,这会返回当前同一帖子的每个版本。我不确定如何只获得该帖子的最新版本。我知道我怎么可能&#34;要做到这一点,但高度怀疑它是最好的方式。有没有人知道用这样的自定义查询获取最新帖子的标准化方法?
此外,我不一定会致力于自定义查询。我只是不知道如何使用带有WP_Query()
函数的参数创建此查询。
谢谢, 詹姆斯
答案 0 :(得分:0)
所以我最终使用了一个子查询。这是我使用的。
$querystr = "SELECT x.*
FROM (
SELECT y.*,
CASE
WHEN @category != y.slug THEN @rownum := 1
ELSE @rownum := @rownum + 1
END AS rank,
@category := y.slug
FROM (
SELECT $wpdb->posts.*,
$wpdb->terms.slug
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'portfolio'
AND $wpdb->posts.post_date < NOW()
AND $wpdb->terms.slug IN ('".implode("','", $category_array)."')
AND $wpdb->posts.ID NOT IN
(
SELECT $wpdb->posts.ID
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'portfolio'
AND $wpdb->posts.post_date < NOW()
AND $wpdb->terms.slug IN ('".implode("','", $category_array)."')
AND $wpdb->posts.post_type = 'revision'
GROUP BY $wpdb->posts.ID
)
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->terms.slug
) y
LEFT JOIN (SELECT @rownum := 0, @category := NULL) r ON (1 = 1)
) x
WHERE x.rank <= 10
ORDER BY
Case When x.slug = 'residential' Then 1
When x.Slug = 'recreational' Then 1
When x.Slug = 'civic' Then 1
When x.Slug = 'contact' Then 2
When x.Slug = 'blog' Then 3
When x.Slug = 'services' Then 4
When x.Slug = 'team' Then 5
Else 6
End,
rand()";