Site icon WP Smith

How to Include Another Post Type and Custom Taxonomy on the Genesis Blog Page

So recently, someone asked me to help them assimilate two post types on the Genesis blog page. Since because of the Genesis loops, this becomes quite easy. So here's what we need to do:

  1. Setup the Template Name
  2. Create the Custom Loop
  3. Modify the Post Meta
  4. Customize the Content Display

Setup the Template Name

First and as always, we need to set up the template. You can start by scratch, which this tutorial will do, or you can copy page_blog.php from the genesis folder.
[php]<?php
/**

Template Name: Blog

*/
[/php]

You may want to change the template name to something different so there aren't any confusions with Genesis's version. So something like this would work:
[php]<?php
/**

Template Name: Custom Blog

*/
[/php]

Create the Custom Loop

Now, I have a site with multiple custom post types and what I want to do is to combine two of those post types (post and wps_videos) into the blog feed. Then I want to limit them based on the Genesis category settings as well as incorporate the Video Category (wps_vidcats) in the same manner.

So first, I have to unhook the genesis standard loop.
[php]
remove_action('genesis_loop', 'genesis_do_loop');
[/php]

Then I add my own loop with its custom function.
[php]
add_action( 'genesis_loop', 'wps_custom_do_loop' , 5 );
function wps_custom_do_loop() {
$include = genesis_get_option( 'blog_cat' );
$exclude = genesis_get_option( 'blog_cat_exclude' ) ? explode( ',' , str_replace ( ' ' , '' , genesis_get_option( 'blog_cat_exclude' ) ) ) : '';
[/php]

These lines are right out of the Genesis standard loop. They pull the information set on the Theme Settings page and place them in variables to be used later. However, these are limited to just categories. Now, it is important to note that $include only contains one category id and $exclude contains an array of categories (even if there is only one).

Now, I need to build my $args. In my args, I want to add my post types as well as build my taxonomy query. So continuing inside wps_custom_do_loop:
[php]
$args = array(
'showposts' => genesis_get_option('blog_cat_num'),
'post_type' => array( 'post' , 'wps_videos' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'wps_vidcategory',
'field' => 'id',
'terms' => array( 92 )
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $include )
),
)
);
[/php]

So let me break this down for you. 'showposts' pulls from the Genesis Theme Settings page. 'post_type' is where we add the registered names of all the post types we would like to include in an array. 'tax_query' is where I limit the query based on my requirements. Now I wanted to only include posts from one category, which was set on the Genesis Theme Settings page, and I also wanted to do likewise with my videos and include videos from one specific Video Category (here term ID of 92). And since posts and videos do not share the same taxonomy, the tax_query is simple. So, in summary, I wanted posts from one category ($include) OR (and that's my relation argument) posts from my custom taxonomy video category (here 92).

Now, if I wanted, I could replace the last array with a different array to allow for all categories except (again based on category ids entered in Genesis Theme Settings). It would look something like this:
[php]
$args = array(
'showposts' => genesis_get_option('blog_cat_num'),
'post_type' => array( 'post' , 'wps_videos' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'wps_vidcategory',
'field' => 'id',
'terms' => array( 92 )
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $exclude,
'operator' => 'NOT IN'
),
)
);
[/php]

Now, we can finish the function by calling the Genesis Custom Loop.
[php]

genesis_custom_loop( $args );
}
[/php]

So all together the function looks like this (to simply limit posts to one category and vids from another category):
[php]<?php
remove_action('genesis_loop', 'genesis_do_loop');
add_action( 'genesis_loop', 'custom_do_loop' , 5 );

function custom_do_loop() {
$include = genesis_get_option('blog_cat');
$exclude = genesis_get_option('blog_cat_exclude') ? explode(',', str_replace(' ', '', genesis_get_option('blog_cat_exclude'))) : '';
$cf = genesis_get_custom_field('query_args'); /** Easter Egg **/
$args = array(
'showposts' => genesis_get_option('blog_cat_num'),
'post_type' => array( 'post' , 'wps_videos' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'wps_vidcategory',
'field' => 'id',
'terms' => array( 92 )
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $include )
),
)
);
genesis_custom_loop( $args );
}
[/php]

Modify the Post Meta

Now, because my videos do not share the same taxonomy with posts (and it could have [dare I say, should have] but the client didn't want it that way). My posts on the blog page that are from the videos custom post type won't have any post meta. I could simply remove it, but since the client wanted it, I simply filter them.

To filter the post meta to include terms from categories and my video categories, simply do a basic if-then statement. Normally Genesis has $post_meta = '[ post_categories] [ post_tags]';. However, I wanted to change it to just display categories.
[php]<?php
/** Customize the post meta function */
add_filter( 'genesis_post_meta' , 'post_meta_filter' );
function post_meta_filter( $post_meta ) {
global $post;
if ( $post->post_type == 'wps_videos' ) {
$vid_cats = get_the_term_list( $post->ID, 'wps_vidcategory' , '' , ', ' , '' );
$post_meta = sprintf( '<span class="categories">%2$s%1$s</span> ', $vid_cats, 'Filed Under: ' );
}
else {
$post_meta = 'Filed Under: Custom Post Types, Genesis, Tutorials, WordPress';
}
return $post_meta;
}
[/php]

Customize the Content Display

Now, because of UX, the video custom post type had a metabox to make displaying the videos consistent. However, if we do nothing then the videos never appear, which is not what we want.

First, I must unhook the standard Genesis content function.
[php]
/** Customize the content */
remove_action( 'genesis_post_content', 'genesis_do_post_content' );
[/php]

Second, I write a basic if-then function to display my videos on the blog page.
[php]<?php
add_action( 'genesis_post_content', 'custom_do_post_content' );
function custom_do_post_content() {
global $post;
if ( $post->post_type == 'wps_videos') {
if( genesis_get_custom_field('_wps_videoembedcode') != '' ) {
?>
<div id="post-<?php the_ID(); ?>" class="video-entry">
<div class="video-embed">
<?php echo genesis_get_custom_field('_wps_videoembedcode'); ?>
</div><!-- end .video-embed -->
<div class="clear"></div>
<?php genesis_do_post_content(); ?>
</div><!-- end .video-entry -->
<?php
}
} //end wps_videos
else {
genesis_do_post_content();
}
}
[/php]

Now, in both, I simply referred back to the genesis_do_post_content() because I just wanted a way to insert my videos for posts with the video custom post type.

Now as with any Genesis template, to initiate the framework, we have to add the Genesis call.
[php]<?php
genesis();
?>[/php]

All Together

[php]<?php
/**

Template Name: Custom Blog

*/

// Custom Loop
remove_action('genesis_loop', 'genesis_do_loop');
add_action( 'genesis_loop', 'custom_do_loop' , 5 );
function custom_do_loop() {
$include = genesis_get_option('blog_cat');
$exclude = genesis_get_option('blog_cat_exclude') ? explode(',', str_replace(' ', '', genesis_get_option('blog_cat_exclude'))) : '';
$cf = genesis_get_custom_field('query_args'); /** Easter Egg **/
$args = array(
'showposts' => genesis_get_option('blog_cat_num'),
'post_type' => array( 'post' , 'wps_videos' ),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'wps_vidcategory',
'field' => 'id',
'terms' => array( 92 )
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $include )
),
)
);
genesis_custom_loop( $args );
}

// Customize the post meta function
add_filter( 'genesis_post_meta' , 'post_meta_filter' );
function post_meta_filter( $post_meta ) {
global $post;
if ( $post->post_type == 'wps_videos' ) {
$vid_cats = get_the_term_list( $post->ID, 'wps_vidcategory' , '' , ', ' , '' );
$post_meta = sprintf( '<span class="categories">%2$s%1$s</span> ', $vid_cats, 'Filed Under: ' );
}
else {
$post_meta = 'Filed Under: Custom Post Types, Genesis, Tutorials, WordPress';
}
return $post_meta;
}

// Customize the content
remove_action( 'genesis_post_content', 'genesis_do_post_content' );
add_action( 'genesis_post_content', 'custom_do_post_content' );
function custom_do_post_content() {
global $post;
if ( $post->post_type == 'wps_videos') {
if( genesis_get_custom_field('_wps_videoembedcode') != '' ) {
?>
<div id="post-<?php the_ID(); ?>" class="video-entry">
<div class="video-embed">
<?php echo genesis_get_custom_field('_wps_videoembedcode'); ?>
</div><!-- end .video-embed -->
<div class="clear"></div>
<?php genesis_do_post_content(); ?>
</div><!-- end .video-entry -->
<?php
}
} //end wps_videos
else {
genesis_do_post_content();
}
}

genesis();
?>
[/php]