Skip Navigation

[Resolved] Content template not working for post type with rewritten permalinks

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 14 replies, has 2 voices.

Last updated by rithvikU 1 year, 1 month ago.

Assisted by: Waqar.

Author
Posts
#2525837

I used the following code to modify the permalinks for the post type called 'professional'. Although the links are all updated to reflect this change, the content template linked to this post type is not being applied. You can view the issue here by clicking on one of the posts - hidden link

function tssupp_custom_rewrite($args, $post_type) {
	$slug = 'professional'; 
	if ($post_type == $slug) {
		$args['rewrite'] = array('slug' => 'resources/%profession%', 'with_front' => 0);
	}
	return $args;
}
add_filter('register_post_type_args', 'tssupp_custom_rewrite', 10, 2);

function profession_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%profession%')) {
        $profession_type_term = get_the_terms($post->ID, 'profession');
        if (!empty($profession_type_term))
            $post_link = str_replace('%profession%', array_pop($profession_type_term)->
            slug, $post_link);
        else
            $post_link = str_replace('%profession%', 'uncategorized', $post_link);
    }
    return $post_link;
}
add_filter('post_type_link', 'profession_permalink_structure', 10, 4);
#2526855

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

I checked the links for the two 'professional' posts that were showing on the page, but they both led to a '404 - page not found' message.

Technically the content template assignment logic shouldn't be affected by the rewrite rules. Does the content template show correctly if the custom code for the rewrite rule is removed?

regards,
Waqar

#2527781

Hey,

So I did a little more digging and I'm quite sure it has something to do with the rewriting of URLs. If I remove the rewrite code, the content template gets applied fine (albeit, I had to go to the professional post type page, and re-save the post type for it to work).

However, once I add the rewrite code back in, I face the same 404 error again. Interestingly, the default WordPress URL hidden link always works and shows the content template, regardless of the rewrite rule. Do you know what the issue might be? happy to give admin access if required.

Warm regards,
Rithvik

#2530747

Hey Waqar,

Any update on this? Please let me know soon.

Warm regards,
Rithvik

#2530837

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Rithvik,

Thank you for waiting as we had a busy forum queue after the holidays.

I've performed some tests on my website with the exact same code but couldn't reproduce this issue. The rewrite rule and the post link replacement for the term's slug worked as expected and the assigned content template also kept on showing.

This suggests that something other than this code is also involved in your website. Have you checked for any other custom code or third-party plugin conflict?

In case, the issue still persists, with only Toolset plugins and a default theme like Twenty Twenty-One, you're welcome to share temporary admin login details, in reply to this message.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2531969

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

I've noticed that on your website, you've included that custom code through a custom plugin and not through the theme. The order of execution code can make a difference in this case.

Can you please test a slightly different code where the 'resources/' part of the URL is appended through the function attached to the 'post_type_link' filter instead of the 'register_post_type_args' filter?
( this works on my test website )


function tssupp_custom_rewrite($args, $post_type) {
    $slug = 'professional'; 
    if ($post_type == $slug) {
        $args['rewrite'] = array('slug' => '/%profession%', 'with_front' => 0);
    }
    return $args;
}
add_filter('register_post_type_args', 'tssupp_custom_rewrite', 10, 2);
 
function profession_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%profession%')) {
        $profession_type_term = get_the_terms($post->ID, 'profession');
        if (!empty($profession_type_term))
            $post_link = str_replace('%profession%', 'resources/'.array_pop($profession_type_term)->
            slug, $post_link);
        else
            $post_link = str_replace('%profession%', 'resources/uncategorized', $post_link);
    }
    return $post_link;
}
add_filter('post_type_link', 'profession_permalink_structure', 10, 4);

In case it still doesn't work, be sure to test with a different priority value in place of the default '10' ( ref: https://developer.wordpress.org/reference/functions/add_filter/ ) and also see if moving the code into the theme's 'functions.php' file has any effect.

#2532593

Hey Waqar,

Thank you for these suggestions! I have tried them all, updating the code with the one you provided, changed the priority to be lower than and higher than 10, and moving the code to the child theme's functions.php file. However, I am still facing the same issue. I really feel at a loss for what it could be. Have I perhaps misconfigured some setting for the post type or content template? What else can I test out?

Warm regards,
Rithvik

#2534339

I've also tried removing the child theme and adding the code directly to the primary theme's functions.php file. Still same result. I've removed the code for now as it was causing issues while giving the client a walkthrough of the site. Let me know if you want me to add it back in for testing purposes.

Warm regards,
Rithvik

#2540563

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting as I continued testing on my test website, as well as on your staging website.

I've added the following code, in the "functions.php" file of your website's child theme and flushed/resaved the permalinks settings and it is working as expected.


function tssupp_custom_rewrite($args, $post_type) {
    $slug = 'professional'; 
    if ($post_type == $slug) {
        $args['rewrite'] = array('slug' => 'resources/%profession%', 'with_front' => 0);
    }
    return $args;
}
add_filter('register_post_type_args', 'tssupp_custom_rewrite', 10, 2);
 
function profession_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%profession%')) {
        $profession_type_term = get_the_terms($post->ID, 'profession');
        if (!empty($profession_type_term)) {
            $post_link = str_replace('%profession%', array_pop($profession_type_term)->slug, $post_link);
        } else {
            $post_link = str_replace('%profession%', 'uncategorized', $post_link);
        }
    }
    return $post_link;
}
add_filter('post_type_link', 'profession_permalink_structure', 10, 4);

You'll also see that the assigned content template "Professional Single" is also properly being used for these single post pages.

Please feel free to test and let me know if you need any further assistance.

#2540867
Screenshot 2023-01-25 at 3.21.03 AM.png

Hey Waqar,

Thanks for getting back to me on this. Your code didn't work for me earlier because I had set the 'profession' taxonomy slug to be replaced with 'resources' instead of the default which is 'profession'. You should be able to reproduce this issue now if you try doing the same.

I wanted to set things up this way so that from an single professional's page, if someone removed the slug of the post title from the URL, they would be able to go back to the relevant 'profession' archive.

Please let me know if you are able to reproduce the issue I am facing by setting the taxonomy slug to be replaced with 'resources'.

Warm regards,
Rithvik

#2541787

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Your staging website is no longer accessible, but the same code continues to work, even if I change the slug of the taxonomy to 'resources'.

If the staging website has been removed, does it mean you managed to get it working?

#2542463

Hey Waqar,

I had to move to a different production server, hence the interruption. The code still doesn't work, but we had to push the website to live.

I've recreated the staging environment with our code here hidden link , you'll notice that none of the individual posts seem to have the template applied. The WordPress login is still the same, happy to provide FTP access if needed to this new server.

You've already tried to help us fix this a few times, so I leave it up to you if you'd like to continue troubleshooting or close this ticket. Thanks a lot for the help!

Warm regards,
Rithvik

#2542465

The staging site exists now only for troubleshooting this issue, so please feel free to disable plugins, change themes, or make any other drastic edits. I have tried both of those things, but they don't seem to help. Perhaps I don't know how to flush the permalinks cache properly.

Warm regards,

#2544091

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing the staging website again.

I made the following two changes and the code is working on this staging website too:

1. I replaced your old code from the theme's "functions.php" file with the one from my last message:


function tssupp_custom_rewrite($args, $post_type) {
    $slug = 'professional'; 
    if ($post_type == $slug) {
        $args['rewrite'] = array('slug' => 'resources/%profession%', 'with_front' => 0);
    }
    return $args;
}
add_filter('register_post_type_args', 'tssupp_custom_rewrite', 10, 2);
  
function profession_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%profession%')) {
        $profession_type_term = get_the_terms($post->ID, 'profession');
        if (!empty($profession_type_term)) {
            $post_link = str_replace('%profession%', array_pop($profession_type_term)->slug, $post_link);
        } else {
            $post_link = str_replace('%profession%', 'uncategorized', $post_link);
        }
    }
    return $post_link;
}
add_filter('post_type_link', 'profession_permalink_structure', 10, 4);

2. In the "Professions" taxonomy's settings, I enabled the option:
Allow permalinks to be prepended with front base

After that, I re-saved the permalink settings at WP Admin -> Settings -> Permalinks.

#2546471

Hey Waqar,

Thanks for taking a look again, this time it all seems to be working properly! Only thing that I hadn't tried was to enable permalinks to be prepended from front base. That seems to have fixed the issue.

Thanks a lot for the help!

Warm regards,

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.