function _rand_posts() {
$args = array(
‘post_type’ => ‘post’,
‘orderby’ => ‘rand’,
‘posts_per_page’ => 5,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$string .= ‘
- ‘;
- ‘. get_the_title() .’
while ( $the_query->have_posts() ) {
$the_query->the_post();
$string .= ‘
‘;
}
$string .= ‘
‘;
/* Restore original Post Data */
wp_reset_postdata();
} else {
$string .= ‘no posts found’;
}
return $string;
}
add_shortcode(‘random-posts’,’_rand_posts’);
add_filter(‘widget_text’, ‘do_shortcode’);