Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active August 29, 2015 14:08
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/f9d2fdcee3d5145e9d38 to your computer and use it in GitHub Desktop.
Save devinsays/f9d2fdcee3d5145e9d38 to your computer and use it in GitHub Desktop.
This displays a "directory" post type on the home page, sorted by the meta_key "last_name" in alphabetical order
<?php
/**
* Load 'directory' post type archive on home page
* Orderby the meta_key 'last_name' ASC
*
* Reference: http://wptheming.com/2014/10/displaying-a-custom-post-type-archive-on-the-front-page/
*/
function prefix_directory_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', 'directory' );
$query->set( 'page_id', '' );
$query->set( 'meta_key', 'last_name' );
$query->set( 'orderby', 'last_name' );
$query->set( 'order', 'ASC' );
// 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_directory_front_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment