0

I have made some custom post types with Magicfields. I need to set the parent page for a custom post type to a standard wordpress page. Figured that out with the help of this: http://janina.tumblr.com/post/3588081423/post-parent-different-type

But now the parent page and it's parent is being added to the permalink and the view page or the new URL is returning a 404.

Example:

  • Custom Post Type Products light and post x-rays
  • Set Parent of x-rays Product Page to waves Page which is a standard WP Page and has a parent of science
  • this creates this URL site.com/light/science/waves/x-rays - this breaks
  • without a parent for x-rays, the URL is site.com/light/x-rays
  • with the cross post type parent - the science/waves is added and view page gives 404, but if I removed the /science/waves from the URL, the site.com/light/x-rays works.

So it seems the permalink structure doesn't know how to go from one post type to another. Which makes the get_permalink function not work, which causes all kinds of headaches.

Suggestions, questions?

0

I think that you need only to change the way get_permalink create url for the cpt, ignoring the parent.

You can do it adding a filter to 'post_type_link' hook and then regenerate the permalink for the product cpt in the same way is done in core, just ignoring the parent:

add_filter('post_type_link', 'reset_light_link', 999, 3);

function reset_light_link( $post_link, $post, $leavename ) {
  if ( $post->post_type != 'light' || in_array($post->post_status, array('draft','pending','auto-draft')) ) return $post_link;
  global $wp_rewrite;
  $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
  if ( ! $leavename ) $post_link = str_replace("%$post->post_type%", $post->post_name, $post_link);
  return home_url( user_trailingslashit($post_link) );
}

The code for reset_light_link function is just a modded version of get_post_permalink, the function used in core to generate the permalink (and that fires the 'post_type_link' hook) when get_permalink is called for a cpt.

Here the code for that function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.