Wordpress: Display Random Posts
Keep your site's content fresh and engaging by displaying random posts with this customizable WordPress snippet.
Enhancing user engagement on your WordPress site can be achieved by displaying random posts, which helps in promoting older content and improving content discovery. This snippet fetches and displays random posts within your site.
The WP_Query
object is utilized with the 'orderby' parameter set to 'rand', allowing for the retrieval of posts in a random order each time the page is loaded.
This approach is ideal for sidebars, footers, or dedicated sections on the homepage where keeping content fresh and engaging is crucial.
The snippet includes PHP code that can be inserted into a widget or directly into theme files, complete with a loop that outputs the titles and links of random posts.
Best practices on how to limit the number of posts retrieved and caching techniques to optimize performance when using random post queries are also discussed.
Snippet Code
Version Compatibility
This snippet is compatible with WordPress 2.5 and later, where WP_Query began supporting the 'rand' orderby parameter.
Required PHP Version
PHP 5.2 or higher
Wordpress Core Functions Used
Use Cases
- content discovery
- user engagement
- dynamic content presentation