Sunday, July 17, 2022
HomeWordPress Developmentfilters - Customized youngster web page permalink

filters – Customized youngster web page permalink


I am making an attempt to customise the permalink of some youngster pages.

I’ve this construction:
instance.com/story/story-name-slug/chapter-slug/page-slug

And I am making an attempt to reworking that construction into this:
instance.com/part/page-slug

But additionally I wish to preserve the opposite hyperlinks energetic, so in the long run I ought to have the next:
instance.com/story -> archive of all story;

instance.com/story/story-name-slug -> record of all chapters;

instance.com/part/page-slug -> web page with personalized url.

Needless to say instance.com/story/story-name-slug/chapter-slug isn’t a web page that may be visited by customers. It is solely used to archive the hierarchical relation between pages.

In the meanwhile I bought the url construction working with the next code in capabilities.php:

add_filter('page_link', 'custom_permalink', 10, 3);
add_filter('page_type_link', 'custom_permalink', 10, 3);

operate custom_permalink($permalink, $post_id) {

    $publish = get_post($post_id);

    $post_name = $post->post_name;

    $id_chapter = $post->post_parent;
    $id_story = wp_get_post_parent_id($id_chapter);
    $id_stories = wp_get_post_parent_id($id_story);

    if ($id_chapter !== 0 && $id_story !== 0 && $id_stories ) {
        // the static base url is only for testing
        $permalink = 'instance.com/part/' . $post_name ;
    } else {
        $permalink = $permalink;
    }

    return $permalink;
}

This works however not because it shoudl, I get the appropriate URLs on the appropriate pages however once I go to these pages I get a 404 Error not discovered.

After some digging within the sql queris that get executed I feel that the issue is that WP is deciding on wp_posts.post_type="attachment" as an alternative of wp_posts.post_type="web page".

I feel {that a} rewrite rule is required someplace to specify within the sql question what to pick, however this goes over my present capabilities.

If it is wanted I can present all of the queries that get executed on the web page with and with out the filter that I added.

Thanks!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments