Invalid Taxonomy Wordpress - wordpress

I have created a custom taxonomy which is working fine. Below are the code:
add_action('init', 'canderel_game_taxonomy', 0);
function canderel_game_taxonomy() {
$labels = array(
'name' => 'Gamme',
'singular_name' => 'Gamme',
'search_items' => 'Recherche Gamme',
'all_items' => 'Toutes les Gammes',
'parent_item' => 'Gamme Parent',
'parent_item_colon' => 'Gamme Parent',
'edit_item' => 'Editer Gamme',
'update_item' => 'Enregistrer Gamme',
'add_new_item' => 'Nouvelle Gamme',
'menu_name' => 'Gammes'
);
register_taxonomy('gammes', array('produit'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'public' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'gamme' ),
));
}
Here is my code to fetch the taxonomy term but it is returning invalid taxonomy everytime.
$terms = get_terms([
'taxonomy' => 'gammes',
'hide_empty' => false,
]);

Related

Remove custom post type slug from wordpress url and keep taxonomy term

I have registered CPT & taxonomy with the following code :
// CPT & Taxonomy
add_action('init', 'marques_cpt');
function marques_cpt() {
$labels = array(
'name' => 'Marques',
'singular_name' => 'Marque',
'add_new' => 'Ajouter',
'add_new_item' => 'Ajouter Marque',
'edit_item' => 'Modifier Marque',
'new_item' => 'Nouvelle Marque',
'all_items' => 'Toutes les Marques',
'view_item' => 'Afficher Marque',
'search_items' => 'Rechercher Marques',
'not_found' => 'Pas de marques',
'not_found_in_trash' => 'Pas de marques',
'parent_item_colon' => '',
'menu_name' => 'Marques'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'taxonomies' => array('marquecategory'),
'rewrite' => array('slug' => 'marque/%marquescategory%', 'with_front' => false),
'capability_type' => 'post',
'has_archive' => 'marques',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt'),
);
register_post_type('marque', $args);
$labels = array(
'name' => 'Marques Catégories',
'singular_name' => 'Marques',
'search_items' => 'Rechercher Marques Catégories',
'all_items' => 'Toutes Catégories',
'parent_item' => 'Parent Catégories',
'parent_item_colon' => 'Parent Catégories Marques:',
'edit_item' => 'Modifier Catégories Marques',
'update_item' => 'Mettre à jour Catégories Marques',
'add_new_item' => 'Ajouter Catégories Marques',
'new_item_name' => 'Nouvelle Catégories Marques',
);
$args = array(
'hierarchical' => true,
'rewrite' => array('slug' => 'marque'),
'show_in_nav_menus' => true,
'labels' => $labels
);
register_taxonomy('marquescategory', 'marque', $args);
unset($labels);
unset($args);
}
add_filter('post_type_link', 'marquecategory_permalink_structure', 10, 4);
function marquecategory_permalink_structure($post_link, $post, $leavename, $sample) {
if (false !== strpos($post_link, '%marquescategory%')) {
$projectscategory_type_term = get_the_terms($post->ID, 'marquescategory');
if (!empty($projectscategory_type_term))
$post_link = str_replace('%marquescategory%', array_pop($projectscategory_type_term)->
slug, $post_link);
else
$post_link = str_replace('%marquescategory%', 'non-selectione', $post_link);
}
return $post_link;
}
this code return the url : domain.come/marque/taxonomy-term/post-slug
I need to remove the CPT slug 'marque' from the url & keep the taxonomy term : domain.come/taxonomy-term/post-slug
i have managed to do that by removing 'marque/' from rewrite :
'rewrite' => array('slug' => 'marque/%marquescategory%', 'with_front' => false),
it works perfectly but when i did that, the other simple posts and pages returns 404 page!!

I created cpt in WP and did not show the taxonomy, what did I do wrong?

I created the Wordpress plugin below to show a CPT called articles within the admin panel.
Within this CPT I created a taxonomy item to register the categories.
But I'm having trouble creating the part that shows the CPT taxonomy.
Apparently the code part is correct but I do not find the problem.
I need help finding out what I wrote wrong in the code and why it does not show the taxonomy item in the CPT menu I created.
<?php
/*
Plugin Name: Artigos
Version: 1.0
*/
/*POST TYPE*/
add_action('init', 'cpt_artigos');
function cpt_artigos() {
$labels = array(
'menu_name' => __('Artigos'),
'name' => __('Artigos'),
'singular_name' => __('Artigo'),
'all_items' => __('Todos os cadastros'),
'add_new' => __('Adicionar novo cadastro', 'Novo cadastro'),
'add_new_item' => __('Novo cadastro'),
'edit' => __('Editar'),
'edit_item' => __('Editar cadastro'),
'new_item' => __('Novo cadastro'),
'view' => __('Ver'),
'view_item' => __('Ver cadastro'),
'search_items' => __('Procurar cadastros'),
'not_found' => __('Nenhum registro encontrado'),
'not_found_in_trash' => __('Nenhum registro encontrado na lixeira'),
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-star-filled',
'capability_type' => 'post',
'menu_position' => 5,
'supports' => array('title','editor','thumbnail'),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'query_var' => true,
'can_export' => true,
'has_archive' => true,
'hierarchical' => true,
'rewrite' => true
);
register_post_type( 'artigos' , $args );
flush_rewrite_rules();
}
//TAXONOMY
add_action( 'init', 'taxonomyartigos');
function taxonomyartigos() {
$labels = array(
'menu_name' => __( 'Categorias de Artigos' ),
'name' => __( 'Categorias de Artigos' ),
'singular_name' => __( 'Categoria de Artigo' ),
'search_items' => __( 'Procurar Cadastros' ),
'all_items' => __( 'Todos os Cadastros' ),
'parent_item' => __( 'Categoria de Artigo Principal' ),
'parent_item_colon' => __( 'Categoria de Artigo Principal:' ),
'edit_item' => __( 'Editar Cadastro' ),
'update_item' => __( 'Atualizar Cadastro' ),
'add_new_item' => __( 'Adicionar novo cadastro' ),
'new_item_name' => __( 'Novo Cadastro' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
'rewrite' => true,
);
register_taxonomy( 'taxonomyartigos', array( 'cpt_artigos' ), $args );
flush_rewrite_rules();
}
function cpt_artigos_flush_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init', 'cpt_artigos_flush_rewrite');
?>
You need to write the post type name instead of the function
register_taxonomy( 'taxonomyartigos', 'artigos', $args );
Hope it will do.
Follow the documentation is a good idea in any case.
Below function takes 3 arguments in order.
taxonomy string which will be registered
post_type of CPT taxonomy will be attached with ( array or multiple CPT type or single CPT string)
Taxomony args as an array.
register_taxonomy( string $taxonomy, array|string $object_type, array|string $args = array() )
Ref: Here

Error 404 occurs when switching to the category page for the created taxonomy

I created taxonomy and type post.
// My post type
add_action('init', 'register_post_types_carshowroom');
function register_post_types_carshowroom() {
register_post_type('carshowroom', array(
'labels' => array(
'name' => 'Автосалоны',
'singular_name' => 'Автосалон',
'add_new' => 'Добавить автосалон',
'add_new_item' => 'Добавление автосалона',
'edit_item' => 'Редактирование автосалон',
'new_item' => 'Новый автосалон',
'view_item' => 'Смотреть автосалон',
'search_items' => 'Искать автосалон',
'not_found' => 'Не найдено',
'not_found_in_trash' => 'Не найдено в корзине',
'parent_item_colon' => '',
'menu_name' => 'Автосалоны',
),
'public' => true,
'show_ui' => true,
'has_archive' => true,
'menu_icon' => true,
'supports' => array('title', 'editor', 'thumbnail', 'comments'),
'taxonomies' => array('carshowroom_cat'),
));
}
// My taxonomy
add_action('init', 'add_taxonomy_carshowroom', 0);
function add_taxonomy_carshowroom() {
register_taxonomy(
'carshowroom_cat',
'carshowroom',
array(
'labels' => array(
'name' => 'Категории',
'singular_name' => 'Категория',
'search_items' => 'Найти категорию',
'popular_items' => 'Популярные категории',
'all_items' => 'Все категории',
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Редактировать категорию',
'update_item' => 'Обновить категорию',
'add_new_item' => 'Добавить новую категорию',
'new_item_name' => 'Название новой категории',
'separate_items_with_commas' => 'Разделяйте категории запятыми',
'add_or_remove_items' => 'Добавить или удалить категорию',
'choose_from_most_used' => 'Выбрать из наиболее часто используемых категорий',
'menu_name' => 'Категории'
),
'hierarchical' => true,
'public' => true,
'publicly_queryable' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array(
'slug' => 'carshowroom_cat',
'hierarchical' => true
),
)
);
}
Next, I re-saved the settings for permanent links:
/%category%/%postname%/
I created post for my post stype and category fo my taxonomy. I assigned this category to this post. I created template "taxonomy-carshowroom_cat.php".
Error 404 occurs when switching to the category page for the created taxonomy:
https://avtospletnik.com/carshowroom_cat/salon-moscow/
Could you explain to me what I'm doing wrong?

Metabox Not Showing on Custom Post Type But On Pages and Posts

I have a Custom Post Type like
function cpt_Projects() {
$labels = array(
'name' => 'Projects',
'singular_name' => 'Project',
'menu_name' => 'Projects',
'name_admin_bar' => 'Projects',
'parent_item_colon' => 'Parent Projects:',
'all_items' => 'All Projects',
'view_item' => 'View Project',
'add_new_item' => 'Add New Project',
'add_new' => 'Add New Project',
'new_item' => 'New Projects',
'edit_item' => 'Edit Project Item',
'update_item' => 'Update Project Item',
'search_items' => 'Search Project Item',
'not_found' => 'Project Not found',
'not_found_in_trash' => 'Project Not found in Trash',
);
$args = array(
'label' => 'ProjectsCPT',
'description' => 'This Post Type Adds Eyeglasses to Website',
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', 'editor'),
'taxonomies' => array( 'ProjectsTax' ),
'register_meta_box_cb' => 'add_details_metabox',
'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,
'capability_type' => 'post',
);
register_post_type( 'ProjectsCPT', $args );
}
add_action( 'init', 'cpt_Projects', 0 );
and a Metabox like
function add_details_metabox($post_type) {
$types = array('post', 'page', 'ProjectsCPT');
if (in_array($post_type, $types)) {
add_meta_box(
'details-metabox',
'Project Details',
'detail_meta_callback',
$post_type,
'normal',
'high'
);
}
}
after running the code the Metabox is showing on all Pages and Posts but not on Custom Post Type ProjectsCPT Can you please let me know what I am doing wrong? (It works fine if I remove the if statement
if (in_array($post_type, $types)) {}
but this add metabox to all Posts and Pages which is not what I need to do
Can you please add 'rewrite' => array('slug' => 'ProjectsCPT'), in your register_post_type $args
below example here:
$args = array(
'label' => 'ProjectsCPT',
'description' => 'This Post Type Adds Eyeglasses to Website',
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', 'editor'),
'taxonomies' => array( 'ProjectsTax' ),
'register_meta_box_cb' => 'add_details_metabox',
'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' => array('slug' => 'ProjectsCPT'),
'capability_type' => 'post',
);
Add metabox like
function add_details_metabox($post_type) {
$types = array('post', 'page', 'ProjectsCPT');
if (in_array($post_type, $types)) {
add_meta_box(
'details-metabox',
'Project Details',
'detail_meta_callback',
$types,
'normal',
'high'
);
}
}

Get post by category id in custom post type with custom taxonomy

This is my script for create custom post type and its taxonomy. It created successfully bust post fetching problem by category id. In my category I have 3 post but can't get any one post.
For create code :
function man_ourteam_custom_init() {
$man_labels = array(
'name' => 'Member',
'singular_name' => 'ourteam',
'add_new' => 'Add New',
'add_new_item' => 'Add New Member',
'edit_item' => 'Edit Member',
'new_item' => 'New Member',
'all_items' => 'All Member',
'view_item' => 'View Member',
'search_items' => 'Search Member',
'not_found' => 'No Member found',
'not_found_in_trash' => 'No Member found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Our Team'
);
$man_args = array(
'labels' => $man_labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'ourteam'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type('ourteam', $man_args);
register_taxonomy("member-category", array("ourteam"), array("hierarchical" => true, 'show_admin_column' => true,
"label" => "Member Categories",
"singular_label" => "Member Categories",
"rewrite" => array('slug' => 'ourteam')));
}
add_action('init', 'man_ourteam_custom_init');
For post get code :
My category id is 191 it's have 3 posts.
$ourteam_category_check = '191';
$niche_ourteam_args = array(
'post_type' => 'ourteam',
'cat' => $ourteam_category_check,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
)
);
$niche_ourteam = new WP_Query($niche_ourteam_args);
while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();
/*** My loop code ***/
endwhile;
You need to add 'tax_query', not 'cat'..
$ourteam_category_check = '191';
$niche_ourteam_args = array(
'post_type' => 'ourteam',
// 'cat' => $ourteam_category_check,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
),
'tax_query' => array(
array(
'taxonomy' => 'member-category',
'field' => 'term_id',
'terms' => $ourteam_category_check
),
),
);
$niche_ourteam = new WP_Query($niche_ourteam_args);
while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();
/*** My loop code ***/
endwhile;

Resources