无法订购wordpress帖子

时间:2017-12-02 20:14:27

标签: php wordpress

我有我正在使用的代码,我正在尝试按照$menus数组中ID的顺序获取帖子但是不这样做,他给了我发布的帖子从最新到最旧...我尝试将orderDESC一起使用,但数组没有改变。

$menus = array(105, 54, 111);
       $args = array(
        'post__in' => $menus,
        'orderby' => 'ID',
        'order'   => 'DESC',
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) :
    $i = 1;
    while ( $query->have_posts() ) : $query->the_post();
    do_action('fwp_before_post_content');
        get_template_part('extend-helpers/' . $layout);
    do_action('fwp_after_post_content');
    $i++;
    endwhile;
    else:
        get_template_part('extend-helpers/content', 'none');
    endif;

更新:

这些帖子来自不同的类别。

更新II:

SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (105,54,111) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.menu_order, FIELD( wp_posts.ID, 105,54,111 ) LIMIT 0, 1000" 

为什么我有ORDER BY wp_posts.menu_order?因为在$args我没有orderby menu_order ..

3 个答案:

答案 0 :(得分:0)

您需要添加" orderby"你传递WP_Query的args中的项目。

You can find the documentation here

答案 1 :(得分:0)

你见过WP_query循环吗? https://code.tutsplus.com/tutorials/mastering-wp_query-using-the-loop--cms-23031。它使用$ args = ASC和DSC以及' orderby' =>' ID'太

答案 2 :(得分:0)

对于第一部分,您想要在Juan的评论中建议使用 post__in in orderby line 。从文档:'post__in' - 保留'post__in'数组中给出的帖子ID顺序。因此,您的代码应如下所示

    $menus = array(55, 53, 57); // I have used mine ids
    $args = array(
        'post__in' => $menus,
        'orderby' => 'post__in',
    );
    $query = new WP_Query( $args );

对于第二部分,更重要的是,为什么你有ORDER BY wp_posts.menu_order。您也可以在评论中找到答案。查询被某些内容(主题或插件)更改,检查pre_get_posts挂钩,尝试切换主题等。

我已经测试了这段代码并且它正在运行,所以它是在数组$菜单中订购帖子。