How to Include Pages into the WordPress Homepage Posts List

Sometimes you want to have listed on your WordPress homepage both posts and pages. By default, WordPress only lists and paginates posts.

It’s pretty simple to add the pages to the list, you only have to add a few lines of code to your theme’s functions.php file:

// ...

add_filter( 'pre_get_posts', 'get_posts_and_pages' );

function get_posts_and_pages( $query ) {
    if ( is_home() && false == $query->query_vars['suppress_filters'] ) {
        $query->set( 'post_type', array( 'post', 'page') );
        $query->set( 'post__not_in', array(781, 773, 516) );
    }
    return $query;
}

As you notice, we also excluded some pages from the list using the post ID. You will need this in order to exclude some specific pages, like contact or privacy policy, from the homepage listing.

Hope this helps you making a better WordPress site. Please let me know in the comments if you have ant trouble setting this up or if you know of a bettor solution.

Have a nice and productive day!

Leave a Reply

Your email address will not be published. Required fields are marked *