Custom Post Type Slug Rewrite Issues - php

To put a long story short, I'm trying to get the custom post type category to show where it says 'project-item' as you can see HERE
I presume it has something to do with the way I've registered the custom post type such as in this code:
<?php
add_action( 'init', 'register_posts' );
function register_posts() {
register_post_type( 'team_post',
array(
'labels' => array(
'name' => __( "Team" ,"um_lang"),
'singular_name' => __( "Team" ,"um_lang")
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE),
'supports' => array('title','editor','thumbnail','page-attributes')
)
);
register_post_type( 'project_post',
array(
'labels' => array(
'name' => __( "Projects","um_lang"),
'singular_name' => __( "Project" ,"um_lang")
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE),
'supports' => array('title','editor','thumbnail','page-attributes')
)
);
register_taxonomy('project_category',array (
0 => 'project_post',
),array( 'hierarchical' => true, 'label' => __('Projects Category',"um_lang"),'show_ui' => true,'query_var' => true,'singular_label' => __('Projects Category',"um_lang")) );
}
?>

This tells wordpress to insert the text project_item into the slug, which is what is happening:
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE)
And this says to insert the value of project_item:
'rewrite' => array('slug' => "%project_item%", 'with_front' => TRUE)

Try this:
global $wp_rewrite;
$movies_structure = '/movies/%year%/%monthnum%/%day%/%movies%';
$wp_rewrite->add_rewrite_tag("%movies%", '([^/]+)', "movies=");
$wp_rewrite->add_permastruct('movies', $movies_structure, false);
return link:
domain.com/movies/movie_name

I figured it out. I installed this plugin: https://wordpress.org/plugins/custom-post-type-permalinks/
Then instead of using %categories% (Which didn't work) I used the specific custom post type category name (which was %project_category%)

Related

Wordpress custom post parent slug

What is the best way to structure a parent/child custom post type combo, so that my slug url can look like this
https://www.mysite.co.uk/system/parent/child
The system part of the slug is vital, it is part of the multiple theme plugin for wordpress. Also, the parent/child parts of the url will not always be the same. Lets say I make a course called "How to ride a bike", and a series inside it called "Series 1", my slug needs to look like this
https://www.mysite.co.uk/system/how-to-ride-a-bike/series-1
at the moment my custom post types look like this
<?php
function create_post_type() {
//Courses
register_post_type( 'courses',
[
'labels' => array(
'name' => __( 'Courses' ),
'singular_name' => __( 'Course' )
),
'description' => 'All courses',
'public' => true,
'rewrite' => array(
'slug' => 'system',
),
'menu_icon' => 'dashicons-welcome-learn-more',
'supports' => ['title', 'custom_fields', 'page-attributes']
]
);
//Series
register_post_type( 'series',
[
'labels' => array(
'name' => __( 'Series' ),
'singular_name' => __( 'Series' )
),
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=courses',
'description' => 'Course series',
'public' => true,
'rewrite' => array('slug' => '/'),
'hierarchical' => true,
'with_front' => false,
'capability_type' => 'post',
'has_archive' => false,
'supports' => ['title', 'custom_fields', 'page-attributes']
]
);
}
add_action( 'init', 'create_post_type' );
?>
I'm not 100% sure on the best way to actually link the child element to the parent? At the moment I have an advanced custom field on the child post type (series) where you select the parent (course) its linked to.
What is the best way to structure my custom post types, so that I can link a child to a parent and have the slug specified above?
You can use parent/child structures of the same custom post type using the page-attributes value in the supports array.
You do need to flush the rewrite rules of Wordpress before adding the sub-item. Just visit the permalinks page and hit save a couple of times.
Here is my example with the custom post type subcase:
register_post_type( 'subcase',
array(
'hierarchical' => true, // needs to be true
'labels' => array(
'name' => __( 'Sub Cases' ),
'singular_name' => __( 'Sub Case' )
),
'taxonomies' => array( 'category', 'post_tag' ),
'public' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => 'cases/subcases',
'with_front' => true
),
'supports' => array(
'page-attributes', // creates parent select box
'title',
'editor',
'excerpt',
'thumbnail'
)
)
);

fail to register new posttype and new taxonomie

this is my code and i'm in a serios problem with it : any help please.
first i have registered the post type and second i have registered the taxonomie
an always i got invalid taxonomies error
add_action('init', 'create_categorie_video_tax');
function create_categorie_video_tax()
{
register_post_type('video',
array(
'labels' => array(
'name' => 'video',
'singular_name' => 'video'
),
'public' => true,
'has_archive' => true,
)
);
register_taxonomy(
'categorie-video',
'video',
array(
'label' => 'categorie-video',
'rewrite' => array('slug' => 'video'),
'hierarchical' => true,
)
);
}

Custom post type permalink remove

I want to remove the custom post type name of the permalink.
I created a custom post type like this:
add_action( 'init', 'register_projects' );
function register_projects() {
register_post_type( 'proyectos_post',
array(
'labels' => array(
'name' => __( 'Proyectos' )
),
'public' => true,
'supports' => array( 'title', 'editor'),
'hierarchical' => true,
'menu_position' => 4
)
);
}
The problem is that when I enter to a post, the url gets: www.myweb.com/proyectos_post/mypost
I want it to be like this:
www.myweb.com/mypost
Is it possible to do it without external plugins?
Try this.
'rewrite' => array('slug' => 'custom-post-name', 'with_front' => FALSE)

Wordpress Custom Post Type link not showing content

I have created 2 custom post types. One called artist and one called work. When I create a post in artist and view it the link is fine and I can view the content. However in work when I add a new post and go to view it it says that it cannot find the content and the page is missing.
Here is my functions.php code.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'artist',
array(
'labels' => array(
'name' => __( 'Talent' ),
'singular_name' => __( 'talent' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'thumbnail' ),
'supports' => array('thumbnail', 'title', 'editor'),
'rewrite' => array('slug' => 'talent'),
)
);
register_post_type( 'work',
array(
'labels' => array(
'name' => __( 'Content' ),
'singular_name' => __( 'content' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'thumbnail' ),
'supports' => array('thumbnail', 'title', 'editor'),
'rewrite' => array('slug' => 'content'),
)
);
}

Custom Post Type URL redirected wrong

I've got some Wordpress CPTs. They correct URL should be /wordpress/training/training-page/, and that's the URL I see in the admin manage page, but when I click the link the URL I end up with is /wordpress/blog/2010/05/21/training-page/.
I deactivated my plugins with no success. Does anyone know how to keep the correct URL intact?
Here's my code:
<?php
add_action( 'init', 'tv_content_posttype' );
function tv_content_posttype() {
register_taxonomy('content',
'training',
array(
'hierarchical' => true,
'label' => 'Content Index',
'query_var' => true,
'rewrite' => true
)
);
register_post_type( 'training',
array(
'type' => 'page',
'labels' => array(
'name' => __( 'TV Training' ),
'singular_name' => __( 'TV Training' )
),
'public' => true,
'rewrite' => array(
'with_front' => false,
'slug' => 'training',
),
'hierarchical' => true,
'query_var' => true,
'taxonomies' => array( 'content' ),
)
);
}
Just a few observations: there's no such thing as a 'type' argument for register_post_type(), so you can get rid of that line. Second, 'with_front' => false is telling WordPress that the URL structure should be /training/training-page/. /wordpress/ in this isntance is the 'front' part you're telling it to leave off. Additionally, you don't need to add 'taxonomies' for the post_type, but you do need to register the post type before you register the taxonomy. So try this:
<?php
add_action( 'init', 'tv_content_posttype' );
function tv_content_posttype() {
register_post_type( 'training',
array(
'labels' => array(
'name' => __( 'TV Training' ),
'singular_name' => __( 'TV Training' )
),
'public' => true,
'rewrite' => array(
'with_front' => true,
'slug' => 'training',
),
'hierarchical' => true,
'query_var' => true,
)
);
register_taxonomy('content',
'training',
array(
'hierarchical' => true,
'label' => 'Content Index',
'query_var' => true,
'rewrite' => true
)
);
}

Categories