Solved

Wordpress Custom Post Type and Custom Taxonomy Invalid

Posted on 2014-04-04
2
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

733 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