在Wordpress中,我有六类帖子。页面必须调用每个类别中最近的3个帖子并按顺序显示它们(不混合)。
要完成该任务,首先我向DB询问类别。然后,foreach
类别我运行查询以使用$ wp_query对象检索该类别名称下的最后3个帖子 - 这就像6个查询一样。类别可以改变,当然我不想在每次改变代码时都被调用。
我尝试使用$ wpdb使用一次性MySQL(5.5)查询来检索所有这些帖子and variables:
SELECT
DISTINCT $wpdb->posts.ID,
$wpdb->posts.post_title,
$wpdb->posts.post_excerpt,
$wpdb->posts.comment_count,
$wpdb->posts.guid,
$wpdb->posts.post_status,
$wpdb->posts.post_name,
$wpdb->posts.post_date,
$wpdb->posts.post_type,
$wpdb->terms.slug
FROM (
SELECT
$wpdb->posts.ID,
$wpdb->posts.post_title,
$wpdb->posts.post_excerpt,
$wpdb->posts.comment_count,
$wpdb->posts.guid,
$wpdb->posts.post_status,
$wpdb->posts.post_name,
$wpdb->posts.post_date,
$wpdb->posts.post_type,
$wpdb->terms.slug,
@num := if(@group = $wpdb->terms.slug, @num + 1, 1) as row_number,
@group := $wpdb->terms.slug
FROM $wpdb->posts
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)
ORDER BY $wpdb->terms.slug ASC, $wpdb->posts.post_date DESC
) as $wpdb->posts
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_type = 'site_type'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'site_category'
AND $wpdb->terms.slug IN ('cars', 'planes', 'motorcycles', '4x4', 'bicycles', 'jetpacks')
AND row_number <= 5
ORDER BY $wpdb->terms.slug ASC, $wpdb->posts.post_date DESC
LIMIT $total
然后,在此之后,我split the resulting array取决于slug
。结果就是问题。
在PHPMyAdmin中,我执行此查询并且row_number
没有求和,每一行都获得值1.这使得每个“X slug下的帖子组”没有限制,row_number <= 5
不会工作。在自定义页面中,第一个类别(包含7个帖子)显示超过3个。
答案 0 :(得分:0)
如果我理解你的问题,那么我会告诉你使用query_posts
<?php
$categories=array('cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6');
foreach($categories as $cat)
{
query_posts('category_name='.$cat.'&showposts=3&orderby=date&oredr=DESC');
while (have_posts()) : the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
wp_reset_query();
}
?>
只需使用您的真实类别名称替换$ categories数组中的cat1
,cat2
...... cat6
。
此代码段将显示我们之前指定的$categories
数组中每个类别的最新3篇帖子(此处仅作为示例)。
答案 1 :(得分:0)
再次尝试使用MySQL变量,并且没有这样做之后,我看到了另一个选项:使用UNION ALL
- has stated here,你SELECT
每个组的N个数,然后你团结起来简单来说,这些都是。性能方面,至少在我的配置中,它的表现几乎与使用变量相同。
因此,我构建了一个foreach
类别并为该组添加了查询。
$p = 1;
$numb = count($products);
global $wpdb;
foreach($categories as $product) {
$query .= "(SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_excerpt, $wpdb->posts.comment_count, $wpdb->posts.guid,
$wpdb->posts.post_status,
$wpdb->posts.post_name,
$wpdb->posts.post_date,
$wpdb->posts.post_type,
$wpdb->terms.slug
FROM $wpdb->posts
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_type = 'pb_review'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'product_reviews'
AND $wpdb->terms.slug IN ('$product->slug')
ORDER BY $wpdb->posts.post_date DESC
LIMIT 3)" . ($p == $numb ? '' : ' UNION ALL ');
$p++;
}
构造$ query之后,我将它作为变量传递给$ wpdb和voilà:
$productos_query = $wpdb->get_results($query, OBJECT);
为什么变量不起作用,为row_number
添加+1?我的猜测是因为我使用的是LEFT JOIN
,而是我的客人。