get_terms() returns Invalid Taxonomy - wordpress

I've looked at other examples and solutions, but there has to be something I am missing. I've tried everything the other posts answers have suggested, but to no avail.
I'm not sure what exactly the issue is, but I can use get_terms without any parameters, and I get every term on my site, but clearly I want to isolate this to only the taxonomy declared. I am running this outside a loop, so I cannot pass an ID, nor do I want to pass an ID.
Any insight is helpful!
I am getting this when using get_terms().
object(WP_Error)#992 (2) {
["errors"]=>
array(1) {
["invalid_taxonomy"]=>
array(1) {
[0]=>
string(17) "Invalid taxonomy."
}
}
["error_data"]=>
array(0) {
}
}
My Code:
Taxonomy
if ( ! function_exists( 'car_ctax_catalog' ) ) {
// Register Custom Taxonomy
function car_ctax_catalog() {
$labels = array(
'name' => _x( 'Categories', 'Taxonomy General Name', 'carmon' ),
'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'carmon' ),
'menu_name' => __( 'Category', 'carmon' ),
'all_items' => __( 'All Categories', 'carmon' ),
'parent_item' => __( 'Parent Category', 'carmon' ),
'parent_item_colon' => __( 'Parent Category:', 'carmon' ),
'new_item_name' => __( 'New Category Name', 'carmon' ),
'add_new_item' => __( 'Add New Category', 'carmon' ),
'edit_item' => __( 'Edit Category', 'carmon' ),
'update_item' => __( 'Update Category', 'carmon' ),
'view_item' => __( 'View Category', 'carmon' ),
'separate_items_with_commas' => __( 'Separate categories with commas', 'carmon' ),
'add_or_remove_items' => __( 'Add or remove categories', 'carmon' ),
'choose_from_most_used' => __( 'Choose from the most used', 'carmon' ),
'popular_items' => __( 'Popular Categories', 'carmon' ),
'search_items' => __( 'Search Categories', 'carmon' ),
'not_found' => __( 'Not Found', 'carmon' ),
'no_terms' => __( 'No categories', 'carmon' ),
'items_list' => __( 'Categories list', 'carmon' ),
'items_list_navigation' => __( 'Categories list navigation', 'carmon' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'rewrite' => false,
'show_in_rest' => true,
'rest_base' => 'ctax_catalog',
);
register_taxonomy( 'ctax_catalog', array( 'cpt_catalog' ), $args );
}
add_action( 'init', 'car_ctax_catalog', 0 );
}
Get Terms
$args = array (
'post_type' => 'catalog',
'taxonomy' => 'ctax_catalog',
'hide_emoty' => false
);
$tax = array ( 'ctax_catalog' );
$terms = get_terms($tax, $args);
echo"<pre>";var_dump($terms);echo"</pre>";
EDIT
I've also tried this with the same result.
Get Terms Try 2
$terms = get_terms('ctax_catalog');
$tterms = get_terms( array ('taxonomy' => 'ctax_catalog' ) );
echo"<pre>";var_dump($terms);echo"</pre>";
echo"<pre>";var_dump($tterms);echo"</pre>";

I came up with the following hackish work-around, until a better solution could be found:
$terms = get_terms();
$gradeOptions = "";
foreach ($terms as $term) {
if ($term->taxonomy === "pa_grade") {
$gradeOptions .= "<options value=\"{$term->term_id}\">{$term->name}</options>";
}
}
var_dump($gradeOptions);
die();

Related

Taxonomy returns invalid taxonomy error when using get_terms()

I have registered a custom taxonomy.Please find the code below
function register_taxonomy_state() {
$labels = array(
'name' => _x( 'States', 'taxonomy general name' ),
'singular_name' => _x( 'State', 'taxonomy singular name' ),
'search_items' => __( 'Search State' ),
'all_items' => __( 'All States' ),
'parent_item' => __( 'Parent States' ),
'parent_item_colon' => __( 'Parent States:' ),
'edit_item' => __( 'Edit State' ),
'update_item' => __( 'Update State' ),
'add_new_item' => __( 'Add New State' ),
'new_item_name' => __( 'New State Name' ),
'menu_name' => __( 'State' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'public' =>true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'alumns-reps-states' ],
);
register_taxonomy( 'alumns-reps-states', [ 'alumns-reps' ], $args );
}
add_action( 'init', 'register_taxonomy_state' );
I would like to display all terms added under this taxonomy, and for that i used following code
$taxonomies = get_terms( array(
'taxonomy' => 'alumns-reps-states',
'hide_empty' => false
) );
var_dump($taxonomies);
It is always returning 'Invalid Taxonomy' error. WHat is wrong with this code ?

Show only specific categories in permalinks for custom post type in WordPress

I tried to ask this question in the Wordpress Devlopment network with no success, I'm trying to display only 3 specific categories in permalinks for custom-post-type categories.
Right now the permalinks structure (that is the structure given by the theme i'm using and that i'm modifying through a child theme)is as follows:
www.myfoodblog.com/recipes/the-post-title/
^ ^ ^
root custom-post-type title
some post are under 3 categories that i would like to display in permalinks which are restaurant, users and admin to get something like this
www.myfoodblog.com/recipes/restaurant/the-post-title/
www.myfoodblog.com/recipes/users/the-post-title/
www.myfoodblog.com/recipes/admin/the-post-title/
leaving the original structure for posts that are not in those categories.
I tried using this plugin but it will display the custom post type category for all posts.
I also tried to follow the instructions provided in this question but it's not working, no changes are made in the permalinks structure.
Any advice is much appreciated.
You have to work with both post_type_link and add a rewrite rule
function recipes_post_link( $post_link, $id = 0 ){
$post = get_post( $id );
if ( is_object( $post ) ){
$terms = get_the_terms( $post->ID, 'recipe-category' );
foreach($terms as $term) {
if( $term->slug == 'restaurants' || $term->slug == 'users'){
return str_replace( site_url()."/recipes" , site_url()."/recipes/".$term->slug , $post_link );
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'recipes_post_link', 1, 3 );
function custom_rewrite_basic() {
add_rewrite_rule('^recipes/(.+)/(.+)', 'index.php?recipe=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Create Post Type
add_action( 'init', 'codex_recipes_init' );
/**
* Register a recipes post type.
*
* #link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function codex_recipes_init() {
$labels = array(
'name' => _x( 'Recipes', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Recipe', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Recipes', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Recipe', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'recipe', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Recipe', 'your-plugin-textdomain' ),
'new_item' => __( 'New Recipe', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Recipe', 'your-plugin-textdomain' ),
'view_item' => __( 'View Recipe', 'your-plugin-textdomain' ),
'all_items' => __( 'All Recipes', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Recipes', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Recipes:', 'your-plugin-textdomain' ),
'not_found' => __( 'No recipes found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No recipes found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'recipes/%type%' ),
'capability_type' => 'post',
'has_archive' => 'recipes',
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'recipes', $args );
}
Important: look on rewrite rule: 'rewrite'=> array( 'slug' => 'recipes/%type%' ),
Create Custom Taxonomy
// hook into the init action and call create_recipes_taxonomies when it fires
add_action( 'init', 'create_recipes_taxonomies', 0 );
function create_recipes_taxonomies() {
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Type', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Type', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Types', 'textdomain' ),
'popular_items' => __( 'Popular Types', 'textdomain' ),
'all_items' => __( 'All Types', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Type', 'textdomain' ),
'update_item' => __( 'Update Type', 'textdomain' ),
'add_new_item' => __( 'Add New Type', 'textdomain' ),
'new_item_name' => __( 'New Type Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate types with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove types', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used types', 'textdomain' ),
'not_found' => __( 'No types found.', 'textdomain' ),
'menu_name' => __( 'Types', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
);
register_taxonomy( 'type', 'recipes', $args );
}
Add Filter Post Type Link
function recipes_post_link( $post_link, $id = 0 ){
$post = get_post( $id );
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'type' );
if( $terms ){
return str_replace( '%type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'recipes_post_link', 1, 3 );
Important: After all go to permalink options and reset it.

WordPress Custom Post Type and Taxonomy URLs

I made a custom post type and taxonomy so that I could have a structure of /episodes for an archive page that would show all children "terms", /episodes/custom-term to show an archive of a specific term, and /episodes/custom-term/post-title to show single posts.
I was able to get /episodes/custom-term working for any custom term I create. However, I am receiving 404 for /episodes as well as 404 for /episodes/custom-term/post-title.
In the CMS, when I make a post, assuming my custom term is Season One and the post title is Sample Episode. You can go to /episodes/season-one and it will use the "taxonomy-episode_category.php" template to output all posts within Season One. It also knows when creating the post that the permalink is /episodes/season-one/sample-episode however it reaches a 404 page.
I am not sure where to go from here or if I am doing this wrong.
function episodes() {
$labels = array(
'name' => _x( 'Episodes', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Episodes', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Episodes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Episodes', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'add_new' => __( 'Add new episode', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' )
);
$rewrite = array(
'slug' => 'episodes/%episode_cat%',
'with_front' => false,
'pages' => true,
'feeds' => true
);
$args = array(
'label' => __( 'episodes', 'text_domain' ),
'description' => __( 'All episodes', 'text_domain' ),
'labels' => $labels,
'supports' => array('title' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
'query_var' => true,
'_builtin' => false
);
register_post_type( 'episodes_listing', $args );
}
add_action( 'init', 'episodes', 0 );
function episodes_taxomony() {
$labels = array(
'name' => _x( 'Episodes Categories', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Episodes', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Episode Categories', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Item Name', 'text_domain' ),
'add_new_item' => __( 'Add new episode', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' )
);
$rewrite = array(
'slug' => 'episodes',
'with_front' => false,
'hierarchical' => true
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
'rewrite' => $rewrite
);
register_taxonomy( 'episodes_category', array('episodes_listing'), $args );
}
add_action( 'init', 'episodes_taxomony', 0 );
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'episodes_listing')
return $link;
if ($cats = get_the_terms($post->ID, 'episodes_category')) {
$link = str_replace('%episode_cat%', array_pop($cats)->slug, $link);
return $link;
}
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
Turns out pagination was working correctly when I set permalink structure to default instead of Post name. To use post name I was able to use the exact code along with a new function:
function add_rewrite_rules() {
add_rewrite_rule('episodes/(.+?)/page/?([0-9]{1,})/?$', 'index.php', 'top');
}
add_filter('init', 'add_rewrite_rules')

Permalink of a custom post type

I am trying to get the link to a custom post type - Video. I have achieved this by doing:
get_post_type_archive_link( 'video' );
This returns http://mylink.co.uk/video which is brillant and exactly what I am looking for. However, when I click onto a custom post type category for example...LIVE and it loads the filtered results, the script link using get_post_type_archive_link( 'video' ); now changes to the LIVE category link showing http://mylink.co.uk/category/live/.
What is wrong?
UPDATE
function custom_post_type_video() {
$labels = array(
'name' => _x( 'Videos', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Video', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Video', 'text_domain' ),
'parent_item_colon' => __( 'Parent Video:', 'text_domain' ),
'all_items' => __( 'All Video', 'text_domain' ),
'view_item' => __( 'View Video', 'text_domain' ),
'add_new_item' => __( 'Add New Video', 'text_domain' ),
'add_new' => __( 'New Video', 'text_domain' ),
'edit_item' => __( 'Edit Video', 'text_domain' ),
'update_item' => __( 'Update Video', 'text_domain' ),
'search_items' => __( 'Search Videos', 'text_domain' ),
'not_found' => __( 'No Videos found', 'text_domain' ),
'not_found_in_trash' => __( 'No Videos found in Trash', 'text_domain' ),
);
$args = array(
'label' => __( 'Video', 'text_domain' ),
'description' => __( 'Video information pages', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => '',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'Video', $args );
add_action( 'init', 'my_taxonomies_product', 0 );
// Initialize Taxonomy Labels
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name', 'text_domain' ),
'singular_name' => _x( 'Category', 'taxonomy singular name', 'text_domain' ),
'search_items' => __( 'Search Types', 'text_domain' ),
'all_items' => __( 'All Categories', 'text_domain' ),
'parent_item' => __( 'Parent Category', 'text_domain' ),
'parent_item_colon' => __( 'Parent Category:', 'text_domain' ),
'edit_item' => __( 'Edit Categories', 'text_domain' ),
'update_item' => __( 'Update Category', 'text_domain' ),
'add_new_item' => __( 'Add New Category', 'text_domain' ),
'new_item_name' => __( 'New Category', 'text_domain' ),
);
// Register Custom Taxonomy
register_taxonomy('tagvideo',array('video'), array(
'hierarchical' => true, // define whether to use a system like tags or categories
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'cat-video' ),
));
}
// Hook into the 'init' action
add_action( 'init', 'custom_post_type_video', 0 );
I don't know why is it so. I read the code (both yours and WordPress' source code) and didn't find why is it so. I can tell you only two things that might or might not be helpful:-
First
This line:-
register_post_type( 'Video', $args );
should be like this:-
register_post_type( 'video', $args ); // small v
Second
One quick and dirty workaround that you can do is to 'hard-code' the video post type archive link. Try inserting this code in your functions.php:-
function video_post_type_archive_link( $link, $post_type ) {
if( $post_type === 'video' ) {
$link = 'http://mylink.co.uk/video' // <-- this should be link to 'video' post type archives
}
return $link;
}
add_filter('post_type_archive_link', 'video_post_type_archive_link', 10, 2);
Now, whenever you'll call this:
get_post_type_archive_link( 'video' );
You'll get:
http://mylink.co.uk/video
Only for video custom post type.

Wordpress custom taxonomy move meta_box

I have a custom meta box(taxonomy) that is positioned on side of my custom post. I want to move it to normal position, but when I remove it(remove_meta_box), and re-add(add_meta_box) I only get bar, with no options to select anything. I think I didn't write proper $callback, but I tried many variations, and don't have a clue.
function create_isotope_taxonomies()
{
$labels = array(
'name' => _x( 'Select Category', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => null,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit' ),
'update_item' => __( 'Update' ),
'add_new_item' => __( 'Add New' ),
'new_item_name' => __( 'New Category' ),
'separate_items_with_commas' => __( 'Separate writers with commas' ),
'add_or_remove_items' => __( 'Add or remove categories' ),
'choose_from_most_used' => __( 'Choose from the most used categories' ),
'menu_name' => __( 'Categories' ),
);
register_taxonomy('fzisotope_categories','fzisotope_post',array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'fzisotope_categories' ),
));
}
function fzisotope_categories_meta_box(){
remove_meta_box('fzisotope_categoriesdiv', 'fzisotope_post', 'side');
add_meta_box( 'fzisotope_categoriesdiv', 'Select Category', 'fzisotope_categories_meta_box', 'fzisotope_post', 'normal', 'high');
//print '<pre>';print_r( $wp_meta_boxes['post'] );print '<pre>';
}
add_action( 'admin_init', 'fzisotope_categories_meta_box', 0 );
Thanks for reading
You have to use:
post_categories_meta_box for taxonomies with hierarchies.
remove_meta_box('fzisotope_categoriesdiv', 'fzisotope_post', 'side');
add_meta_box( 'fzisotope_categoriesdiv', 'XXXXXXXXXXX', 'post_categories_meta_box', 'post', 'normal', 'high', array( 'taxonomy' => 'fzisotope_categories' ));

Resources