Created
May 19, 2015 19:56
-
-
Save devinsays/e3a9504881fec4ba5bc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Load alternate page on front page | |
*/ | |
function prefix_query_change( $query ) { | |
// Only filter the main query on the front-end | |
if ( is_admin() || ! $query->is_main_query() ) { | |
return; | |
} | |
global $wp; | |
$front = false; | |
// If the latest posts are showing on the home page | |
if ( ( is_home() && empty( $wp->query_string ) ) ) { | |
$front = true; | |
} | |
// If a static page is set as the home page | |
if ( ( $query->get( 'page_id' ) == get_option( 'page_on_front' ) && get_option( 'page_on_front' ) ) || empty( $wp->query_string ) ) { | |
$front = true; | |
} | |
if ( $front ) : | |
$query->set( 'post_type', 'page' ); | |
$query->set( 'pagename', 'home' ); // Page slug | |
// Set properties to match a page | |
$query->is_page = 1; | |
endif; | |
} | |
add_action( 'pre_get_posts', 'prefix_query_change' ); |
Full example:
/**
* Load alternate page on front page
*/
function prefix_query_change( $query ) {
// Only filter the main query on the front-end
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
global $wp;
$front = false;
// If a static page is set as the home page
if ( ( $query->get( 'page_id' ) == get_option( 'page_on_front' ) && get_option( 'page_on_front' ) ) || empty( $wp->query_string ) ) {
$query->set( 'page_id', '2965' );
}
}
add_action( 'pre_get_posts', 'prefix_query_change' );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If a page is set as the home page, this won't actually work.
Turns out you need to use the ID:
Rather than: