Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active December 7, 2018 19:53
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save devinsays/a4bc9bf08d27be51e29e to your computer and use it in GitHub Desktop.
Save devinsays/a4bc9bf08d27be51e29e to your computer and use it in GitHub Desktop.
custom-post-types-home-page
/**
* Load custom post type archive on home page
*
* Reference: http://www.wpaustralia.org/wordpress-forums/topic/pre_get_posts-and-is_front_page/
* Reference: http://wordpress.stackexchange.com/questions/30851/how-to-use-a-custom-post-type-archive-as-front-page
*/
function prefix_downloads_front_page( $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', 'download' );
$query->set( 'page_id', '' );
// Set properties to match an archive
$query->is_page = 0;
$query->is_singular = 0;
$query->is_post_type_archive = 1;
$query->is_archive = 1;
endif;
}
add_action( 'pre_get_posts', 'prefix_downloads_front_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment