Wordpress custom post parent slug - php

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

Related

Doesn't go to single post page

Created Custom Post Type. The page template for each post is single-speaker.php. But when creating a post and going to its page, it goes to the main page. Can't see page template. And the condition does not work -if(is_singular( 'speaker' )). How can this be fixed?
// create post type
function create_post_type() {
register_post_type( 'speaker',
array(
'labels' => array(
'name' => __( 'Speakers' ),
'singular_name' => __( 'speaker' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'speaker' ),
)
);
register_post_type( 'sessions',
array(
'labels' => array(
'name' => __( 'Sessions' ),
'singular_name' => __( 'sessions' )
),
'public' => true,
'has_archive' => true,
)
);
register_taxonomy(
'positions',
'speaker',
array(
'label' => ( 'Positions' ),
'hierarchical' => true,
)
);
register_taxonomy(
'countries',
'speaker',
array(
'label' => ( 'Countries' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_post_type' );
What you can do first, is to install Query Monitor to check the templates you're landing on and which conditional tags you can use to call this specific template
Try using is_single('speaker') instead
And last, check your loop. Are you calling the main loop or a custom loop for the speaker template
I hope this helps!

Wordpress, Custom post Type give 404 if more than 1 post type

I know it's common problem.
First i want say - I try: flush rewrite, reset Permalink Settings, Delete .htacces and create again using Permalink Settings, tried: flush_rewrite_rules().
Problem: Custom post_type show 404 when using Permalink Settings - /%postname%/, without work fine, same problem is with build in posts!.
When I create one post_type - it work, but when create next one - work only last post type added, first one post_type give 404.
Here is my Create post function:
add_action( 'init', 'create_post_types', 0 );
function create_post_types() {
/***********************************
*
* Register post type - Svømming
*
***********************************/
$labels = array(
'name' => _x( 'Svømming', 'Post Type General Name', TD),
'singular_name' => _x( 'Svømming', 'Post Type Singular Name', TD ),
'menu_name' => __( 'Svømming', TD ),
'name_admin_bar' => __( 'Svømming', TD ),
);
$supports = array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'revisions', 'page-attributes');
$args = array(
'label' => __( 'Svømming', TD ),
'description' => __( 'Svømming', TD ),
'labels' => $labels,
'supports' => $supports,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => THEMEURL.'/img/swimming2x2.svg',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => array( 'slug' => '/' ),
'capability_type' => 'page',
);
register_post_type( 'svomming', $args );
/***********************************
*
* Register post type - Stup
*
***********************************/
$labels = array(
'name' => _x( 'Stup', 'Post Type General Name', TD),
'singular_name' => _x( 'Stup', 'Post Type Singular Name', TD ),
'menu_name' => __( 'Stup', TD ),
'name_admin_bar' => __( 'Stup', TD ),
);
$supports = array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'revisions', 'page-attributes');
$args = array(
'label' => __( 'Stup', TD ),
'description' => __( 'Stup', TD ),
'labels' => $labels,
'supports' => $supports,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => THEMEURL.'/img/stup.svg',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => array( 'slug' => '/' ),
'capability_type' => 'page',
);
register_post_type( 'stup', $args );
/* There will be 4 more post types */
}
Those Post types must be hierarchical, and slug => '/'
I have totally no idea why there is that problem :(
The strangest part is - why work only last registered post type and why build in "posts" don't work
The problem is that you are creating two post types having the same slug. You should change the slug in one of the.
'rewrite' => array( 'slug' => '/' ),
You can't have two post types with same slug, so change name of one of them, don't forget to update your permalinks: Go to Settings-> Permalinks and click Save changes button in the bottom without changing any of the settings, this will simply update the permalinks table. And this will do it..
Use different post name. The post type you created will not be deleted from the database. So create new post type with some other name. Select the Permalink Settings to Post name

WordPress, taxonomy page not linking up

I have a custom post type named Sectors, which was made using the code below:
add_action( 'init', 'wpsites_custom_post_type' );
function wpsites_custom_post_type() {
register_post_type( 'sectors',
array(
'labels' => array(
'name' => __( 'Sectors' ),
'singular_name' => __( 'sector' ),
),
'has_archive' => true,
'hierarchical' => true,
'menu_icon' => 'dashicons-heart',
'public' => true,
'rewrite' => array( 'slug' => 'sectors', 'with_front' => false ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'sectors', 'post_tag' ),
));
}
Then I have this code, which adds the categories to sectors:
add_action( 'init', 'create_sector_cat_categories' );
function create_sector_cat_categories() {
register_taxonomy(
'SectorCategories',
'sectors',
array(
'label' => __( 'Sector Categories' ),
'rewrite' => array( 'slug' => 'sectorcategories' ),
'hierarchical' => true,
)
);
}
So, when I go to my sectors page, I currently have a list of the categories using the page 'sectorcategories.php'.
Then when you click on a sector category, this goes to display all sectors in this category, using taxonomy-sectorcategories.php.
Now, the issue I have, is when I click onto a sector from here, It is using the index.php file, yet I have a page setup for this called single-sectors.php.
Does anyone know why it is not following its taxonomy?
Sometimes if you make taxonomies on the fly, wordpress does't update it. Just goto your settings > Permalinks and click update

Custom Post Type Slug Rewrite Issues

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

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'),
)
);
}

Categories