1

Is it possible to make URLs relative for just a singular custom post type? I used these 2 lines in make just my custom post types to use relative URLs.

add_filter( 'post_type_link', 'wp_make_link_relative' );  // Custom post type link
add_filter( 'post_type_archive_link', 'wp_make_link_relative' ); // Post type archive link

This worked great, until I realized I had a trickle down problem of my events no longer resolving properly if they were shown on a sub-domain. My site has a few different mapped sub-domains, so I'm now looking to narrow my add_filter to just the custom post type I need instead of all of them.

2
  • look at the first example for post_type_link
    – Milo
    Feb 9, 2018 at 19:19
  • @Milo Thanks, that seems to have got me on the right track.
    – jbwharris
    Feb 9, 2018 at 21:08

1 Answer 1

1

At @milo's suggestion, I used the post_type_link example and adapted it to my needs. This made it so only my chosen post type would be relative, while leaving any other custom post types functioning as usual.

function make_yourposttype_relative ( $url, $post ) {
    if ( 'yourposttype' == get_post_type( $post ) ) {
        add_filter( 'post_type_link', 'wp_make_link_relative' );  // Custom post type link
        add_filter( 'post_type_archive_link', 'wp_make_link_relative' ); // Post type archive link
    }
    return $url;
}
add_filter( 'post_type_link', 'make_yourposttype_relative', 10, 2 );

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.