wordpress排序随机数据已按升序排序

时间:2017-08-07 01:19:25

标签: wordpress

我的要求 例如 - 我有20个帖子(wordpress) - 我需要从升序中发布10个帖子(从自定义元键排序) - 然后我需要从已经排序的数据(从10个帖子)中随机显示1个帖子

我在function.php中的代码

add_filter( 'the_posts', function( $posts, WP_Query $query )
{
    if( $pick = $query->get( '_shuffle_and_pick' ) )
    {
        shuffle( $posts );
        $posts = array_slice( $posts, 0, (int) $pick );
    }
    return $posts;
}, 10, 2 );

在模板页面

$args =array('post_type'  => 'products',
                    'posts_per_page'        => 10,
                    'meta_key' => '_showoption_',
                    'orderby' => 'meta_value', 
                    'order'                 => 'asc',
                    'no_found_rows'         => 'true',
                    '_shuffle_and_pick'     => 1 ,  
                );
                $my_query = new WP_Query( $args );

我的结果:从10个帖子中获得1个帖子:但我需要从这10个帖子中随机发送一个

1 个答案:

答案 0 :(得分:0)

解决了这个问题,我添加了以下代码 在function.php中

function flatten_array( $arg ) 
{
    return is_array( $arg ) 
        ? 
        array_reduce( $arg, function ( $c, $a ) 
            { 
                return array_merge( $c, flatten_array( $a ) ); 
            }, array() ) 
        : 
        array($arg);
}

// The the_posts filter
add_filter( 'the_posts', function ( $posts, $q )
{
    // First we need remove our filter
    remove_filter( current_filter(), __FUNCTION__ );

    // Check if our custom parameter is set and is true, if not, bail early
    if ( true !== $q->get( 'wpse_custom_sort' ) )
        return $posts; 

    // Before we do any work, and to avoid WSOD, make sure that the flatten_array function exists
    if ( !function_exists( 'flatten_array' ) )
        return $posts;

    // Our custom parameter is available and true, lets continue
    // Loop through the posts
    $major_array = array();
    foreach ( $posts as $p ) {
        $meta = get_post_meta(
            $p->ID,
            '_shuffle_and_pick',
            true
        );

        // Bail if we do not have a $meta value, just to be safe
        if ( !$meta )
            continue;

        // Sanitize the value
        $meta = filter_var( $meta, FILTER_SANITIZE_STRING );

        // Lets build our array
        $major_array[$meta][] = $p;
    }

    // Make sure we have a value for $major_array, if not, bail
    if ( !$major_array )
        return $posts;

    // Lets randomize the posts under each custom field
    $sorted = array();
    foreach ( $major_array as $field ) 
        $sorted[] = shuffle( $field );

    // Lets flatten and return the array of posts
    $posts = flatten_array( $sorted );

    return array_values( $posts );
}, 10, 2 );

和模板页面

$args =array('post_type'  => 'products',
                    'posts_per_page'        => 10,
                    'meta_key' => '_showoption_',
                    'wpse_custom_sort' => true,
                    'orderby' => 'meta_value', 
                    'order'                 => 'asc',
                    'no_found_rows'         => 'true',
                    '_shuffle_and_pick'     => 1 ,  
                );
                $my_query = new WP_Query( $args );

来源:https://wordpress.stackexchange.com/questions/226229/random-sort-within-an-already-sorted-query