Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created May 19, 2015 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinsays/e3a9504881fec4ba5bc2 to your computer and use it in GitHub Desktop.
Save devinsays/e3a9504881fec4ba5bc2 to your computer and use it in GitHub Desktop.
/**
* 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' );
@devinsays
Copy link
Author

If a page is set as the home page, this won't actually work.

Turns out you need to use the ID:

$query->set( 'page_id', '2965' ); // Example ID

Rather than:

$query->set( 'pagename', 'home' ); // Page slug

@devinsays
Copy link
Author

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