Only Show Specific Pages or Posts On Home Page

This code enables you to select which single pages you want displayed on the front page of your WordPress site. It excludes all posts and only shows pages.

Single Page by I.D #

The following code displays a single page with the ID of 1594 on your home page.

add_filter( 'pre_get_posts', 'display_page_only_in_loop' );

function display_page_only_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {
  
  $query->set('post_type', array( 'page' ) );
  
  $query->set('post__in', array( '1594' ) );
  
  }
  
}

Single Post by I.D #

The following code displays a single post with the ID of 68356 on your home page.

add_filter( 'pre_get_posts', 'display_post_only_in_loop' );

function display_post_only_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {
  
  $query->set('post__in', array( '68356' ) );
  
  }
  
}

Multiple Single Pages by I.D #

To display multiple single pages on your home page, use a comma separated array of page id’s like this :

add_filter( 'pre_get_posts', 'include_pages_in_loop' );

function include_pages_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {

    $query->set('post_type', array( 'page' ) );
  
    $query->set('post__in', array( 1594, 852 ) );
  
  }
  
}

Swap out the page id’s in the above code from 1594, 852 to your own page ID’s.

Multiple Single Posts by I.D #

To display multiple single posts on your home page, use a comma separated array of post id’s like this :

add_filter( 'pre_get_posts', 'include_posts_in_loop' );

function include_posts_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {
  
    $query->set('post__in', array( 1, 2 ) );
  
  }
  
}

Swap out the post id’s in the above code from 1, 2 to your own post ID’s.

Multiple Single Pages & Posts by I.D #

To display multiple single posts & single pages on your home page, use a comma separated array of post and page id’s like this :

add_filter( 'pre_get_posts', 'include_pages_posts_in_loop' );

function include_pages_posts_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {

    $query->set('post_type', array( 'post', 'page' ) );
  
    $query->set('post__in', array( 1594, 852, 68356, 68353 ) );
  
  }
  
}

Swap out the post & page id’s in the above code from 1594, 852, 68356, 68353 to your own post ID’s.

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.