Custom taxonomy archive doesnt work - wordpress

here is my registerin taxonomy function to my posts.
add_action( 'init', 'marka_taxonomies', 0 );
function marka_taxonomies()
{
$labels = array(
'name' => _x( 'Markalar', 'taxonomy general name' ),
'singular_name' => _x( 'Marka', 'taxonomy singular name' ),
'search_items' => __( 'Markalarda Ara' ),
'all_items' => __( 'Tüm Markalar' ),
'parent_item' => __( 'Alt Marka Kategorisi' ),
'parent_item_colon' => __( 'Üst Marka Kategorisi:' ),
'edit_item' => __( 'Markayı Düzenle' ),
'update_item' => __( 'Markayı Güncelle' ),
'add_new_item' => __( 'Yeni Marka Ekle' ),
'new_item_name' => __( 'Yeni Marka Adı' ),
'menu_name' => __( 'Marka' )
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'marka' )
);
register_taxonomy( 'marka', array( 'post' ), $args );
}
Thnik everthing is normal but the taxonomy-marka.php doesnt work. I still get 404 Not found on title. And there is nothing on my taxonomy archive page. What should i do also where i am wrong ?

When I updated my permalinks option the taxonomy-marka.php page listed my posts.

Related

register own taxonomy in standard posts

I want to register my own taxonomy in standard, WP posts but it doesn't work. Look at my code
register_taxonomy(
'special-category',
array('post', 'books'),
array(
'hierarchical' => false,
'labels' => array(
//...
),
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array('slug' => 'cpecial-category' )
)
);
Tin my post type books it displays correctly but in standard wp post not. Is it possible to register own taxonomy in standard posts?
Try registering taxonomy in init hook.
Try out this code.
add_action( 'init', 'create_subjects_hierarchical_taxonomy', 0 );
function create_subjects_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'Subjects', 'taxonomy general name' ),
'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
'search_items' => __( 'Search Subjects' ),
'all_items' => __( 'All Subjects' ),
'parent_item' => __( 'Parent Subject' ),
'parent_item_colon' => __( 'Parent Subject:' ),
'edit_item' => __( 'Edit Subject' ),
'update_item' => __( 'Update Subject' ),
'add_new_item' => __( 'Add New Subject' ),
'new_item_name' => __( 'New Subject Name' ),
'menu_name' => __( 'Subjects' ),
);
// Now register the taxonomy
register_taxonomy(
'special-category',
array('post', 'books'),
array(
'hierarchical' => false,
'labels' => array(
//..
),
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array('slug' => 'cpecial-category' )
)
);
}

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 ?

custom taxonomy meta box doesn't show in the WordPress admin menu

I have created a new custom taxonomy in WordPress functions.php file but when I want to add new post or edit one I cannot found my custom taxonomy meta box in the right side of the admin menu
my functions.php code for custom taxonomy:
function create_business_taxonomy() {
$labels = array(
'name' => _x( 'Businesses', 'taxonomy general name' ),
'singular_name' => _x( 'Business', 'taxonomy singular name' ),
'search_items' => __( 'Search Businesses' ),
'all_items' => __( 'All Businesses' ),
'parent_item' => __( 'Parent Business' ),
'parent_item_colon' => __( 'Parent Business:' ),
'edit_item' => __( 'Edit Business' ),
'update_item' => __( 'Update Business' ),
'add_new_item' => __( 'Add New Business' ),
'new_item_name' => __( 'New Business Name' ),
'menu_name' => __( 'Business' ),
);
register_taxonomy('business-type',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'rewrite' => array( 'business-type' => 'topic' ),
));
}
add_action( 'init', 'create_business_taxonomy', 0 );
but I can't find in where I marked in the screenshot:
Did I forget or missed something?
You need to set 'show_in_rest' => true. ( (boolean) (optional) Whether to include the taxonomy in the REST API. You will need to set this to true in order to use the taxonomy in your gutenberg metablock. ). So your code will be like this:
function create_business_taxonomy() {
$labels = array(
'name' => _x( 'Businesses', 'taxonomy general name' ),
'singular_name' => _x( 'Business', 'taxonomy singular name' ),
'search_items' => __( 'Search Businesses' ),
'all_items' => __( 'All Businesses' ),
'parent_item' => __( 'Parent Business' ),
'parent_item_colon' => __( 'Parent Business:' ),
'edit_item' => __( 'Edit Business' ),
'update_item' => __( 'Update Business' ),
'add_new_item' => __( 'Add New Business' ),
'new_item_name' => __( 'New Business Name' ),
'menu_name' => __( 'Business' ),
);
register_taxonomy('business-type',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => false,
'query_var' => true,
'show_in_rest' => true,
'rewrite' => array( 'business-type' => 'topic' ),
));
}
add_action( 'init', 'create_business_taxonomy', 0 );

Custom url structure in CPT does not work

I have to have this URL structure: http://url.pl/custompostype/taxonomy/singlepost
I have such, but I also have 404 page instead of post or list of posts. I know that there is a very popular problem, but all the tips from the Google do not work for me :( I think, that I have to change 'with_front' into false and it should work? I changed a lot of parameters in multiple ways and it still does not work.
Here is my code:
function learning() {
$labels = array(
'name' => _x( 'Myposttype', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Myposttype', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Myposttype', 'text_domain' ),
'name_admin_bar' => __( 'Myposttype', 'text_domain' ),
'archives' => __( 'Archiwa', 'text_domain' ),
'attributes' => __( 'Atrybuty', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'Wszystkie', 'text_domain' ),
'add_new_item' => __( 'Dodaj nowy post', 'text_domain' ),
'add_new' => __( 'Dodaj nowy', 'text_domain' ),
'new_item' => __( 'Nowy', 'text_domain' ),
'edit_item' => __( 'Edytuj', 'text_domain' ),
'update_item' => __( 'Zaktualizuj', 'text_domain' ),
'view_item' => __( 'Zobacz', 'text_domain' ),
'view_items' => __( 'Zobacz', 'text_domain' ),
'search_items' => __( 'Szukaj', 'text_domain' ),
'featured_image' => __( 'Obrazek wyróżniający', 'text_domain' ),
'set_featured_image' => __( 'Ustaw obrazek wyróżniający', 'text_domain' ),
'remove_featured_image' => __( 'Usuń obrazek', 'text_domain' ),
);
$args = array(
'label' => __( 'Myposttype', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ),
'taxonomies' => array( 'customtaxonomy'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-chat',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array('with_front' => false ),
);
register_post_type( 'myposttype', $args );
}
add_action( 'init', 'learning', 0 );
// Register Custom Taxonomy
function myposttype2() {
$labels = array(
'name' => _x( 'CustomTaxonomy', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'CustomTaxonomy', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'CustomTaxonomy', 'text_domain' ),
'all_items' => __( 'Wszystkie', 'text_domain' ),
'new_item_name' => __( 'Dodaj nowy', 'text_domain' ),
'add_new_item' => __( 'Dodaj nowy', 'text_domain' ),
'edit_item' => __( 'Edytuj', 'text_domain' ),
'update_item' => __( 'Zaktualizuj', 'text_domain' ),
'view_item' => __( 'Zobacz', 'text_domain' ),
'separate_items_with_commas' => __( 'Oddziel kolejne tagi przecinkami', 'text_domain' ),
'choose_from_most_used' => __( 'Wybierz spośród popularnych', 'text_domain' ),
);
$rewrite = array(
'slug' => 'myposttype/customtaxonomy',
'with_front' => false,
'hierarchical' => true,
//'has_archive' => 'myposttype',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
);
register_taxonomy( 'customtaxonomy', array( 'myposttype' ), $args );
}
add_action( 'init', 'myposttype2', 0 );
Please do one thing go to admin dashboard, Setting->Permalinks change permalinks to plain and save it.
And again change permalinks to Post Name and save it.
It will resolve 404 issue.
Rest the permalinks and also make sure same custom post type slug is not used any where in the website, this may also create the issue.
Add custom taxonomy in url with following way.
Change custom post type rewrite array item with following
'rewrite' => array('slug' => '%customtaxonomy%', 'with_front' => false ),
and then add following filter in theme's functions.php file.
function d_reset_permlinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'myposttype' ){
$terms = wp_get_object_terms( $post->ID, 'customtaxonomy' );
if( $terms ){
return str_replace( '%customtaxonomy%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'd_reset_permlinks', 1, 2 );
Now reset the permlinks and it will work . you can see its tasted in my local system http://prntscr.com/jnxvyo

Why is wordpress making a query for attachment instead of taxonomy?

I made a custom post type and I've made a taxonomy registered to the cpt and the standard 'post'. This all works perfect.
Now I needed a 2nd taxonomy for organization reasons. I've simply copied the first one and changed the name and labels.
On the backend it all works normal, but on the front end clicking on it redirects to index.php. If I look via the debug toolbar at the query it is attachment=(term-name) instead of (taxonomy)=(term-name). Does anybody have any idea why wordpress does this? (Or to put it better, how did I make wordpress do this?)
This is how the taxonomies are registered, 'arweb' is the cpt and 'onderwerp' is the normal working taxonomy:
add_action( 'init', 'register_custom_taxonomies' );
function register_custom_taxonomies(){
// ///////////////////// ONDERWERP //////////////////////
$labels = array(
'name' => _x( 'Onderwerpen', 'taxonomy general name' ),
'singular_name' => _x( 'Onderwerp', 'taxonomy singular name' ),
'search_items' => __( 'Zoek Onderwerpen' ),
'popular_items' => __( 'Popular Onderwerpen' ),
'all_items' => __( 'All Onderwerpen' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Onderwerp' ),
'update_item' => __( 'Update Onderwerp' ),
'add_new_item' => __( 'Nieuw Onderwerp' ),
'new_item_name' => __( 'Naam Nieuw Onderwerp' ),
'add_or_remove_items' => __( 'Add or remove onderwerpen' ),
'choose_from_most_used' => __( 'Choose from the most used onderwerps' ),
'not_found' => __( 'No onderwerps found.' ),
'menu_name' => __( 'Onderwerpen' ),
);
$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' => 'onderwerp' ),
);
register_taxonomy( 'onderwerp', array( "arweb","post" ), $args );
register_taxonomy_for_object_type( 'onderwerp', 'arweb' );
///////////////////// English onderwerp /////////////////////////////////////////////
$labels = array(
'name' => _x( 'Subjects', 'taxonomy general name' ),
'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
'search_items' => __( 'Zoek Subjects' ),
'popular_items' => __( 'Popular Subjects' ),
'all_items' => __( 'All Subjecten' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Subject' ),
'update_item' => __( 'Update Subject' ),
'add_new_item' => __( 'Nieuw Subject' ),
'new_item_name' => __( 'Naam Nieuw Subject' ),
'add_or_remove_items' => __( 'Add or remove onderwerpen' ),
'choose_from_most_used' => __( 'Choose from the most used onderwerps' ),
'not_found' => __( 'No onderwerps found.' ),
'menu_name' => __( 'Subjecten' ),
);
$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' => 'english-subject' ),
);
register_taxonomy( 'onderwerp_en', array( "arweb","post" ), $args );
register_taxonomy_for_object_type( 'onderwerp_en', 'post' );
register_taxonomy_for_object_type( 'onderwerp_en', 'arweb' );
}
Try updating your permalinks (by going to Settings .. Permalinks and saving). It's possible the URL is matching an outdated rewrite rule.

Categories

Resources