Skip to content

@chrisguitarguy /cpt-on-home.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Put a custom post type on your WordPress home page.
<?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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.