Custom Post Types and Category Archives

Community Forums Forums Design Tips and Tricks Custom Post Types and Category Archives

This topic is: resolved

This topic contains 7 replies, has 4 voices, and was last updated by  cbryant 1 month ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #40708

    SaraGraybill
    Participant
    Post count: 18

    I have created a custom post type for my website listing the services available.  I am using categories for the custom post type and I can’t seem to figure out how to show the service cpt in the category archive.  It just says no posts match your criteria.  Any ideas?

    http://www.cobbengr.com/civilservants/
    #40797

    braddalton
    Participant
    Post count: 10261

    Just noticed your footer links not working.

    I think you may have setup your custom post types incorrectly but hard to say from here. I may be wrong.


    #46843

    01themas
    Participant
    Post count: 1

    Try something like this to be able to have categories  displayed across all CPTs:

    //enables categories to be used across different post types
    add_filter(‘pre_get_posts’, ‘query_post_type’);
    function query_post_type($query) {
    if(is_category() || is_tag() || is_home() && empty( $query->query_vars['suppress_filters'] ) ) {
    $post_type = get_query_var(‘post_type’);
    if($post_type)
    $post_type = $post_type;
    else
    $post_type = array(‘post’,’business’, ‘academic’, ‘partners’, ‘people’, ‘facilities’,’programs’ ,’nav_menu_item’);
    $query->set(‘post_type’,$post_type);
    return $query;
    }
    }

    #125458

    cbryant
    Participant
    Post count: 12

    I’m running into the same problem described by SaraGraybill.

    I’ve set up a CPT called “sermon”.
    Set it up manually in my functions.php. (code at bottom).
    The CPT itself correctly uses the Genesis 2.0 Archive template support for CPTs.
    That is: I can view a complete list of all CPTs on an Archive page, and add info to it using Genesis Archive settings. All good.

    However, this CPT has “categories” enabled.
    Yet, when I click on the “Filed under ‘Category Name‘ on a single CPT it brings me to the standard Genesis Category Page (good) but gives me the error “Sorry, no content matched your criteria.”

    The category heading and title which I added in the backend do appear correctly on the category page.
    So… it’s as if it’s seeing the Category correctly – but not seeing the CPTs within the category .

    @ 01themas idea seemed to make sense. But I had no success with it.

    Is there a simple Genesis function for adding Genesis Category Archive Support to CPT Categories (and not just to the Post Type)?

    Any suggestions are welcomed.

    I’m working locally so have no link to provide.

    Here is my CPT registration code… in case maybe anyone spots a glaring omission on my part….

    Thanks in advance for any thoughts or comments.

    Chris

    add_action('init', 'register_cpt_sermon');
    	function register_cpt_sermon() {
    		register_post_type('sermon', array(
    			'label' => 'Sermons',
    			'description' => 'Audio Recordings of Sermons',
    			'public' => true,
    			'show_ui' => true,
    			'show_in_menu' => true,
    			'capability_type' => 'post',
    			'map_meta_cap' => true,
    			'hierarchical' => true,
    			'rewrite' => array('slug' => 'sermon', 'with_front' => 1),
    			'query_var' => true,
    			'has_archive' => true,
    			'menu_position' => '5',
    			'supports' => array('title','editor','excerpt','custom-fields','revisions','thumbnail','author','page-attributes','genesis-cpt-archives-settings'),
    			'taxonomies' => array('category'),
    			'labels' => array (
    				  'name' => 'Sermons',
    				  'singular_name' => 'Sermon',
    				  'menu_name' => 'Sermons',
    				  'add_new' => 'Add Sermon',
     /*I've included a full series of labels - but left them out for readability here*/
    			)
    		) ); 
    	}

    #125460

    braddalton
    Participant
    Post count: 10261

    Where did you get the code for the CPT?


    #125461

    braddalton
    Participant
    Post count: 10261
    #125478

    cbryant
    Participant
    Post count: 12

    Hi Brad,

    Thank you for responding.

    The code was generated by Custom Post Types UI plugin.

    I’m going to take a look at your recommendations and links and will post the results.

    Thanks again – Chris


    #125481

    cbryant
    Participant
    Post count: 12

    This code snippet did the trick:

    //* Show Sermon CPT entries in addition to Posts in Category Archive Pages 
    add_action( 'pre_get_posts', 'cpt_entries_and_posts_in_archives' );
    function cpt_entries_and_posts_in_archives( $query ) {
     
    	if ( $query->is_main_query() && !is_admin() && is_category() ) {
    	$query->set( 'post_type', array( 'post', 'sermon' ) );
    	}
    }

    (props Bill Erickson)

    @Brad – thanks for your input. Your wpsites is a valuable resource!


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

You must be logged in to reply to this topic.