[英]WordPress posts ordered by category and then date
我不知道该怎么做,这就是需要发生的事情。 帖子需要按以下顺序显示:
其中每一个都应按从最新到最旧的顺序排列,并且第三层中的那些不应包括前两个类别的任何重复项。
我尝试使用UNION,但无法正确执行顺序,然后发现UNION最后仅支持一个“ 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>
从您的问题看来,您想要所有已发布的帖子,而不仅仅是您提到的类别中的帖子。
因此,您需要使用WHERE
条件将其全部选中。 我认为您不希望仅在这些类别中拥有一个UNION。
如果您使用这样的ORDER BY
子句,则应按照您提到的顺序获取它们。
ORDER BY $wpdb->term_taxonomy.term_id = 22 DESC,
$wpdb->term_taxonomy.term_id = 25 DESC,
$wpdb->posts.post_date DESC
前两项的计算结果为true 1
或false 0
。 订购他们DESC
则将真理放在首位。 最后一项将所有内容按数据降序排列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.