【问题标题】:Order By with have_post wordpress使用 have_post wordpress 订购
【发布时间】:2014-05-22 02:47:12
【问题描述】:

我有一些对我来说具有更高优先级的帖子 ID 数组的 ID。

现在在列出所有帖子时,我希望数组 id 应该排在第一位,然后是所有其他 id 帖子。

我已经为该顺序设置了逻辑,就像我有两个 id (15,19) 的数组然后我想要这两个帖子首先然后其他所有 id 帖子应该来。

我的逻辑如下,效果很好。

按情况订购 当 id 在 (15,19) THEN 0 其他标识 结束

但是我怎样才能用 have_posts 循环顺序添加这个逻辑。

请帮帮我。

【问题讨论】:

  • 这是自定义查询吗?这些 ID 是静态的吗,这意味着您知道它们并且它们不会改变?您是否依赖任何元数据进行订购?你能发布你的循环代码吗?
  • 是的,当我使用自定义查询添加它时,它可以工作。是的,我的 id 是静态的。它们不会改变。我不依赖任何元数据。我想用 post-id 订购。代码是下面 我在这个循环中做所有的事情。
  • 也许您可以使用post__in 换成WP_Query - 查看此[支持文章](_listing_year)

标签: php wordpress


【解决方案1】:

快速考虑一下,我会创建两个循环,一个是您想要在开头修复的帖子,第二个是其他帖子,如下所示:

 $fixed = array(8,14); 
 //gets only the posts with IDs 8 and 14
    $fixed_args = array(
        'post__in' => $fixed
    );
    $fixed_query = new WP_Query($fixed_args);

    if($fixed_query->have_posts()){
        while ($fixed_query->have_posts()){
            $fixed_query->the_post();
            the_title();
            the_content();                
        }
    }
    //excludes the posts with IDs 8 and 14
    $query_args = array(
        'post__not_in' => $fixed
    );
    $the_query = new WP_Query($query_args); 
    if($the_query->have_posts()){
        while ($the_query->have_posts()){
            $the_query->the_post();   
            the_title();
            the_content();     
        }
    }

我不知道是否有一种方法可以在一个简单的循环中做到这一点,但如果我发现任何东西,我会调查并更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2023-01-14
    • 2011-08-03
    • 1970-01-01
    • 2020-04-25
    • 2013-01-06
    • 2022-01-02
    相关资源
    最近更新 更多