Custom Post Types in Category Archive Loop and Standard Blog

Community Forums Forums Design Tips and Tricks Custom Post Types in Category Archive Loop and Standard Blog

This topic is: resolved

This topic contains 2 replies, has 2 voices, and was last updated by  haciendoeco 10 months, 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #73318

    Lord_Devi
    Participant
    Post count: 10

    Hi guys,

    So I need some help adding a custom post type I have created (“review-epilator”) to my standard Category archive loop. As well, I would like to add this custom post type to my standard Home or Blog template.

    So what I mean by Home or Blog template, is that right now my website has a standard “Latest Posts” type layout on the home page, and I would like to be able to add the custom post type to that. HOWEVER… I am planning on switching to a more static homepage, adding my “latest posts” type functionality to a page titled “Blog”.. So if there are any differences in what I need to do to implement that I would need to know.

    I have already gotten my custom post type added to my TAGS archive loop, but I do not understand the code I am using well enough to be able to modify it for the above two purposes.

    So here is the custom function code I am already using to add the custom post type to my tag archive loop to let you guys know where I currently stand. If this code can just be ENHANCED or grown to include the above two requirements, that would be preferable to me than creating two separate new functions I believe.

    /* ----------------------------------------------------------- */
    /* Add custom post type "review-epilator" to tags archive loop */
    /* ----------------------------------------------------------- */
    function review_epilator_to_tag_loop( $query ) {
    	
    	// we don't want this running on the admin side
    	if ( is_admin() )
    		return;
    
    	// include our stream type on tag pages
    	if ( is_tag() && $query->is_main_query() ) {
    
    		$query->query_vars['post_type'] = array( 'post', 'review-epilator' );
    		return;
    	}		
    		
    }
    add_action ( 'pre_get_posts', 'review_epilator_to_tag_loop' );

    P.S. If anyone would also be willing / able to explain the above code to me in ways a n00bie can understand, I would very much love that!

    • This topic was modified 11 months, 1 week ago by  Lord_Devi.
    • This topic was modified 11 months, 1 week ago by  Lord_Devi.
    #73331

    Lord_Devi
    Participant
    Post count: 10

    Ok! I actually figured this one out just now!

    Pretty cool actually. I have to say I am rather excited about picking up on this Custom Post Type stuff, and I love how easy Genesis is making this whole learning process for me.

    So I went through the following two pages, rather forcing myself to not just read, but to try and make sense of the data there! Which isn’t exactly that easy for someone just picking up PHP.

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    and

    http://codex.wordpress.org/Class_Reference/WP_Query

    Just noticing a pattern with how if ( is_tag() && $query->is_main_query() was structured, and comparing that with the examples found under the pre_get_posts link I posted above, using the Proprties found on the WP_Query page above, I tried to just hack the properties in and lo and behold my custom post type was appearing on both my Category Archive Loop, and my Home Loop.

    I ended up using the two following pieces of code initially to get my custom post types posting to my other archive loops:

    For adding CTP ‘review-epilator’ to Category Loop:

    function review_epilator_to_category_loop( $query ) {
    	// we don't want this running on the admin side
    	if ( is_admin() )
    		return;
    	// include our stream type on tag pages
    	if ( is_category() && $query->is_main_query() ) {
    		$query->query_vars['post_type'] = array( 'post', 'review-epilator' );
    		return;
    	}
    }
    add_action ( 'pre_get_posts', 'review_epilator_to_category_loop' );

    For adding CTP ‘review-epilator’ to Home Loop:

    function review_epilator_to_home_loop( $query ) {
    	// we don't want this running on the admin side
    	if ( is_admin() )
    		return;
    	// include our stream type on tag pages
    	if ( is_home() && $query->is_main_query() ) {
    		$query->query_vars['post_type'] = array( 'post', 'review-epilator' );
    		return;
    	}
    }
    add_action ( 'pre_get_posts', 'review_epilator_to_home_loop' );

    So the three I’ve pasted so far DID indeed add the functionality I was looking for. However I try to minimize code where I can so I tried to group the 3 separate functions I pasted above into the following single function:

    function review_epilator_to_archive_loops( $query ) {
    	// we don't want this running on the admin side
    	if ( is_admin() )
    		return;
    	// include our stream type on tag pages
    	if ( is_tag() && $query->is_main_query() ) {
    		$query->query_vars['post_type'] = array( 'post', 'review-epilator' );
    		return;
    	}
        // include our stream type on category pages
        if ( is_category() && $query->is_main_query() ) {
    		$query->query_vars['post_type'] = array( 'post', 'review-epilator' );
    		return;
    	}
        // include our stream type on home page
    	if ( is_home() && $query->is_main_query() ) {
    		$query->query_vars['post_type'] = array( 'post', 'review-epilator' );
    		return;
    	}
    }
    add_action ( 'pre_get_posts', 'review_epilator_to_archive_loops' );

    And it also works great! So I got everything I needed, and a nice small little function that will make it very easy for me to add new CTPs to my archive pages with later! SO EXCITING!

    So my only remaining question before I mark this thread as solved (I’ll wait a while before I close it), is does anyone have any input about my compiled function above?

    Are there any reasons I should not group these three functions into just the one above?

    Is there any better or more efficient way of writing this function?

    #78072

    haciendoeco
    Member
    Post count: 1

    Great! Thank you for your investigation…

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.