1

I have inherited a pretty dated project, and it contains a lot of legacy code, which I am still fumbling around in.

Currently I have a custom post type with the URL: /something/faqs/{topic}/{faq-post}, however, we would like to introduce sub-topics into the mix with the URL's to the individual post to match. (i.e /something/faqs/{topic}/{sub-topic}/{faq-post}).

The previous developer had simply hidden via CSS the parent selector in the admin, so I have re-enabled that and I am now able to create sub topics and assign each individual FAQ to it.

Now it's time to update the URL, and this is where I am stumped, I've tried following a few threads on here, but still running into problems.

I will list what code I can find relating to these areas here (bear in mind, the codebase is real fragmented, so there could be something hidden somewhere else that I haven't found yet).

Registering the Custom Taxonomy:

$args = array(
  'hierarchical'      => true,
  'labels'            => $labels,
  'show_ui'           => true,
  'show_admin_column' => true,
  'query_var'         => true,
  'rewrite'           => array( 'slug' => 'topics', 'hierarchical' => true),
);

register_taxonomy('topic', array('faq'), $args);

Registering the custom post type:

$args = array(
  'labels'             => $labels,
  'public'             => true,
  'publicly_queryable' => true,
  'show_ui'            => true,
  'show_in_menu'       => true,
  'query_var'          => true,
  'rewrite'            => array( 'slug' => 'something/faqs/%topic%', 'hierarchical' 
    => true ),
  'capability_type'    => 'post',
  'menu_icon'          => 'dashicons-format-status',
  'map_meta_cap'       => true,
  'has_archive'        => false,
  'hierarchical'       => true,
  'menu_position'      => null,
  'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 
    'comments' )
);

register_post_type('faq', $args);

I have also found the following, which after debugging appears to only effect the individual post page:

add_filter('post_link', 'faq_permalink_rewrite', 1, 3);
add_filter('post_type_link', 'faq_permalink_rewrite', 1, 3);

function faq_permalink_rewrite($permalink, $post_id, $leavename) {
  if (strpos($permalink, '%topic%') === FALSE) return $permalink;

  $post = get_post($post_id);

  if (!$post) return $permalink;

  $terms = wp_get_object_terms($post->ID, 'topic');

  if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
    $taxonomy_slug = $terms[0]->slug;
  else $taxonomy_slug = 'no-type';

  return str_replace('%topic%', $taxonomy_slug, $permalink);
}

There three URL steps that need to be involved, these are:

  • /something/faqs - Lists all parent topics
  • /something/faqs/{topic} - Lists all sub topics of the parent
  • /something/faqs/{topic}/{sub-topic} - List all individual posts within the parent/{sub-topic}
  • /something/faqs/{topic}/{sub-topic}/{faq} - The actual individual FAQ.

The actual functionality appears to be working, however, everything is always at a single topic URL. for example, assigning FAQ to Sub Topic 1 would give me the URL something/faqs/sub-topic/faq but I would need this to be something/faqs/parent-topic/sub-topic/faq and likewise for the sub topic list page, the URL is currently something/faqs/sub-topic but this needs to be something/faqs/parent-topic/sub-topic.

Any ideas how I can achieve this with what I have and not rewriting a bunch of stuff?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.