phpwordpresssorting

wordpress Sort random data already sorted ascending order


My requirement for example - I have 20 posts (wordpress) - i need to 10 post from ascending order (sorting from custom meta key) - Then i need to display random 1 post from already sorted data ( from 10 posts)

My code in 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 );

in template page

$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 );

My result : get 1 post from 10 ascending post: but i need 1 random from this 10 post


Solution

  • Solved this issues i have added following code in 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 );
    

    and template page

    $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 );
    

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