Stripping custom post type from the permalink

Hi there,

I’m trying to amend the permalink for some things on my site but for some reason it’s not working. I’m using preg_replace to remove a part of the URL so it will create a new URL.

Here is some info:

the_permalink() currently returns a URL like this:

http://mydomain.com/cpo_feature/postname/

I want to remove the “cpo_feature/” part so it becomes:

http://mydomain.com/postname

I have tried the following code:

$link = the_permalink();
$pattern = ‘|/|cpo_feature’;
$replacement = ‘’;
$tidyLink = preg_replace($pattern, $replacement, $link);

But when I echo $tidyLink it still shows the cpo_feature directory.

I have also tried escaping the “/” by putting a "" in front of it instead of using the denominator.

What am I doing wrong?

Try this:

http://php.net/manual/en/function.str-replace.php

What does the permalink setting look like in the Admin Control Panel?

It’s just set to postname.

Hmmm. if that is so, I don’t know how “cpo_feature” would be getting into the URL.

Is your WordPress installed in a subfolder by that name?

“cpo_feature” is in your “siteurl” and / or “home” settings?

It’s a custom post type so the slug is being put in there. I believe that’s standard procedure for custom post types. Which is why I’m trying to remove it because I want to link some featured boxes to the relevant page on the site but the boxes are created with a custom post type so I need to change the permalink.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.