public
Created

Put a custom post type on your WordPress home page.

  • Download Gist
cpt-on-home.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?php
/*
Plugin Name: CPT On Home Page
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
*/
 
/**
* Change this to be what you want to include on the home page.
*
* eg define( 'CDUD_TYPE', 'your_post_type' );
*/
define( 'CDUD_TYPE', 'page' );
 
add_filter( 'parse_query', 'cd_wprirc1_parse_query' );
/**
* Hook into parse query, which gets fired before posts are fetched.
* Check to see if we're on the home page AND if this is the main loop
* before changing anything.
*
* $query is passed in by reference (like a pointer in C), so you can
* change it in place. No need to "return" it.
*/
function cd_wprirc1_parse_query( $query )
{
if( ! $query->is_main_query() ) return;
if( is_home() || is_search() || is_tax() || is_category() || is_tag() || is_archive() ) {
$query->query_vars['post_type'] = array( 'post', CDUD_TYPE );
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.