1

I have custom post type named: ticket (http://localhost/wp/wp-admin/edit.php?post_type=ticket) And custom taxonomy named product (http://localhost/wp/wp-admin/edit-tags.php?taxonomy=product&post_type=ticket)

here is the code for register custom post type:

function wpas_register_post_type() {

    $slug = defined( 'WPAS_SLUG' ) ? sanitize_title( WPAS_SLUG ) : 'ticket';

    /* Supported components */
    $supports = array( 'title' );

    /* Template components for Gutenberg */
    $gutenburg_new_template = array(
                    array( 'core/paragraph', array(
                            'placeholder' => _x('Enter the contents for your new ticket here', 'placeholder for main paragraph when adding a new ticket', 'awesome-support' )
                        ) ),
                );

    /* If the post is being created we add the editor */
    if( !isset( $_GET['post'] ) ) {
        array_push( $supports, 'editor' );
    }

    /* Post type menu icon */
    $icon = version_compare( get_bloginfo( 'version' ), '3.8', '>=') ? 'dashicons-forms' : WPAS_ADMIN_ASSETS_URL . 'images/icon-tickets.png';

    /* Post type labels */
    $labels = apply_filters( 'wpas_ticket_type_labels', array(
            'name'               => _x( 'Tickets', 'post type general name', 'awesome-support' ),
            'singular_name'      => _x( 'Ticket', 'post type singular name', 'awesome-support' ),
            'menu_name'          => _x( 'Tickets', 'admin menu', 'awesome-support' ),
            'name_admin_bar'     => _x( 'Ticket', 'add new on admin bar', 'awesome-support' ),
            'add_new'            => _x( 'Add New', 'ticket', 'awesome-support' ),
            'add_new_item'       => __( 'Add New Ticket', 'awesome-support' ),
            'new_item'           => __( 'New Ticket', 'awesome-support' ),
            'edit_item'          => __( 'Edit Ticket', 'awesome-support' ),
            'view_item'          => __( 'View Ticket', 'awesome-support' ),
            'all_items'          => __( 'All Tickets', 'awesome-support' ),
            'search_items'       => __( 'Search Tickets', 'awesome-support' ),
            'parent_item_colon'  => __( 'Parent Ticket:', 'awesome-support' ),
            'not_found'          => __( 'No tickets found.', 'awesome-support' ),
            'not_found_in_trash' => __( 'No tickets found in Trash.', 'awesome-support' ),
    ) );

    /* Post type capabilities */
    $cap = apply_filters( 'wpas_ticket_type_cap', array(
            'read'                   => 'view_ticket',
            'read_post'              => 'view_ticket',
            'read_private_posts'     => 'view_private_ticket',
            'edit_post'              => 'edit_ticket',
            'edit_posts'             => 'edit_ticket',
            'edit_others_posts'      => 'edit_other_ticket',
            'edit_private_posts'     => 'edit_private_ticket',
            'edit_published_posts'   => 'edit_ticket',
            'publish_posts'          => 'create_ticket',
            'delete_post'            => 'delete_ticket',
            'delete_posts'           => 'delete_ticket',
            'delete_private_posts'   => 'delete_private_ticket',
            'delete_published_posts' => 'delete_ticket',
            'delete_others_posts'    => 'delete_other_ticket'
    ) );

    /* Post type arguments */
    $args = apply_filters( 'wpas_ticket_type_args', array(
            'labels'              => $labels,
            'public'              => true,
            'exclude_from_search' => true,
            'publicly_queryable'  => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'query_var'           => true,
            'rewrite'             => array( 'slug' => apply_filters( 'wpas_rewrite_slug', $slug ), 'with_front' => false ),
            'capability_type'     => 'view_ticket',
            'capabilities'        => $cap,
            'has_archive'         => true,
            'hierarchical'        => false,
            'menu_position'       => null,
            'menu_icon'           => $icon,
            'supports'            => $supports,
            'template'            => $gutenburg_new_template
    ) );

    register_post_type( 'ticket', $args );

}

All works fine but I want to attach custom taxonomy(product in my case) in this custom post(ticket). I tried providing 'taxonomies' => array('product'), inside $arg array(). But no luck. Any idea?

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.