WP_Query ; get one random post out of the last 20; am I doing this right?

On our website, WebMaster asked me to config a wordpress widget so it would get a random post out of the last 20 that were submitted for a given category.

I managed to set up a WP_Query which seems to work, but as I couldn’t find any feedback on the Web about such a specific query, I am taking the opportunity to ask for you advice and see if what I managed to come with is of any good:

$query_args = array(
                array (
                    'cat' => '6',
                    'orderby' => 'date',
                    'order' => 'DSC',
                    'posts_per_page' => '20'
                ),
                'orderby' => rand,
                'showposts' => '1'
            );  
$my_query = new WP_Query($query_args); 

Best Regards from France,

Read more here: WP_Query ; get one random post out of the last 20; am I doing this right?

Leave a Reply

Your email address will not be published. Required fields are marked *