WordPress 以任意顺序获取特定帖子

发布于 2024-10-05 21:54:11 字数 686 浏览 2 评论 0原文

现在,我正在做:

$posts = get_posts(array('post_type' => 'page', 'post__in' => array(1, 3, 2, 9, 7)));

并且遇到两个问题:

  1. Post 3 的内容是 'post_type' => 'post',所以它没有被选中,但我想要它!如果我省略 'post_type' =>; 'page',则仅选择帖子 3(因为它必须假定 'post_type' => 'post'。)。
  2. 我希望能够按帖子的 ID 任意排序 。如果我知道如何使用 MySQL,我可以这样做:

    从 wp_posts 中选择 *,其中 ID IN (1, 3, 2, 9, 7)
    ORDER BY FIND_IN_SET(ID, '1,3,2,9,7');
    

但是,我应该如何使用 WordPress 来做到这一点?

Right now, I'm doing:

$posts = get_posts(array('post_type' => 'page', 'post__in' => array(1, 3, 2, 9, 7)));

and am having two issues:

  1. Post 3 is of 'post_type' => 'post', so it doesn't get selected, but I want it! If I leave out 'post_type' => 'page', then only post 3 is selected (because it must assume 'post_type' => 'post'.).
  2. I want to be able to order the posts arbitrarily by their ids. If I knew how to use MySQL, I could do:

    SELECT * FROM wp_posts WHERE ID IN (1, 3, 2, 9, 7)
    ORDER BY FIND_IN_SET(ID, '1,3,2,9,7');
    

But, how should I do this with WordPress?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏の忆 2024-10-12 21:54:11

首先通过 ID 任意获取所有帖子,然后循环遍历所有帖子

您可以这样做:-

$posts=$wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE ID IN (1, 3, 2, 9, 7)
ORDER BY FIND_IN_SET(ID, '1,3,2,9,7')");
$count=count($posts);
for ($counter=0 ; $counter < $count; $counter++)
{
   $post=get_post( $posts[$counter]->ID, $output );
  //do your stuffs with posts
}

希望这会有所帮助

First fetch all posts arbitrarily by their ids and then loop through all the posts

You can do in this way:-

$posts=$wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE ID IN (1, 3, 2, 9, 7)
ORDER BY FIND_IN_SET(ID, '1,3,2,9,7')");
$count=count($posts);
for ($counter=0 ; $counter < $count; $counter++)
{
   $post=get_post( $posts[$counter]->ID, $output );
  //do your stuffs with posts
}

Hope this helps

垂暮老矣 2024-10-12 21:54:11
  1. #wordpress IRC 频道上的 Kawauso 告诉我“post_type 采用一组值”。由此,我发现以下内容也选择了帖子 3:

  2. 因此,我执行了以下操作:

    $post_ids = array(1 => 0, 3 => 1, 2 => 2, 9 => 3, 7 => 4);
    $posts = get_posts(array('post_type' => array('post', 'page'),
                             'post__in'=>; array_keys($post_ids)));
    $ordered_posts = 数组(0,0,0,0,0); // 大小为五;维持秩序
    foreach ($posts as $p) {
      setup_postdata($p);
      $ordered_posts[$post_ids[$p->ID]] = array(
        '永久链接' => get_permalink($p->ID),
        '标题' => $p->post_title,
        '摘录' =>获取摘录(),
        '日期' => date('F j, Y', strtotime($p->post_date)));
    }
    
  1. Kawauso on the #wordpress IRC channel informed me that "post_type takes an array of values." From that, I found that the following also selects post 3:

  2. So, I did the following:

    $post_ids = array(1 => 0, 3 => 1, 2 => 2, 9 => 3, 7 => 4);
    $posts = get_posts(array('post_type' => array('post', 'page'),
                             'post__in' => array_keys($post_ids)));
    $ordered_posts = array(0,0,0,0,0); // size of five; keeps order
    foreach ($posts as $p) {
      setup_postdata($p);
      $ordered_posts[$post_ids[$p->ID]] = array(
        'permalink' => get_permalink($p->ID),
        'title' => $p->post_title,
        'excerpt' => get_the_excerpt(),
        'date' => date('F j, Y', strtotime($p->post_date)));
    }
    
嘿咻 2024-10-12 21:54:11

根据此线程,您可以使用此代码,然后使用经典的 WordPress 循环:

$args = array(  
'post_type'=>'page',  
    'orderby'=>'menu_order',  
    'order'=>'ASC'  
);  
query_posts($args);

According to this thread, you can use this code and then use the classic WordPress loop:

$args = array(  
'post_type'=>'page',  
    'orderby'=>'menu_order',  
    'order'=>'ASC'  
);  
query_posts($args);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文