代码之家  ›  专栏  ›  技术社区  ›  user979331

WordPress发布和私人帖子,然后订购它们

  •  1
  • user979331  · 技术社区  · 6 年前

    我这里有一个代码,用于获取我的已发布和私人帖子:

    $args = array(
        'post_status' => array( 'publish', 'private'),
    );
    $query = new WP_Query( $args ); 
    
    $posts = $query->posts;
    

    现在我试着按邮政状态订购它们,这样私人的就低于发布的,然后按日期订购。我的问题是我该怎么做?

    我试过这个:

    $args = array(
        'post_status' => array( 'publish', 'private'),
        'order_by' => array( 'post_status' => 'ASC')
    );
    $query = new WP_Query( $args ); 
    
    $posts = $query->posts;
    

    但这并没有改变我的职位顺序。

    以下是我如何展示我的帖子

    <?php if (have_posts()) : ?>
            <?php while ($query->have_posts()) : $query->the_post(); ?>
                 <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                    <div class="title">
                        <h2><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                    </div>
                        <div class="meta"><a href="<?php the_permalink() ?>"><?php the_time('d. F Y') ?></a> &middot; <?php comments_popup_link(__('Write a comment', 'picolight'), __('1 comment', 'picolight'), __('% comments', 'picolight')); ?>
                        <?php
                            picolight_show_categories();
                            picolight_show_tags();
                        ?>
                        <?php edit_post_link( __( '(Edit)', 'picolight' ), '<span class="edit-link">', '</span>' ); ?></div>
    
                    <div class="entry">
                        <?php if ( has_post_thumbnail() ) { ?>
    
                        <div class="thumbnail">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                                <?php the_post_thumbnail(); ?>
                            </a>
                        </div>
                        <div class="indexexzerpt">
                            <?php the_content(__('More &raquo;', 'picolight')); ?>
                        </div>
                        <?php }
                        else {
                            the_content(__('More &raquo;', 'picolight'));
                        } ?>
                        <?php if(wp_link_pages('echo=0') != "") { 
                            echo '<div class="pagelinks">';
                            wp_link_pages();
                            echo '</div>';
                        } ?>
                    </div>
                </div>
    
            <?php endwhile; ?>
            <?php 
            if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
            else { 
            ?>
            <div class="navigation">
                <div class="alignleft"><?php next_posts_link(__('&laquo;  Older articles', 'picolight')); ?></div>
                <div class="alignright"><?php previous_posts_link(__('Newer articles &raquo;', 'picolight')); ?></div>
            </div>
    
    
            <?php } ?>
    
        <?php else : ?>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Eriks Klotins    6 年前

    尝试此操作(ORDER BY子句中的字段错误):

    $args = array(
     'post_status' => array( 'publish', 'private'),
     'order_by' => array( 'post_date' => 'ASC')
    );
    $query = new WP_Query( $args ); 
    
    $posts = $query->posts;