[英]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.