Forums

[resolved] Custom Post slug and nav_menu_item slug collisions (3 posts)

  1. loic.etibe
    Member
    Posted 8 months ago #

    Hello :-)

    I'm developping some plugin with custom post types and I'm now running into a weird problem.

    I have a custom type called, let's say, "My Custom Post". I want a list of those to be reachable at a convenient URL, so I set up my rewrite slug like this :

    'rewrite' => array(
                  'slug' => 'my-custom-posts',
                )

    Good. Now I want to use a custom Menu to handle all of this, so I start building it. Now I want an item called "My Custom Posts", which slug will be "my-custom-posts" and pointing to /my-custom-posts/ (my plugin handles this and provides a listing page if pagename is found).

    Now the problem : if I click on my Menu's item, I'm redirected to my default Permalink rewrite rule, pointing to the nav_menu_item's ID, not having even the chance to do anything with it.

    I tried a few tricks, but nothing seems to work . Except changing my Custom Post slug. The best solution would be to tell WordPress to ignore nav_menu_item post type, as it will always point to a "Post not found page", but I couldn't manage to do that.

  2. MrZed
    Member
    Posted 8 months ago #

    Hello,

    I have the same problem, and it's really annoying ...

    I tried to find the function which redirect, to deactivate it or modify it, but I did not find it.
    I hope there is another solution, without affecting the WordPress sources.

  3. loic.etibe
    Member
    Posted 8 months ago #

    Hello back.

    I couldn't find any way to exclude the nav_menu_item's from the requests, so I tried the other way around : altering the post_name at insert time.

    So, here is a function to add to a 'wp_insert_post' action hook :

    add_action('wp_insert_post', 'nav_menu_items', 8);

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Important Notice : the wp_insert_post hook is actually called at the end of the insert/update function and I couldn't find a way to limit it to the inserts only. The overhead doesn't seem too high so I think it's OK. And it will solve the problem for all pre-existing menus.

    Enjoy :-)

Reply

You must log in to post.

About this Topic