• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2665

Original Genesis Tutorials & 5000+ Guaranteed Code

Snippets

  • Support
  • Newsletter
  • Videos
  • Log in

Premium Member? - Request custom code

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.

pre_get_posts

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Recent Posts

  • Checkbox to Remove Specific Posts & Pages From Your Home Page Loop
  • Menu Logo Menu Using CSS Grid In New Versions of Genesis Sample Theme
  • Featured Posts With Pagination In Widget Area Using Business Pro
  • How To Translate the Property Details Additional Features Text In AgentPress Pro
  • Unlink All Archive Links For Each Entry On Per Post Basis

Advertise · WPEngine · Genesis · Log in

  • How Premium Membership Works
  • Sign Up
  • Support
  • Subscription Details/Invoice
  • Tagged Tutorials
  • Access-Download Problems