Wordpress taxonomy archive throws 404 - wordpress

I have a custom taxonomy 'categories' assigned to a custom post type 'food'. I've created a template 'taxonomy-categories.php'. If I set the permalinks to 'simple', the archive of my taxonomy is working. But if I set the permalinks to 'postname', wordpress throws a 404 error. As an example, mydomain.com/food/summer/ doesn't work, but mydomain.com/?categories=summer is working and shows the content from taxonomy-categories.php.
Here's my code:
add_action('init', 'post_type_food');
function post_type_food() {
register_post_type('food',
array(
'labels' => array(
'name' => 'Rezepte',
'singular_name' => 'Rezept'
),
'public' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'has_archive' => true
)
);
register_taxonomy( 'categories', array('food'), array(
'hierarchical' => true,
'label' => 'Categories',
'singular_label' => 'Category',
'rewrite' => array( 'slug' => 'food', 'with_front'=> false )
)
);
}
Any ideas? Thanks in advance!

Related

Wordpress displays the wrong taxonomy for a custom post type on the edit page

The problem is that only on the edit page of custom post type it shows the taxonomy from Post type and not taxonomy associated with this post type. It correctly shows at Quick Edit post block:
But it doesn't show correctly on the edit page, it shows the taxonomy of the post:
The code which I'm using:
add_action( 'init', function() {
register_taxonomy( 'categories', array( 'blog' ), [
'labels' => [
'name' => 'Blog Categories',
'singular_name' => 'Blog Category',
],
'public' => true,
'hierarchical' => true,
'show_in_rest' => true,
'show_admin_column' => true,
] );
register_post_type( 'blog', array(
'labels' => array(
'name' => 'Blog posts',
'singular_name' => 'Blog post'
),
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'supports' => array('title','editor', 'thumbnail', 'author'),
'taxonomies' => array( 'categories' ),
) );
} );
I can't figure out where the glitch is and why the taxonomy works fine, but doesn't show up on the edit page.
I updated the friendly URL and it didn't help.
You have to 1st user register_post_type method and after that use register_taxonomy() . May be it work

How to organize hierarchical structure in custom post type with taxonomy (like with categories)

can anyone help me with custom post type arhive? I want there organise taxonomy term structure like with categories.
Full image url: http://i.stack.imgur.com/nzl4y.png
Add this to your functions.php file
function create_post_types() {
register_post_type('myPostType',
array(
'labels' => array(
'name' => __( 'My post types' ),
'singular_name' => __( 'My post type' )
),
'taxonomies' => array('category'), //this is the 'magic' one
'public' => true,
'show_ui' => true,
'hierarchical' => true,
'supports' => array( 'title', 'editor' )
)
);
}
add_action( 'init', 'create_post_types' );
Edit
If you want to only show the custom post type categories in the editor consider using custom fields with ACF: http://www.advancedcustomfields.com/
or you add your own taxonomy type:
https://wordpress.stackexchange.com/questions/57493/custom-taxonomy-specific-to-a-custom-post-type
function themes_taxonomy() {
register_taxonomy(
'themes_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'themes', //post type name
array(
'hierarchical' => true,
'label' => 'Themes store', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'themes', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'themes_taxonomy');
haven't tried that one though
All you need to do, is when registering the custom taxonomy, you specify which post type it belongs to. Like so:
add_action('init', 'product_tags', 0);
function product_tags() {
$labels = array(
'name' => _x( 'Product Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Product Tag', 'taxonomy singular name' ),
'menu_name' => __( 'Product Tags' )
// etc...
);
$args = array(
'hierarchical' => false,
'public' => true,
'show_in_nav_menus' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array(
'slug' => 'product-tag',
'with_front' => true,
'hierarchical' => true
),
);
// 'products' is the name of the Custom Post Type, ensuring that it will not pop up under any other post types.
register_taxonomy( 'product_tags', 'products', $args );
}

Filter WordPress custom post types by taxonomy term

I have been struggling with this for a few days now and I seem to be no closer to a solution. I have been searching forums and tutorial sites, but have ended up more confused, as there seem to be a lot of ways and variations to achieve what I am looking for.
What I want to do is create a Custom Post Type archive that can be filtered by taxonomy terms based on the url string.
_domain/products/_
_domain/products/taxonomy-term/_
_domain/products/taxonomy-term/product-1_
So the taxonomy term will only display custom posts of that type.
I have got as far as this. Which seems to work for domain/products/taxonomy_term/product_1, but isn't picking up any archive templates.
// define custom post types
add_action( 'init', 'create_products' );
function create_products() {
register_post_type( 'products',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
'rewrite' => array('slug' => 'products/%product_cat%', 'with_front' => true ),
'hierarchical' => true,
'query_var' => true,
'show_in_nav_menus' => true,
'menu_position' => 5
)
);
}
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
register_taxonomy(
'product_cat',
'products',
array(
'labels' => array(
'name' => 'Product Categories',
'add_new_item' => 'Add New Product',
'new_item_name' => "New Product Category"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'products', 'with_front' => true ),
'query_var' => true,
)
);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'product_listing')
return $link;
if ($cats = get_the_terms($post->ID, 'product_cat'))
$link = str_replace('%product_cat%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
For me it was a conflict in naming. I had a post type with the same name as the taxonomy. That caused a conflict.
register_post_type( 'some_name' );
register_taxonomy( 'some_name' );
Rename the taxonomy to something unique.
register_taxonomy( 'some_tax_name' );

Category Template for custom posts

Hi I would like to use category-(slug).php for my custom post type.
In functions.php I have defined the movies post type as following
add_action( 'init', 'create_post_type_movie' );
function create_post_type_movie() {
register_post_type( 'ra_movie',
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'has_archive' =>true,
'rewrite' => array('slug' => 'movie'),
'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields','revisions'),
'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
));
}
So the slug of teh post type is movie. when I load
../category-movie/design
it displays the 404. I am using wordpress 3.5.1 so it should be able to display category-(slug).php
Any ideas?
Thank you

WordPress - Custom Post Type permalink collision

I use WordPress with Custom Post Types and Custom Taxonomies.
Problem
I try to create a post with the path /nyheter/bolag/post/ but it gets redirected to /bolag/post/. It's because I have a Custom Post Type with the slug bolag and WP probably try to correct the path.
How is this solved? Code, plugins?
Code placed in functions.php
<?php
add_action( 'init', 'create_post_type' );
add_action( 'init', 'register_taxonomies' );
function create_post_type() {
$args_bolag = array(
'labels' => array(
'name' => 'Bolag',
'singular_name' => 'Bolag'
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'bolag'),
);
$args_nyheter = array(
'labels' => array(
'name' => 'Nyheter',
'singular_name' => 'Nyheter'
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'nyheter'),
);
register_post_type( 'bolag', $args_bolag );
register_post_type( 'nyheter', $args_nyheter );
}
function register_taxonomies()
{
$args_firma = array(
'label' => __( 'Firma' ),
'rewrite' => array( 'slug' => 'nyheter/bolag' ),
'hierarchical' => true,
);
register_taxonomy( 'firma', 'nyheter', $args_firma );
}

Resources