Solved

Wordpress Custom Post Type and Custom Taxonomy Invalid

Posted on 2014-04-04
2
315 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I edit this Drupal page? 9 30
.htaccess file settings 4 35
php image upload 3 27
Setting Up Local Lan Web Server 1 15
This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 look for a specific file type in a local or remote server directory using PHP.

706 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

19 Experts available now in Live!

Get 1:1 Help Now