我不知道如何做到这一点,这是需要发生的事情。帖子需要按以下顺序显示:
其中每一项都应按照从最新到最旧的顺序排列,而第三层中的每一项不应包含前两个类别中的任何重复项。
我尝试使用UNION,但我永远无法得到正确的订单,然后发现UNION只支持ONE' ORDER BY'最后,他们把所有人都弄乱了,打败了目的。
这是另一个问题。这必须与$ wp_pagenavi一起使用,并且当前方法只是将它们全部转储到一页上。
这是我目前所拥有的:
<div id="content" class="post right">
<?php
$query = "(SELECT * 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) WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = 22 ORDER BY post_date DESC) UNION ";
$query .= "(SELECT * 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) WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = 25 ORDER BY post_date DESC)";
error_log($query);
$pageposts = $wpdb->get_results($query, OBJECT);
?>
<?php if ($pageposts) : ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post) : ?>
<?php setup_postdata($post); ?>
<div class="article">
<p class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php the_time('F jS, Y') ?><br />
<?php the_excerpt(); ?>
</div>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="navigation">
<?php wp_pagenavi(); ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php else : ?>
<div id="post-0" class="post error404 not-found">
<h1>Not Found</h1>
<div class="entry">
<?php
if ( is_category() ) { // If this is a category archive
printf("<p>Sorry, but there aren't any posts in the %s category yet.</p>", single_cat_title('',false));
} else if ( is_date() ) { // If this is a date archive
echo("<p>Sorry, but there aren't any posts with this date.</p>");
} else if ( is_author() ) { // If this is a category archive
$userdata = get_userdatabylogin(get_query_var('author_name'));
printf("<p>Sorry, but there aren't any posts by %s yet.</p>", $userdata->display_name);
} else if ( is_search() ) {
echo("<p>No posts found. Try a different search?</p>");
} else {
echo("<p>No posts found.</p>");
}
?>
<?php get_search_form(); ?>
</div>
</div>
答案 0 :(得分:0)
从您的问题来看,您似乎想要所有发布的帖子,而不仅仅是您提到的类别中的帖子。
因此,您需要WHERE
条件才能全部选择它们。我不认为你想要那些类别的联盟。
如果你使用这样的ORDER BY
条款,你应该按照你提到的顺序得到它们。
ORDER BY $wpdb->term_taxonomy.term_id = 22 DESC,
$wpdb->term_taxonomy.term_id = 25 DESC,
$wpdb->posts.post_date DESC
前两个词的评估结果为真1
或假0
。对它们进行排序DESC
将真相放在第一位。最后一项将所有内容按数据的降序排列。