Solved

Wordpress Custom Post Type and Custom Taxonomy Invalid

Posted on 2014-04-04
2
334 Views
Last Modified: 2016-05-21
I have a custom post type called EWD Item (ewd-item slug). Tied to this custom post type, is a custom taxonomy called EWD Sector (ewdsector slug). I have a development server and live server which are both identical except that the live server is a WP Multi-site installation. When I run get_terms('ewdsector') on the dev site, it returns all the terms but when I run it on the live site, it gives me an "Invalid Taxonomy" error.

Here's the custom post type registration code:
function register_ewd_item_posttype() {
		$labels = array(
			'name' 				=> _x( 'EWD Items', 'post type general name' ),
			'singular_name'		=> _x( 'EWD Item', 'post type singular name' ),
			'add_new' 			=> __( 'Add New' ),
			'add_new_item' 		=> __( 'EWD Item' ),
			'edit_item' 		=> __( 'EWD Item' ),
			'new_item' 			=> __( 'EWD Item' ),
			'view_item' 		=> __( 'EWD Item' ),
			'search_items' 		=> __( 'EWD Item' ),
			'not_found' 		=> __( 'EWD Item' ),
			'not_found_in_trash'=> __( 'EWD Item' ),
			'parent_item_colon' => __( 'EWD Item' ),
			'menu_name'			=> __( 'EWD Items' )
		);
		
		$taxonomies = array();

		$supports = array('title','editor','author','thumbnail','revisions');
		
		$post_type_args = array(
			'labels' 			=> $labels,
			'singular_label' 	=> __('EWD Item'),
			'public' 			=> true,
			'show_ui' 			=> true,
			'publicly_queryable'=> true,
			'query_var'			=> true,
			'exclude_from_search'=> true,
			'show_in_nav_menus'	=> false,
			'capability_type' 	=> 'post',
			'has_archive' 		=> true,
			'hierarchical' 		=> false,
			'rewrite' 			=> array('slug' => 'ewd-item', 'with_front' => false ),
			'supports' 			=> $supports,
			'menu_position' 	=> 5,
			'taxonomies'		=> $taxonomies
		 );
		 register_post_type('ewd-item',$post_type_args);
	}
	add_action('init', 'register_ewd_item_posttype');

Open in new window


Here's the custom taxonomy registration code:
function register_ewdsector_tax() {
			$labels = array(
				'name' 					=> _x( 'EWD Sectors', 'taxonomy general name' ),
				'singular_name' 		=> _x( 'EWD Sector', 'taxonomy singular name' ),
				'add_new' 				=> _x( 'Add New EWD Sector', 'EWD Sector'),
				'add_new_item' 			=> __( 'Add New EWD Sector' ),
				'edit_item' 			=> __( 'Edit EWD Sector' ),
				'new_item' 				=> __( 'New EWD Sector' ),
				'view_item' 			=> __( 'View EWD Sector' ),
				'search_items' 			=> __( 'Search EWD Sectors' ),
				'not_found' 			=> __( 'No EWD Sector found' ),
				'not_found_in_trash' 	=> __( 'No EWD Sector found in Trash' ),
			);
			
			$pages = array('ewd-item');
			$args = array(
				'labels' 			=> $labels,
				'singular_label' 	=> __('EWD Sector'),
				'public' 			=> true,
				'show_ui' 			=> true,
				'hierarchical' 		=> false,
				'show_tagcloud' 	=> false,
				'show_in_nav_menus' => false,
				'rewrite' 			=> array('slug' => 'ewdsector', 'with_front' => false ),
			 );
			register_taxonomy('ewdsector', $pages, $args);
		}
		add_action('init', 'register_ewdsector_tax');

Open in new window


When I do
$terms = get_terms('ewdsector');
			print_r($terms);

Open in new window


It returns WP_Error Object Invalid Taxonomy.

Also, there's something a bit odd going on in the database.

On my dev server, the "term_taxonomy_id" and "term_id" are always different values while on the live server, they're all equal.

I'm doing all I can to replicate "Invalid Taxonomy" error on my dev site but can't seem to figure out the issue.

How can I get the custom taxonomy working on the live site?

Thanks in advance!
0
Comment
Question by:AccurateDesign
2 Comments
 
LVL 15

Accepted Solution

by:
eemit earned 500 total points
ID: 39980468
No doubt you are doing it too early. Hook your code in later e.g. in a functions.php:

add_action( 'init', 'twentyfourteenchild_ee_test', 11 );
function twentyfourteenchild_ee_test() {
	 $terms = get_terms( array( 'ewdsector' ) );
	 var_dump($terms);
}

Open in new window


Init action hook is only illustration, you can test it using a shortcode e.g. in a functions.php:
function yourchildthemeslug_shortcode_my_term_list() {
	$terms = get_terms( array( 'ewdsector' ) );
	$output = '';
	if ( !empty( $terms ) && !is_wp_error( $terms ) ){
		$output .= '<ul>';
		foreach ( $terms as $term ) {
			$output .= '<li>' . $term->name . '</li>';
		}
		$output .= '</ul>';
	 }
	return $output;
}
add_shortcode( 'shortcode_my_term_list', 'yourchildthemeslug_shortcode_my_term_list' );

Open in new window

And in your Template:
<?php echo do_shortcode('[shortcode_my_term_list]'); ?>

Open in new window



You can use the function the_terms() or the function get_the_term_list() in a template inside the loop.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to count occurrences of each item in an array.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now