简体   繁体   English

WordPress帖子按类别排序,然后按日期排序

[英]WordPress posts ordered by category and then date

I have no idea how to do this, this is what needs to happen. 我不知道该怎么做,这就是需要发生的事情。 Posts need to display in this order: 帖子需要按以下顺序显示:

  1. Posts in Category 1 (that's the name, not the ID though I can use the ID it's 22) 类别1中的帖子(这是名称,不是ID,尽管我可以使用ID为22)
  2. Posts in Category 2 (id is 25) 类别2中的帖子(id为25)
  3. the rest of the posts 其余职位

Each of these should be in order from newest to oldest and the ones in the third tier should not include any duplicates from the first two categories. 其中每一个都应按从最新到最旧的顺序排列,并且第三层中的那些不应包括前两个类别的任何重复项。

I tried using a UNION but I could never get the order correct, then found out UNION only supports ONE 'ORDER BY' at the end, which jumbles them all up, defeating the purpose. 我尝试使用UNION,但无法正确执行顺序,然后发现UNION最后仅支持一个“ ORDER BY”,这使它们全都混乱了,无法达到目的。

Here's the other issue. 这是另一个问题。 This has to work with $wp_pagenavi, and the current method just dumps them all out on one page. 这必须与$ wp_pagenavi一起使用,并且当前方法只是将它们全部转储到一页上。

Here's what I currently have: 这是我目前拥有的:

<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>

From your question it sounds like you want all the published posts, not just the ones in the categories you mentioned. 从您的问题看来,您想要所有已发布的帖子,而不仅仅是您提到的类别中的帖子。

Therefore, you need WHERE criteria to select them all. 因此,您需要使用WHERE条件将其全部选中。 I don't think you want a UNION of the ones in just in those categories. 我认为您不希望仅在这些类别中拥有一个UNION。

If you use an ORDER BY clause like this, you should get them in the order you're mentioning. 如果您使用这样的ORDER BY子句,则应按照您提到的顺序获取它们。

ORDER BY $wpdb->term_taxonomy.term_id = 22 DESC, 
         $wpdb->term_taxonomy.term_id = 25 DESC,
         $wpdb->posts.post_date DESC

The first two terms evaluate to either true 1 or false 0 . 前两项的计算结果为true 1或false 0 Ordering them DESC puts the trues first. 订购他们DESC则将真理放在首位。 The last term puts everything in descending order of data. 最后一项将所有内容按数据降序排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM