Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the following code to create a short permalink for one of my custom post types. I have another cpt that I wish to just use the default permalink structure, so what would be the best way to restrict this filtering to just cpt1? to be honest I thought one of the functions here would already handle this (add_permastruct?) but the same permalink rewrite is applied to other cpts. the documentation in the codex is a little thin on this… thanks Rhys

function cpt1_rewrite() {
global $wp_rewrite;
$queryarg = 'post_type=cpt1name&p=';
$wp_rewrite->add_rewrite_tag('%cpt1_id%', '([^/]+)', $queryarg);
$wp_rewrite->add_permastruct('cpt1name', '/cpt1/%cpt1_id%', false);}

function cpt1_permalink($post_link, $id = 0, $leavename) {
global $wp_rewrite;
$post = &get_post($id);
if ( is_wp_error( $post ) )
    return $post;   
$newlink = $wp_rewrite->get_extra_permastruct('cpt1name');
$newlink = str_replace("%cpt1_id%", $post->ID, $newlink);
$newlink = home_url(user_trailingslashit($newlink));
return $newlink;}

add_action('init', 'cpt1_rewrite');
add_filter('post_type_link', 'cpt1_permalink', 1, 3);
share|improve this question
1  
Try to explain the use case better –  Bainternet Mar 3 '11 at 14:20
    
Multiple custom post types registered, but only 1 requiring the id based custom rewrite outlined above (/cpt1/172 vs /cpt1/post-title). –  rhyslbw Mar 3 '11 at 19:43
    
This is how I have currently handled it: function cpt1_permalink($post_link, $id = 0, $leavename) { if(get_post_type() == 'cpt1name'){ global $wp_rewrite; $post = &get_post($id); if ( is_wp_error( $post ) ) return $post; $newlink = $wp_rewrite->get_extra_permastruct('cpt1name'); $newlink = str_replace("%cpt1_id%", $post->ID, $newlink); $newlink = home_url(user_trailingslashit($newlink)); return $newlink; } else { return $post_link; } } Is this the best way? –  rhyslbw Mar 3 '11 at 21:06

1 Answer 1

up vote 1 down vote accepted

The post_type_link hooks gets called for all links to custom post types, you are responsible for checking the link type. Remember to use the passed $post object (not post ID), otherwise you check the current global $post variable, which may not be the post for which you are creating a link now. So the code you added in your comment is almost correct, I would write it like this:

function cpt1_permalink( $post_link, $post, $leavename )
{
    // Yoda condition to be safe
    // http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2430307#2430307
    if ( 'cpt1name' != $post->post_type ) {
        return $post_link;
    }
    // Rest of your code
}
share|improve this answer
    
Thanks Jan, makes a lot of sense –  rhyslbw Mar 27 '11 at 23:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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