Hi,
I'm trying to make a custom post archive/single URL prepended with a custom rewrite tag.
It works perfectly for the single page but not for archive as attached.
This link work:
site/clinic_name/visits/visit-one/
But this is just redirecting back to the homepage:
site/clinic_name/visits/
Another Issue related to this is when I disable query_var I still can get it to work through the following link:
site/?post_type=visits
Thanks.
Hello,
In your screenshot, there is a text "/%hospital_name%/" in option has_archive, where does it come from? Are you using custom codes to resolve this text?
Please describe detail steps to duplicate the same problem.
Types plugin is using WordPress funciton register_post_type() to create new post type, according to wordpress document:
https://codex.wordpress.org/Function_Reference/register_post_type#has_archive
It supports only static text.
Hi Luo
Yes i use a custom code to register and resolve this rewrite tag:
add_action('init', 'tdd_add_rewrite_rules2');
function tdd_add_rewrite_rules2(){
// Register custom rewrite rules
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%hospital_name%', '([^/]+)', '');
add_rewrite_rule(
'^%hospital_name%/visits/?$',
'index.php?post_type=visits',
'top'
);
}
add_filter('post_type_link', 'tdd_permalinks2', 10, 3);
function tdd_permalinks2($permalink, $post, $leavename){
$no_data = get_the_author_meta('ID');;
$post_id = $post->ID;
if($post->post_type != 'visits' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) return $permalink;
$hospital_id = get_user_meta(wp_get_current_user()->ID, 'wpcf-hospital', true);
$hospital_name = get_post($hospital_id)->post_name;
$hospital_name = sanitize_title($hospital_name);
if(!$hospital_name) { $hospital_name = $no_data; }
$permalink = str_replace('%hospital_name%', $hospital_name, $permalink);
return $permalink;
}
It works perfectly in Single rewrite but not archive.
Do you mean that it is allowed only in single but not for the archive?
Thanks.
There isn't such kind of built-in feature within Types plugin, and I have tried your custom codes in my localhost, it does not work for single "visits" post too, I get 404 error, and if I am right, there are some missing steps in your description, for example, it needs
- a custom post type "visits",
- a custom user field "hospital",
but even they are created, I still get the 404 error in single "visits" post.
According to our support policy, it is out the range of Toolset support, we don't provide the custom codes support
https://toolset.com/toolset-support-policy/
You might consider our Toolset Contractors:
https://toolset.com/contractors/
It is okay, i don't know what is missed but it really works for singles.
Yes i have cpt for hospital and visits and i have a custom field for every user hold the hospital id.
So Types only support static values as slugs for single and archive?
Thanks for your support.
Yes, you are right, Types plugin supports static values in the rewrite rule of single and archive, in your case it needs custom codes. But that will conduct some unexpected result, for example in my localhost with a fresh wordpress installation, it is 404 error.