2

How can I change the page that the 'view' action links to on the list post screen for a custom post type?

Update

I've got it to work with normal post types using the below code but where do I define the custom post type?

function change_link($post_url,$post) {
    return '/video?id='.$post->ID;
}
add_filter('post_link',"change_link",10,2);

2 Answers 2

2

By adding a filter to the 'post_link' hook. See the get_permalink() function for more info.

For custom post types, you can use the 'post_type_link' hook.

It's a lot easier if you follow the source code (this is for v3.0):

1
  • See updated answer.
    – scribu
    Commented Oct 6, 2010 at 9:28
9

Based on the update of your question:

function change_link( $permalink, $post ) {
    if( $post->post_type == 'video' ) { // assuming the post type is video
        $permalink = home_url( 'video?id='.$post->ID );
    }
    return $permalink;
}
add_filter('post_type_link',"change_link",10,2);
5
  • I tried that before and it doesn't work. It just removes the permalink altogether for my blog posts (because the filter is still being applied to standard 'posts' but the function's not returning anything) and doesn't change the video custom posts.
    – fxfuture
    Commented Sep 30, 2010 at 17:49
  • 1
    I edited my answer. I may have been very tired when I wrote it. :)
    – sorich87
    Commented Sep 30, 2010 at 18:24
  • No problem! Your code works for normal posts if I change ($post->post_type == 'post') but when I use 'video' or another of my custom post types it doesn't work?
    – fxfuture
    Commented Sep 30, 2010 at 20:06
  • I'm probably missing something obvious, but why is manipulating the post_link to add a query var for the custom post type even necessary? I've never had to do this - my "view" links (and all other permalinks for that matter) already have the post type inserted in the URL...
    – somatic
    Commented Oct 22, 2010 at 17:52
  • He wanted all the videos posts to go to the same page (not to their default page) and the post id passed in the URL. E.g.: mysite.com/video?id=23, mysite.com/video?id=54, etc.
    – sorich87
    Commented Oct 22, 2010 at 18:18

Your Answer

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

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