-
AuthorPosts
-
June 30, 2023 at 10:23 am #1412140
Hi Support Team,
I have custom post type “agentss” and i have change the permalink of this post type from site_url/agentss/post-name to site_url/agents/agent_id(post_meta_value)/post-name. Below is the code i have used in functions.php
add_action( ‘init’, ‘pubmonth_register_rewrite_tag’);
function pubmonth_register_rewrite_tag() {
add_rewrite_tag( ‘%agent_id%’, ‘([0-9]{5}’);
}add_filter(‘post_type_link’, ‘pub_term_permalink’, 10, 4);
function pub_term_permalink($permalink, $post, $leavename, $sample)
{
if ( false !== strpos( $permalink, ‘agents/%agent_id%’ ) ) {
//get the month of the publication
$agent_id = get_post_meta($post->ID, ‘agent_id’, true);
$rewritecode = array(
‘%agent_id%’,
$leavename? ” : ‘%postname%’,
);
$rewritereplace = array(
$agent_id,
$post->post_name
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}add_action( ‘init’, ‘pub_add_rewrite_rules’ );
function pub_add_rewrite_rules() {
add_rewrite_rule( ‘^([^/]*)/([0-9]{4})/([^/]+)?’, ‘agentss/index.php?pagename=$matches[2]’, ‘top’ );
}
then i saved the permalink. after this post link is showing correctly in the admin but wghen i open in front end it is throwing page-not-found/?avia_forced_reroute=1July 4, 2023 at 12:19 am #1412376Hi NicomIT,
Do you use a custom 404 page? if yes, can you try to disable it and see if it helps.
Best regards,
NikkoJuly 4, 2023 at 11:42 am #1412407Hi Nikko,
Sorry, it didn’t help me, once disabling custom 404 page it is going to default page not found situation.
July 5, 2023 at 12:56 am #1412476Hi NicomIT,
Can you give us temporary admin access? so we can try to check the settings and try to replicate the issue.
I tried to replicate it on my end however I think I need to change some settings in my cpt ui plugin since your code does not seem to have any effect or atleast a change in the permalink.
Just post the credentials in private content.Best regards,
NikkoJuly 5, 2023 at 1:06 pm #1412508July 7, 2023 at 4:23 am #1412654Hi NicomIT,
Thanks for giving us admin access.
I tried to check it and it seems to be working on the two latest agents (link in private content) but not on the others.
I also tried to add your code in functions.php of the twenty twenty theme and the results are the same.Best regards,
NikkoJuly 7, 2023 at 10:15 am #1412683Hi Nikko,
Thanks for your help and effort. it seems that i have written correct code but in the regex expression i put ^([^/]*)/([0-9]{5})/([^/]+)? this but when checking agent url I was checking agents with agent id of 4 digits so I was getting page not found error never checked agents urls with 5 digits agent id, when you mentioned the above urls then only it strike into my mind. Now that I have changed the regex expression it is working fine.
But never the less thanks for your help and effort to give valuable time in this to solve my problem.
Thanks a lot again and have nice weekend.- This reply was modified 1 year, 4 months ago by NicomIT.
July 8, 2023 at 8:17 pm #1412764Hi,
Great, I’m glad that Nikko could help you out. Please let us know if you should need any further help on the topic, or if we can close it.
Best regards,
RikardJuly 10, 2023 at 9:36 am #1412928Hi Rikard,
My issue has been solved, you can close this ticket.
Thanks to you and Nikko.July 10, 2023 at 10:09 am #1412933Hi,
Thanks for letting us know, I’ll close this thread for now then. Please open a new thread if you should have any further questions or problems.
Best regards,
Rikard -
AuthorPosts
- The topic ‘custom post type permalink change done, showing page not found’ is closed to new replies.