【问题标题】:WP_Query('orderby=post_date') not working with wordpressWP_Query('orderby=post_date') 不适用于 wordpress
【发布时间】:2010-12-25 11:18:20
【问题描述】:

WP_Query('orderby=post_date') 无法使用 wordpress。

如何按降序排列我的帖子?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    还有一个注意事项,我拔掉了头发,因为我一直在列表中获得相同的项目,而不是我意识到我需要添加:

    'ignore_sticky_posts' => true,
    

    我希望其他人会在我之前注意到这一点。

    【讨论】:

    • 我可以在哪个文件中添加这些更改?
    • 您将其添加到与查询相同的文件中,在 args 中。
    • 这对我有用。
    【解决方案2】:

    以下 3 个参数将为您提供自发布之日起按升序排列的帖子(即,较旧的帖子将首先显示)

    'post_status' => 'publish', 'orderby' => 'publish_date', 'order' => 'ASC'

    当您将 order 更改为 DESC 时,您将从发布之日起按降序获取帖子(即,最新帖子将首先显示) )

    'post_status' => 'publish', 'orderby' => 'publish_date', 'order' => 'DESC'

    <?php
    $postsPerPage = 10;
    $page = 1;
    ?>
    <?php
    $query = new WP_Query(array(
        'cat' => 4,
        'post_status' => 'publish',
        'orderby' => 'publish_date',
        'order' => 'ASC',
        'paged' => $page,
        'posts_per_page' => $postsPerPage));
    ?>
    

    【讨论】:

      【解决方案3】:
      WP_Query('orderby=date&order=DESC') 
      

      【讨论】:

      • 有一个我不知道的测试,如果它不是你要找的,那么看看 $wpdb 并运行你自己的查询
      • 这里是列的列表,按描述排序:codex.wordpress.org/Class_Reference/…
      • 然后在哪里写入 $args 变量
      • @AllenGingrich 请解释原因?
      【解决方案4】:

      如果您使用PostTypesOrder plugin,它可能会全局修改您的查询,以避免针对特定帖子类型的这种行为

      add_filter('pto/posts_orderby/ignore', 'theme_pto_posts_orderby', 10, 3);
      
      function theme_pto_posts_orderby($ignore, $orderBy, $query)
      {
             if( (! is_array($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post') ||
             (is_array($query->query_vars)   &&  in_array('post', $query->query_vars)))
                     $ignore = TRUE;
             return $ignore;
      }
      

      【讨论】:

      • 您也可以在查询中使用:'ignore_custom_sort' =&gt; true
      【解决方案5】:

      试试这个

      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          $args = array(
          "post_type" => "post",
            "post_status" => "publish",
            "paged" => $paged,
            "orderby" => "date",
            "order" => 'ASC'
             );
           WP_Query($args);
      

      【讨论】:

      • 这是我正在寻找的方法
      【解决方案6】:

      要在修改日期之前订购,请使用orderby=modified

      WP_Query( 'orderby=modified&order=DESC' ) 
      

      请参阅the documentation 了解更多可能的值。

      【讨论】:

        猜你喜欢
        • 2012-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-20
        • 2017-02-08
        • 2016-06-18
        • 2016-06-15
        • 2020-03-18
        相关资源
        最近更新 更多