The right way to display pages on homepage for WordPress, or display any custom post type content on homepage, or display pages with posts on homepage! Easy way and simple.
How To Display Pages on Homepage
Vey easy, using pre_get_posts Filter, we will display pages on homepage, or display any custom post type content on homepage, or display pages with posts on homepage, and you can do more.
Display Pages on Homepage
Copy function code and paste it in your “functions.php” file:
function WPTime_pages_on_homepage($query){ if( $query->is_home() ){ $query->set('post_type', 'page'); } } add_filter('pre_get_posts', 'WPTime_pages_on_homepage');
Now will be display our pages on homepage.
Display Custom Post Type Content on Homepage
Same the previous code, change “post_type_name_here” to your custom post type name:
function WPTime_display_custom_post_type_content_on_homepage($query){ if( $query->is_home() ){ $query->set('post_type', 'post_type_name_here'); // or add array("custom_post_type1", "custom_post_type2) to display content of 2 custom post type on homepage! } } add_filter('pre_get_posts', 'WPTime_display_custom_post_type_content_on_homepage');
Display Pages with Posts on Homepage
Same the previous code, the difference in array:
function WPTime_display_posts_and_pages_on_homepage($query){ if( $query->is_home() ){ $query->set('post_type', array("post", "page")); // or add array("post", "pages", "custom_post_type_name_here") to display posts and pages and custom post type content on homepage! } } add_filter('pre_get_posts', 'WPTime_display_posts_and_pages_on_homepage');
Note
This pre_get_posts is Filter, if you want Action, read about pre_get_posts Action.
Enjoy.