I've defined two custom post types:
I've also 7 pages using a defined rubrique template: (template-rubrique.php)
Both my Theme and Fiche have an ACF post-object field.
I'd like my CPTS URL to be in the following format: example.com/myRubriqueName/myThemeName/myFicheName
.
myRubriqueName
is a Page, whilemyThemeName
andmyFicheName
are CPT.
Up to now, all my fiche and theme posts URL are well generated, but they end up in a 404 page. Furthermore, my posts and pages are redirected to the front-page.
I'm using the code from this post, which I tried to adjust to my situation.
CPTs registering:
register_post_type('theme', array(
'labels' => array(
'name' => 'Thèmes',
'singular_name' => 'Thème',
),
'public' => true,
'has_archive' => false,
'hierarchical' => false,
'menu_icon' => 'dashicons-art',
'rewrite' => array(
'slug' => '%rubrique%', // %rubrique% is used as placeholder
'with_front' => false
)
));
register_post_type('fiche', array(
'labels' => array(
'name' => 'Fiches',
'singular_name' => 'Fiche',
),
'public' => true,
'has_archive' => false,
'hierarchical' => false,
'menu_icon' => 'dashicons-clipboard',
'rewrite' => array(
'slug' => '%rubrique%/%theme%', // %rubrique%/%theme% is used as placeholder
'with_front' => false
),
));
Rewrite rules
function fiche_rewrite() {
add_rewrite_tag(
'%theme%',
'([^&]+)',
'theme='
);
}
add_action( 'init', 'fiche_rewrite' );
function theme_rewrite() {
add_rewrite_tag(
'%rubrique%',
'([^&]+)',
'rubrique='
);
}
add_action( 'init', 'theme_rewrite' );
CPT placeholder rewriting
function gpc_custom_post_link_replacements( $post_link, $post ) {
$cpts = array('theme', 'fiche');
if ( empty( $post ) || !in_array($post->post_type, $cpts) ) {
return $post_link;
}
switch ($post->post_type) {
case 'fiche':
$theme_id = get_field('fiche-attachment', $post->ID);
$theme_slug = get_post_field( 'post_name', $theme_id );
$rubrique_id = get_field('theme-attachment', $theme_id);
$rubrique_slug = get_post_field('post_name', $rubrique_id);
if ( !empty( $theme_slug ) && !empty( $rubrique_slug ) ) {
$post_link = str_replace('%rubrique%', $rubrique_slug, $post_link );
$post_link = str_replace('%theme%', $theme_slug, $post_link );
}
break;
case 'theme':
$rubrique_id = get_field('theme-attachment', $post->ID);
$rubrique_slug = get_post_field('post_name', $rubrique_id);
if ( !empty( $rubrique_slug ) ) {
$post_link = str_replace('%rubrique%', $rubrique_slug, $post_link );
}
break;
}
return $post_link;
}
add_filter( 'post_type_link', 'wpc_custom_post_link_replacements', 9, 2 );
Mismatched related posts redirecting
function custom_post_redirects() {
global $post, $wp_query;
$redirect_to = get_home_url();
if( ! is_singular( 'fiche' ) && ! is_singular('theme') ) {
return;
}
if( is_singular('fiche') ) {
$given_slug = $wp_query->get( 'theme' );
$expected_theme = get_field('field-attachment', $post->ID );
if( empty( $given_slug ) || empty( $expected_theme ) ) {
wp_redirect( $redirect_to );
exit();
}
$expected_slug = get_post_field( 'post_name', $expected_theme );
if( $given_slug !== $expected_slug ) {
wp_redirect( $redirect_to );
exit();
}
}
else if( is_singular('theme' ) ) {
$given_slug = $wp_query->get( 'rubrique' );
$expected_rubrique = get_field('theme-attachment', $post->ID);
if( empty( $given_slug ) || empty( $expected_theme ) ) {
wp_redirect( $redirect_to );
exit();
}
$expected_slug = get_post_field( 'post_name', $expected_rubrique );
if( $given_slug !== $expected_slug ) {
wp_redirect( $redirect_to );
exit();
}
}
}
add_action( 'template_redirect', 'custom_post_redirects' );
Custom post types are specific post types that have been added to WordPress using custom code or plugins. The idea is that you may want to add additional functionality to your site but don't want to add everything as a standard post. Imagine you would like to add in a section to your website for your team members.
Post Types is a term used to refer to different types of content in a WordPress site. In 2003, WordPress was primarily launched as a blogging platform. Posts is a common blogging terminology that stuck with WordPress as it evolved into a robust content management system (CMS).
use get_terms() function to fetch custom taxonomy by its slug, in your case slug is dining-category. read function refrence from wordpress codex website and try this.
A custom post type is nothing more than a regular post with a different post_type value in the database. The post type of regular posts is post , pages use page , attachments use attachment and so on. You can now create your own to indicate the type of content created.
'Flush' the permalinks.
a 404 when one has added code that creates or affects custom post types often is solved by simply 'flushing' the permalinks. One can do this manually by visiting settings -> permalinks.
https://typerocket.com/flushing-permalinks-in-wordpress/.
It's an 'expensive' operation, so it is suggested not to include the code to do it https://codex.wordpress.org/Function_Reference/flush_rewrite_rules, but just do it once manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With