Custom url structure in CPT does not work - wordpress

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

Related

Check is custom post type

I have a custom post type :
// Custom posttype Events
$labels = array(
'name' => _x('Events', 'Post Type General Name'),
'singular_name' => _x('Events', 'Post Type Singular Name'),
'menu_name' => __('Events'),
'parent_item_colon' => __('Events:'),
'all_items' => __('All Items'),
'view_item' => __('View Item'),
'add_new_item' => __('Add New Event'),
'add_new' => __('Add New'),
'edit_item' => __('Edit Item'),
'update_item' => __('Update Item'),
'search_items' => __('Search Item'),
'not_found' => __('Not found'),
'not_found_in_trash' => __('Not found in Trash'),
);
$args = array(
'labels' => $labels,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', 'custom-fields',),
'taxonomies' => array('post_tag'),
'hierarchical' => false,
'rewrite' => array('slug' => __('events')),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-images-alt2',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type('events', $args);
And a taxonomy for custom post type Events:
// Add new "Type" taxonomy to Events
register_taxonomy('type-events', 'event', array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Types', 'taxonomy general name', 'my_theme' ),
'singular_name' => _x( 'Types', 'taxonomy singular name', 'my_theme' ),
'search_items' => __( 'Search Type', 'my_theme' ),
'all_items' => __( 'All Types', 'my_theme' ),
'parent_item' => __( 'Parent Type', 'my_theme' ),
'parent_item_colon' => __( 'Parent Type:', 'my_theme' ),
'edit_item' => __( 'Edit Type', 'my_theme' ),
'update_item' => __( 'Update Type', 'my_theme' ),
'add_new_item' => __( 'Add New Type', 'my_theme' ),
'new_item_name' => __( 'New Type', 'my_theme' ),
'menu_name' => __( 'Types', 'my_theme' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'type-events',
'with_front' => false,
'hierarchical' => true
),
));
In Dashboard Admin, i create two taxonomy type event :
Taxonomies
Custom post type and taxonomy use the same template.
In file template, I want check if it is post type or taxonomy.
Currently, I use is_post_type_archive() to check, but the both return true. That is not what I need.
How to check if this is custom post type or taxonomy?
If you want to check if a post is the custom post type events within the loop you can use this:
<?php if ( get_post_type() === 'events' ) {
/* Do Stuff */
} ?>
If this is outside of the loop, you need to pass the post id into get_post_type():
<?php if ( get_post_type( $post_id ) === 'events' ) {
/* Do Stuff */
} ?>
Edit
You can test for multiple custom post types this way:
<?php if ( get_post_type() === 'events' || get_post_type() === 'promos' || get_post_type() === 'courses' ) {
/* Do Stuff */
} ?>
This is little complicated, maybe the best way is to check with is_post_type_archive() and is_tax(), before you load the themplate, but in your case you may try with get_queried_object().
Codex - get_queried_object
The returned object will have properties taxonomy, term_id, and term_taxonomy_id if this is taxonomy.
So maybe you can use something like get_queried_object()->taxonomy to determine if this is taxonomy and get its name and get_queried_object()->query_var for custom_post_type.
if ( isset( get_queried_object()->taxonomy ) {
//do taxonomy work ...
}
if ( property_exists( get_queried_object()->query_var ) {
//post things
}
function custom_post_type() {
$singular="Car";
$plural="cars";
$labels = array(
'name' => _x( $plural, 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( $singular, 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( $plural, 'text_domain' ),
'name_admin_bar' => __( $singular, 'text_domain' ),
'archives' => __( '$singular Archives', 'text_domain' ),
'attributes' => __( $singular.' Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent '.$singular.':', 'text_domain' ),
'all_items' => __( 'All '.$plural, 'text_domain' ),
'add_new_item' => __( 'Add New '.$singular, 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New '.$singular, 'text_domain' ),
'edit_item' => __( 'Edit '.$singular, 'text_domain' ),
'update_item' => __( 'Update '.$singular, 'text_domain' ),
'view_item' => __( 'View '.$singular, 'text_domain' ),
'view_items' => __( 'View '.$singular, 'text_domain' ),
'search_items' => __( 'Search '.$singular, 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into '.$singular, 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this '.$singular, 'text_domain' ),
'items_list' => __( $singular.' list', 'text_domain' ),
'items_list_navigation' => __( $singular.' list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter '.$singular.' list', 'text_domain' ),
);
$supports =array('title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats');
$texonomies =array('category', 'post_tag');
$args = array(
'label' => __( $singular, 'text_domain' ),
'description' => __( $singular.' Description', 'text_domain' ),
'labels' => $labels,
'supports' => $supports,
'taxonomies' => $texonomies,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 8,
'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' => 'page',
'menu_icon' => 'dashicons-video-alt'`enter code here`
);
register_post_type( 'post_type', $args );
}
add_action( 'init', 'custom_post_type');

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')

show white screen for single-type.php and archive-type.php file in wordpress

i have created custom post type 'gallery' to display in single page named single-gallery.php and archive-gallery.php file but it provides me white screen
Please help me to solve my problem... here is my code..
// Register Custom Post Type
function gallery() {
$labels = array(
'name' => _x( 'gallery', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'gallery', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'gallery', 'text_domain' ),
'parent_item_colon' => __( 'Parent gallery', 'text_domain' ),
'all_items' => __( 'All gallery', 'text_domain' ),
'view_item' => __( 'View gallery', 'text_domain' ),
'add_new_item' => __( 'Add New gallery', 'text_domain' ),
'add_new' => __( 'New gallery', 'text_domain' ),
'edit_item' => __( 'Edit gallery', 'text_domain' ),
'update_item' => __( 'Update gallery', 'text_domain' ),
'search_items' => __( 'Search gallerys', 'text_domain' ),
'not_found' => __( 'No gallerys found', 'text_domain' ),
'not_found_in_trash' => __( 'No gallerys found in Trash', 'text_domain' ),
);
$args = array(
'label' => __( 'gallery', 'text_domain' ),
'description' => __( 'gallery information pages', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'custom-fields', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'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( 'gallery', $args );
}
// Hook into the 'init' action
add_action( 'init', 'gallery', 0 );
Sometimes just visiting the Permalink settings in wordpress admin fixes such issues. Have you tried that?

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.

Custom taxonomy archive doesnt work

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.

Resources