Place the following code in your theme functions.php file.
function one_random_post_on_home_page( $query )
{
if ( ! ( $query->is_home() && $query->is_main_query() ) )
return;
$query->set( 'orderby ', 'rand' );
$query->set( 'posts_per_page', 1 );
}
add_action( 'pre_get_posts', 'one_random_post_on_home_page' );
I’m assuming when you say index.php you mean your blogs home page. The index.php file in your theme can be used for many other areas of your website so making any alterations in that file might have undesired effects else where.
Related Posts:
- How to check if post meta key exists or not in wordpress database
- Where is the old post permalink slug stored?
- Custom Post Types, slug, archive and SEO plugins
- get all posts ID from a category
- Portable Post Links
- Related Posts by Multiple Tags?
- Action hook ‘wp’ firing twice… why?
- Manipulate post category after time
- How can I display 7 posts on the home page, but 9 posts on the subsequent pages?
- WP REST API, query total posts in a category
- how to enable different number of revisions for different blogs when using WPMU?
- 404 error after publishing a post
- How to remove_filter that filters iframes in posts? [duplicate]
- How to edit layout of a particular post format?
- Some posts returning 404 instead of displaying post
- Showing Thumbnail from Previous and Next Posts
- How to enqueue scripts depending on post formats?
- WP_Query in functions.php overrides global $post object, even with wp_reset_query()
- WP rest api returns 404 only when author param is used
- How to add a custom field after wp post
- What Is meta_id In wp_postmeta?
- the_posts filter been called multiple time
- Submitting POST data to a custom page template triggers a 404, why?
- Reset/Reorder posts ID in the MySQL wp_posts table
- How to prevent WordPress from updating the modified time?
- Custom WP_QUERY $args
- Dynamic carousel on individual posts
- Prepend or add an Image to the content of a Post
- Remove all links in post content
- Select a specific post to display in a news box
- Having trouble with the_post_thumbnail to display thumbnail with custom size
- How to Programmatically add an empty Block to every post on wordpress site?
- I get this error message when I try to add a module to a course on my website using Sensei plugin
- Getting value from get_post_custom
- Publish post’s date is set to Nov 30 -0001
- How to exclude visited posts from loop
- Internal post link creates comments on its own
- outputting posts’ taxonomies: cant get ‘get_the_taxonomies’ working
- Disable link for single posts
- Can’t set Post ID properly
- related author post thumbnail shows post thumbnail
- Change post-slug using wordpress API? Change permalink of a post using php / jquery?
- Query for first 3 posts to change the look and feel
- Custom Post Type archive
- How to create a second posts page which client can update
- Form to post new post with custom taxonomies
- Getting blog pagination to work on page set as front page
- How to identify and hide/remove an element from a WordPress page [closed]
- unique post in “$args” of wp_query display more than one post
- New Posts and Pages Won’t Save
- Why excerpt is Displaying same in Random Posts list
- Is there a way getting post fields using `transition_post_status`
- Get featured image on hover of post title [closed]
- How get top post view for today for today post
- Get current post’s nav menu name (term name)
- Get latest author posts inside the loop
- Changing wp_link_pages() to “Next Page” and “Previous Page” buttons?
- Why my meta boxes won’t show in front post page?
- Authors’ Links on Homepage Not Going to Author Post Pages
- Approve modified posts
- Display Current Posts Category (with the most posts)?
- How can I add random alphabets in wordpress permalink posts structure
- AWS usage queries
- Filter posts by month (dropdown)
- Redirect old Posts URL to new URL
- Hook when post is set from published to draft?
- Use posts from old db by changing prefix
- Display Custom Post Category Count & WordPress Category Count Using Shortcode
- Override default post type ‘post’
- ‘posts’ table should not store the absolute image path
- How to make a page show posts only from specific categories without editing php files
- Assign a day of the week to post, e.g: Assign Monday to post and have it only appear when the day is Monday
- Redirecting user to a page when there is no content?
- Is it possible to query a category with specified posts?
- How do I set content in my pages so that it remains permanent while blog posts are added below?
- How can I alternate styling of images in a post? [closed]
- Add caption to thumbnails in content
- WordPress URL question
- Display iframe or embeded posts on homepage
- How to retrieve a post by inputing the url in a custom field and displaying it on an options page
- Theme does not respect spaces between paragraphs in pages
- How can I use the “Your latest posts” as a template?
- Query to get data of a post, if in category?
- Order posts by custom field
- exclude one child by id from $post->post_parent
- Continuous listing from a custom field
- Text editor has problem with having ” in a post
- Getting posts from multiple users?
- Add class name of content, Hypernate doesn’t work to IE and Chrome
- WordPress Post Grid
- Choose options via url
- Issue with wordpress pagination: last 2 posts show again on second page
- Extra Theme – Fit image inside of Featured Post Slider
- Stripping tag from elements in post
- find view post hyperlink in PHP wordpress admin file
- Even/Odd every two posts
- Polylang – display one post into 3 categories with different lang [closed]
- How can I show the positive and negative comments for a same post separately?
- What function does the loop of displaying posts?
- Dynamic Menu Item