Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #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=1

    ss4
    ss3
    ss2
    ss1

    #1412376

    Hi NicomIT,

    Do you use a custom 404 page? if yes, can you try to disable it and see if it helps.

    Best regards,
    Nikko

    #1412407

    Hi Nikko,

    Sorry, it didn’t help me, once disabling custom 404 page it is going to default page not found situation.

    #1412476

    Hi 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,
    Nikko

    #1412508

    Hi Nikko,

    Thanks for your response.
    i have also tried this code with the help of ChatGPT, with this code i m able to put agent_id after the post name and it is showing correctly in the permalink in the admin but when i m trying to open the link in url it is showing Page not found.

    url2
    url

    #1412654

    Hi 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,
    Nikko

    #1412683

    Hi 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, 2 months ago by NicomIT.
    #1412764

    Hi,

    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,
    Rikard

    #1412928

    Hi Rikard,

    My issue has been solved, you can close this ticket.
    Thanks to you and Nikko.

    #1412933

    Hi,

    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

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘custom post type permalink change done, showing page not found’ is closed to new replies.