WP Query以数字顺序随机选择帖子

时间:2022-06-28 22:44:29

I am using query_posts to return 6 ramdom posts from a custom post type using orderby=rand.

我使用query_posts使用orderby = rand从自定义帖子类型返回6个ramdom帖子。

This works fine. However. Once the query has returned the posts I need them to be sorted in numerical order.

这很好用。然而。一旦查询返回了帖子,我就需要按照数字顺序对它们进行排序。

the current code I am using looks like this:

我正在使用的当前代码如下所示:

<?php query_posts('post_type=my_post_type&orderby=rand&showposts=6'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Do stuff

<?php endwhile; else: ?>
<?php endif; ?>

So I need 6 posts to be selected at random then they need to be sorted in to numerical order. Any help would be greatly appreciated!

所以我需要随机选择6个帖子然后需要按数字顺序排序。任何帮助将不胜感激!

1 个解决方案

#1


0  

Some meta-code for you, actually getting this working will require some digging on your part but this will hopefully get on on the right track.

一些元代码对你来说,实际上让这个工作需要一些挖掘你的部分,但这将有希望在正确的轨道上。

The general idea here is that you just need to get the ids from the query_posts and then reorder them, allowing you to output them as you desire.

这里的一般想法是你只需要从query_posts获取id然后重新排序它们,允许你根据需要输出它们。

<?php 
    query_posts('post_type=my_post_type&orderby=rand&showposts=6'); 

    // Create your result array
    $postArray = array();

    if ( have_posts() ) : while ( have_posts() ) : the_post(); 

        $postArray[] = /*POST ID GOES HERE*/

    endwhile; else:
    endif;

    // Now sort it
    sort($postArray,SORT_NUMERIC);

    // Now iterate over $postArray outputting post id $postArray[0] -> $postArray[n]

?>

Another option could be to populate $postArray with $postArray[/*POST ID*/] = /* OUTPUT */; and then just iterate over $postArray as PHP probably should automatically sort it if numeric keys are used.

另一种选择可能是使用$ postArray填充$ postArray [/ * POST ID * /] = / * OUTPUT * /;然后只需迭代$ postArray,因为如果使用数字键,PHP可能会自动对它进行排序。

#1


0  

Some meta-code for you, actually getting this working will require some digging on your part but this will hopefully get on on the right track.

一些元代码对你来说,实际上让这个工作需要一些挖掘你的部分,但这将有希望在正确的轨道上。

The general idea here is that you just need to get the ids from the query_posts and then reorder them, allowing you to output them as you desire.

这里的一般想法是你只需要从query_posts获取id然后重新排序它们,允许你根据需要输出它们。

<?php 
    query_posts('post_type=my_post_type&orderby=rand&showposts=6'); 

    // Create your result array
    $postArray = array();

    if ( have_posts() ) : while ( have_posts() ) : the_post(); 

        $postArray[] = /*POST ID GOES HERE*/

    endwhile; else:
    endif;

    // Now sort it
    sort($postArray,SORT_NUMERIC);

    // Now iterate over $postArray outputting post id $postArray[0] -> $postArray[n]

?>

Another option could be to populate $postArray with $postArray[/*POST ID*/] = /* OUTPUT */; and then just iterate over $postArray as PHP probably should automatically sort it if numeric keys are used.

另一种选择可能是使用$ postArray填充$ postArray [/ * POST ID * /] = / * OUTPUT * /;然后只需迭代$ postArray,因为如果使用数字键,PHP可能会自动对它进行排序。