Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Wordpress Custom Post Type and Custom Taxonomy Invalid

Posted on 2014-04-04
2
Medium Priority
?
624 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

610 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