• I am trying to create a custom post type of ‘representative’ and I will assign them to states. When I post a new representative, I only want a list of states to show in the drop down. No all post categories.

    //Representatives post type
    function my_custom_post_reps() {
      $labels = array(
        'name'               => _x( 'Representative', 'post type general name' ),
        'singular_name'      => _x( 'Representative', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'Representative' ),
        'add_new_item'       => __( 'Add New Representative' ),
        'edit_item'          => __( 'Edit Representative' ),
        'new_item'           => __( 'New Representative' ),
        'all_items'          => __( 'All Representatives' ),
        'view_item'          => __( 'View Representatives' ),
        'search_items'       => __( 'Search Representatives' ),
        'not_found'          => __( 'No representative found' ),
        'not_found_in_trash' => __( 'No representative found in the Trash' ), 
        'parent_item_colon'  => ’,
        'menu_name'          => 'Representative'
      );
    	
      $args = array(
        'labels'        => $labels,
        'description'   => 'representative specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments','custom-fields' ),
        'has_archive'   => true,
    	'show_in_rest' => true,
    	'show_ui'=>true,
        'show_in_menu'=>true,
        'taxonomies'          => array('states')
    	  
      );
    	
    register_post_type( 'representative', $args ); 
     
    register_taxonomy_for_object_type('category', 'representative');
    register_taxonomy_for_object_type('post_tag', 'representative');
    }
    add_action( 'init', 'my_custom_post_reps' , 0 );
    • This topic was modified 6 years, 3 months ago by Bloke.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Are you saying the “states” taxonomy may have terms that are not states? They could be excluded when dropdowns query for taxonomy terms. Use the “pre_get_terms” filter and set the “exclude” query var to be an array of non-state term IDs. Or set “include” to be an array of state term IDs.

    This will affect all term queries unless you apply conditions to manage when the alterations are used.

    Thread Starter Bloke

    (@bloke)

    No, what I mean is in the admin, when I add a new ‘representative’ I want to choose from the ‘states’ taxonomy. Not all taxonomies. Maye I need to declare it first.

    My code created the new post type of representative.

    • This reply was modified 6 years, 3 months ago by Bloke.
    Moderator bcworkz

    (@bcworkz)

    If your trouble is the taxonomy terms aren’t showing up on the representative edit screen and you are using the block editor, there is some sort of bug with custom taxonomies that does not exist with the classic editor. I don’t know what the proper solution is. The workaround is to use the classic editor plugin. It allows you to selectively use either editor. You only need classic to assign terms, you can still use the block editor for other things.

    Thread Starter Bloke

    (@bloke)

    The editor seems to be ok. Does my code look ok? Something is missing because in the editor it should only show the taxonomy for the post type. It the drop down box says “categories” and it is just listing all categories plus my states (taxonomy).

    Thread Starter Bloke

    (@bloke)

    Made some corrections and made it more simple and its working. Just registered the post type and the taxonomy with out all the arguments and labels and put it in one function. Also removed register_taxonomy_for_object_type().

    function my_custom_post_reps() {
    	register_post_type('representative', 
            array(  'taxonomies'            => array('rep_locations'),
                    'labels'                => array(
                    'name'                  => __('Representatives'),
                    'singular_name'         => __('Representative'),
                    'add_new'               => __('Add a new Representative'),
                    'edit_item'             => __('Edit Representative'),
                    'new_item'              => __('New Representative'),
                    'view_item'             => __('View Representative'),
                    'search_items'          => __('Search in Representatives'),
                    'not_found'             => __('No Representative found'),
                    'not_found_in_trash'    => __('No Representative found in trash')
                    ),
    
                    'has_archive'           => true,
                    'show_in_rest'          => true,
                    'hierarchical'          => true,
                    'public'                => true,
                    'menu_icon'             => 'dashicons-location',
                    'capability_type'       => 'post'
        ));
    	
    	
    	 register_taxonomy('rep_locations', 
            array('representative'), 
            array(
            		'labels'            => array(
                	'name'              => __('location list'),
                	'singular_name'     => __('location list'),
                	'search_items'      => __('Search locations'),
                	'all_items'         => __('All locations'),
                	'parent_item'       => __('Parent location'),
                	'parent_item_colon' => __('Parent location:'),
                	'edit_item'         => __('Edit location'), 
                	'update_item'       => __('Update location'),
                	'add_new_item'      => __('Add new location'),
                	'new_item_name'     => __('New location name'),
                	'menu_name'         => __('locations')
            ),
    
            		'show_ui'           => true,
    				'show_in_rest'          => true,	
            		'query_var'         => true,
            		'hierarchical'      => true,
            		'show_admin_column' => true,
            		'rewrite'           => array('slug' => 'rep_locations')
        ));
    
    }
    add_action( 'init', 'my_custom_post_reps' , 0 );
    
    Moderator bcworkz

    (@bcworkz)

    Cool! I guess your previous version had some conflict that wasn’t readily apparent on visual inspection. When in doubt go with defaults πŸ™‚

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom post type and taxonomies’ is closed to new replies.