Post custom types and page hierarchy – Error 404

I have defined two types of custom publications:

I also have 7 pages using a definite rubric model: (template-rubrique.php)

Both my Theme and Plug have an ACF post-object field.

  • The Theme ACF Post-Object field is used to target a rubric.
  • The post-object field of the ACF form makes it possible to target a Theme.

I would like my URLs to be in the following format: example.com/myRubriqueName/myThemeName/myFicheName.

monRubriqueName is a page, while myThemeName and myFicheName are CPT.

Until now, all my plug and theme the articles are well generated, but they end up in a page 404.

I use the code of this post, which I have tried to adapt to my situation.


CPT registering:

register_post_type (& # 39; theme, array (
& # 39; labels & # 39; => array (
& # 39; name & # 39; => & # 39; Themes & # 39;
& # 39; singular_name & # 39; => & # 39; Theme & # 39 ;,
)
& # 39; public & # 39; => true,
& # 39; has_archive & # 39; => false,
& # 39; hierarchical & # 39; => false,
& # 39; menu_icon & # 39; => & # 39; dashicons-art & # 39;
& # 39; rewrite & # 39; => array (
& # 39; slug & # 39; =>% Heading%, heading% is used as a generic parameter
& # 39; with_front & # 39; => false
)
));


register_post_type (& # 39; listing & # 39;
& # 39; labels & # 39; => array (
& # 39; name & # 39; => & # 39; Cards & # 39 ;,
& # 39; singular_name & # 39; => & # 39; Card & # 39 ;,
)
& # 39; public & # 39; => true,
& # 39; has_archive & # 39; => false,
& # 39; hierarchical & # 39; => false,
& # 39; menu_icon & # 39; => & # 39; dashicons-clipboard & # 39;
& # 39; rewrite & # 39; => array (
& # 39; slug & # 39; =>% Heading% /% theme% & # 39; //% heading% /% theme% is used as placeholder
& # 39; with_front & # 39; => false
)
));

Rewrite the rules

function fiche_rewrite () {

add_rewrite_rule (
& # 39;% subject% & # 39 ;,
& # 39; ([^&]+) & # 39;
& # 39; theme = & # 39;
)
}
add_action (& # 39; init & # 39 ;, & # 39; plug_rewrite & # 39;);


function theme_rewrite () {

add_rewrite_rule (
& # 39;% heading% & # 39;
& # 39; ([^&]+) & # 39;
& # 39; heading = & # 39;
)
}
add_action (& # 39; init & # 39 ;, & # 39; theme_rewrite & # 39;);

Rewriting CPT placeholders

function gpc_custom_post_link_replacements ($ post_link, $ post) {

$ cpts = array ('theme', 'listing');

if (empty ($ post) ||! in_array ($ post-> post_type, $ cpts)) {
return $ post_link;
}

switch ($ post-> post_type) {
case & fiche;
$ theme_id = get_field (& # 39; attachment-record & # 39 ;, post-> ID);
$ theme_slug = get_post_field (& # 39; post_name & # 39 ;, $ theme_id);

$ topic_id = get_field (& theme-attachment & $ 39 ;, $ theme_id);
$ heading_slug = get_post_field (& # 39; post_name & # 39 ;, $ topic_id);

if (! empty ($ theme_slug) &&! empty ($ section_slug)) {
$ post_link = str_replace (% heading%, $ heading_slug, $ post_link);
$ post_link = str_replace (% theme% & # 39 ;, $ theme_slug, $ post_link);
}

Pause;

case "theme":

$ topic_id = get_field (& # 39; theme-attachment & # 39 ;, $ post-> ID);
$ heading_slug = get_post_field (& # 39; post_name & # 39 ;, $ topic_id);

if (! empty ($ section_slug)) {
$ post_link = str_replace (% heading%, $ heading_slug, $ post_link);
}

Pause;

}

return $ post_link;
}
add_filter (& # 39; post_type_link & # 39; wpc_custom_post_link_replacements & # 39 ;, 9,2);

Redirection of incompatible messages

function custom_post_redirects () {

global $ post, $ wp_query;
$ redirect_to = get_home_url ();

if (! is_singular (& # 39;) &&! is_singular (& # 39; theme)) {
return;
}


if (is_singular (& # 39; record)) {
$ given_slug = $ wp_query-> get (theme #);
$ expected_theme = get_field (& # 39; field-attachment & # 39 ;, $ post-> ID);

if (empty ($ given_slug) || empty ($ expected_theme)) {
wp_redirect ($ redirect_to);
exit();
}

$ wait_slug = get_post_field (& # 39; post_name & # 39 ;, expected_theme);

if ($ Given_slug! == $ expected_slug) {
wp_redirect ($ redirect_to);
exit();
}
}

else if (is_singular (& # 39; theme)) {
$ given_slug = $ wp_query-> get (topic & # 39;);
$ expected_rubrique = get_field ("theme-attachment", $ post-> ID);

if (empty ($ given_slug) || empty ($ expected_theme)) {
wp_redirect ($ redirect_to);
exit();
}

$ wait_slug = get_post_field (& # 39; post_name & # 39 ;, expected_rubrique);

if ($ Given_slug! == $ expected_slug) {
wp_redirect ($ redirect_to);
exit();
}
}

}
add_action (& # 39; template_redirect & # 39 ;, & gt; custom_post_redirects & # 39;);