DEV Community

Cover image for How to show random posts in wordpress sidebar
Chetan Rohilla
Chetan Rohilla

Posted on • Updated on • Originally published at w3courses.org

How to show random posts in wordpress sidebar

Showing wordpress posts in sidebar is a great way to increase the number of page views in our website or blog. It also helps our website to increase the page rank, SEO and website rank.

In wordpress we can show the posts in many ways in wordpress sidebar or content page. For an example we can show posts in wordpress by date, posts in wordpress by category, posts in wordpress by views, posts in wordpress by likes, posts in wordpress by tags, posts in wordpress by number shares on social media, posts in wordpress by random order etc. Here we will show our wordpress posts by random order with and without plugin.

Show Random Posts in WordPress Sidebar With Plugin

If you want to show the wordpress posts in sidebar by random order, or by category or tags, or by date then you can use the wordpress plugin Advanced Random Posts Widget. This plugin will enable a custom, flexible and advanced random posts. It allows you to display a list of random posts via shortcode or widget with thumbnail, excerpt and post date, also you can display it from all or specific or multiple taxonomy.

Show Random Posts in WordPress Sidebar Without Plugin

If you want to show your random posts in wordpress sidebar without plugin. Then paste the code given below in your functions.php file.

    function rmc_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 .= '<ul>';
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
                $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
            }
            $string .= '</ul>';
            /* Restore original Post Data */
            wp_reset_postdata();
        } else {

            $string .= 'no posts found';
        }

        return $string; 
    } 

    add_shortcode('rmc_random_posts','rmc_rand_posts');
    add_filter('widget_text', 'do_shortcode');
Enter fullscreen mode Exit fullscreen mode

In the code above we have the shortcode rmc_random_posts. Now just add the text widget and paste the code [rmc_random_posts] in your wordpress sidebar. You can also add this shortcode in your wordpress template. You can read this article how to use shortcode in template with how to create custom plugin in wordpress.

Code Explanation:

  • In the code above we have created the function rmc_rand_posts in which we have run our custom wordpress query.
  • We have add rmc_rand_posts function in our custom shortcode rmc_random_posts.
  • We also used the function add_shortcode which creates the shortcode [rmc_random_posts].
  • Here we added the wordpress filter widget_text which filters the content of text widget. And we applied the widget_text filter to do_shortcode which removes any html tags from our shortcode.
  • We used the post_type is post and you can use your own custom post type. Read our article custom post type in wordpress.
  • We are fetching posts orderby rand.
  • If you want to show custom posts in wordpress sidebar, show posts in wordpress by taxonomy, show posts in wordpress by category, tags, comments shares, author etc. Then Read in detail WP_Query in wordpress.

Please like share subscribe and give positive feedback to motivate me to write more for you.

For more tutorials please visit my website.

Thanks:)
Happy Coding:)

Top comments (0)