简体   繁体   English

Wordpress 获得已发布和私人帖子,然后订购它们

[英]Wordpress get published and private posts, then order them

I have this code here that gets my published and private posts:我在这里有这个代码,可以获取我发布的和私人的帖子:

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

$posts = $query->posts;

Now I am trying to order them by post_status so the private ones are below the published ones and then order by date.现在我试图通过 post_status 订购它们,所以私有的低于发布的,然后按日期订购。 My question is how would I do this?我的问题是我将如何做到这一点?

I tried this:我试过这个:

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

$posts = $query->posts;

But it did not change the order of my posts.但这并没有改变我发帖的顺序。

Here is how I am displaying my post这是我显示帖子的方式

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

Try this (you had a wrong field in your order by clause):试试这个(你的 order by 子句中有一个错误的字段):

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

$posts = $query->posts;

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

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