Display posts from Custom Post Type in category page on front-end

I created my Custom Post Type (bizdirectory), created a new post (Test Business) and created a new category (Business Directory). But if I add the Business Directory category (slug = business) to my Primary Navigation, the link shows, it registers as the category, but it says “nothing found”.

If I search my site (using the search bar on the front page) for Test Business it finds it – the URL it shows is – http://www.domain.com/dev/?bizdirectory=test-business – my code for functions.php is below:

Read More
// Load Custom Post Type 
function add_bizdirectory() {
    $labels = array( 
        'name' => __( 'Businesses', 'text_domain' ),
        'singular_name' => __( 'Business', 'text_domain' ),
        'add_new' => __( 'Add New Business', '${4:Name}', 'text_domain' ),
        'add_new_item' => __( 'Add New Business', 'text_domain}' ),
        'edit_item' => __( 'Edit Business', 'text_domain' ),
        'new_item' => __( 'New Business', 'text_domain' ),
        'view_item' => __( 'View Business', 'text_domain' ),
        'search_items' => __( 'Search Businesses', 'text_domain' ),
        'not_found' => __( 'No Businesses found', 'text_domain' ),
        'not_found_in_trash' => __( 'No Businesses found in Trash', 'text_domain' ),
        'parent_item_colon' => __( 'Parent Business:', 'text_domain' ),
        'menu_name' => __( 'Business Directory', 'text_domain' ),
    );
    $args = array( 
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'description',
        'taxonomies' => array( 'category' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post', 
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'revisions', 'post-formats' ),
    );
    register_post_type( 'bizdirectory', $args );
}
add_action( 'init', 'add_bizdirectory' );
// End

I have tried all of the rewrite rules options apart from the one where I manually delete the rewrite rules from wp_options – it didn’t appear in wp_options.

I appreciate any help or advice that you could give me 🙂

Related posts

2 comments

  1. You are better off separating categories for posts and using custom taxonomy types for CPT categories.

    You’d add this line to your register cpt code

    'taxonomies'   => array( 'bizdirectory-type' ),
    

    Replacing this line in your code.

    'taxonomies' => array( 'category' ),
    

    And then add this code in your functions.php

    add_action( 'init', 'wpsites_custom_taxonomy_types' );
    function wpsites_custom_taxonomy_types() {
    
    register_taxonomy( 'bizdirectory-type', 'bizdirectory',
        array(
            'labels' => array(
                'name'          => _x( 'Types', 'taxonomy general name', 'wpsites' ),
                'add_new_item'  => __( 'Add New Bizdirectory Type', 'wpsites' ),
                'new_item_name' => __( 'New Bizdirectory Type', 'wpsites' ),
            ),
            'exclude_from_search' => true,
            'has_archive'         => true,
            'hierarchical'        => true,
            'rewrite'             => array( 'slug' => 'bizdirectory-type', 'with_front' => false ),
            'show_ui'             => true,
            'show_tagcloud'       => false,
        ));
    
    }
    
  2. I have found the answer, I had completely misunderstood the differences between standard post types and custom post types, as well as their taxonomies. I found several very helpful articles and the code below is what I ended up with, as well as a few of the articles that I found helpful. I hope others find this useful.

    // Load Custom Post Type 
    function add_directory() {
        $labels = array( 
        'name'               => __( 'Businesses', 'text_domain' ),
            'singular_name'      => __( 'Business', 'text_domain' ),
            'add_new'            => __( 'Add New Business', '${4:Name}', 'text_domain' ),
            'add_new_item'       => __( 'Add New Business', 'text_domain}' ),
            'edit_item'          => __( 'Edit Business', 'text_domain' ),
            'new_item'           => __( 'New Business', 'text_domain' ),
            'view_item'          => __( 'View Business', 'text_domain' ),
            'search_items'       => __( 'Search Businesses', 'text_domain' ),
            'not_found'          => __( 'No Businesses found', 'text_domain' ),
            'not_found_in_trash' => __( 'No Businesses found in Trash', 'text_domain' ),
            'parent_item_colon'  => __( 'Parent Business:', 'text_domain' ),
            'menu_name'          => __( 'Business Directory', 'text_domain' ),
        );
        $args = array( 
            'labels'              => $labels,
            'hierarchical'        => true,
            'description'         => 'description',
            'taxonomies'          => array( 'biz-cat' ),
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'menu_position'       => 5,
            //'menu_icon'         => '',
            'show_in_nav_menus'   => true,
            'publicly_queryable'  => true,
            'exclude_from_search' => false,
            'has_archive'         => true,
            'query_var'           => true,
            'can_export'          => true,
            'rewrite'             => true,
            'capability_type'     => 'post', 
            'supports'            => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'revisions', 'post-formats' ),
        );
        register_post_type( 'bizdirectory', $args );
    }
    add_action( 'init', 'add_directory' );
    
    // Create Custom Taxonomy
    function directory_create_taxonomies() 
    {
        register_taxonomy( 'biz-cat', array( 'bizdirectory' ), array(
            'hierarchical' => true,
            'label' => 'Business Categories',
            'singular_name' => 'Business Category',
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'biz-cat' )
        ));
    }
    add_action( 'init', 'directory_create_taxonomies', 0 );
    
    // 'bizdirectory' is the registered post type name
    function directory_columns($defaults) {
        // 'biz-cat' is the registered taxonomy name
        $defaults['biz-cat'] = 'Business Category';
        return $defaults;
    }
    function directory_custom_column($column_name, $post_id) {
        $taxonomy = $column_name;
        $post_type = get_post_type($post_id);
        $terms = get_the_terms($post_id, $taxonomy);
    
        if ( !empty($terms) ) {
            foreach ( $terms as $term )
                $post_terms[] = "<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " . esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
            echo join( ', ', $post_terms );
        }
        else echo '<i>Not assigned.</i>';
    }
    add_filter( 'manage_project_posts_columns', 'directory_columns' );
    add_action( 'manage_project_posts_custom_column', 'directory_custom_column', 10, 2 );
    

Comments are closed.