WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Types Permalinks (18 posts)

  1. Burke Ingraffia
    Member
    Posted 4 years ago #

    I am having a similar problem to http://wordpress.org/support/topic/custom-post-types-permalinks-404

    I have a custom post type called "video." All was working fine with my custom post rewrites when I had it like this:

    In my theme's functions.php register_post_type arguments I just had:

    'rewrite' => true,

    and in my permalinks I had it set up as /0/%postname%/

    Everything was working fine with regular posts coming out like: sitename.com/0/post-name

    and videos coming out like sitename.com/0/video/post-name

    I was given a request by my supervisor to remove the "/0/" and the "/video/" from all URL's so what I did was go back into my themes functions.php and replaced

    'rewrite' => true, with

    'rewrite' => array( 'slug' => '', 'with_front' => true),

    and then in my permalinks I replaced "/0/%postname%/" with just "/%postname%/".

    Now the regular posts are coming out as desired but the custom posts (videos) are all coming up 404.

    ---------------------------------------

    One other thing: If I just set the default in the permalink structure, the videos show up fine as:

    sitename.com/?video=post-name

    ---------------------------------------

    Here's something else -

    If I change

    'rewrite' => array( 'slug' => '', 'with_front' => true), to

    'rewrite' => array( 'slug' => '', 'with_front' => false),

    and then in my permalinks I replaced "/%postname%/" with "/blog/%postname%/"

    Then the custom video posts show up

    as sitename.com/post-name (as desired)

    but the regular posts then become 404.

    Any thoughts out there on this puzzle?

  2. Simon Fransson
    Member
    Posted 4 years ago #

    I just had the same problem on my page with two custom post types and ~10 custom taxonomies. Either I could access the custom post types/taxonomies but not the standard pages (although posts were okay).

    I have seen a recommendation somewhere on not using /%postname%/ as permalink structure, but as I was using /%year%/%monthnum%/%postname%/ I didn't think that would affect me.

    As I messed around with my register_post_type arguments I tried changing the rewrite setting from true into 'rewrite' => array( 'slug' => 'film', 'with_front' => false )

    That didn't change anything at first, but it got me thinking I should try adding a url base, so I changed my permalink structure into /foobar/%year%/%monthnum%/%postname%/. This solved my problem, pages started to show up as well as custom post types (which I could access at /film/king-kong/ as intended).

    But I didn't want the url base in all my URLs, for example I would like to access pages at /%postname%/. To accomplish this I changed the permlink structure once again into /%category%/%year%/%monthnum%/%postname%/ which seems to be working out fine. My permalinks now looks like this:

    Pages: /page-name/
    Blog posts: /blog/2010/08/post-title/
    Custom post types: /post-type/post-title/
    Custom taxonomies: /taxonomy-slug/tag-slug/

    It seems that once you add custom post types you might get rewrite collisions between custom and built in types, depending on your permalink settings.

  3. michael
    Member
    Posted 3 years ago #

    I'm having the same exact issues as you guys but I don't have the option of changing the permalink structure. I need a more general solution since I'm building a free theme:

    my code:

    add_action('init', 'my_custom_post_type_init');
    function my_custom_post_type_init()
    {
      $labels = array(
        'name' => _x('Roundabouts', 'post type general name'),
        'singular_name' => _x('Roundabout', 'post type singular name'),
        'add_new' => _x('Add New', 'Roundabout'),
        'add_new_item' => __('Add New Roundabout'),
        'edit_item' => __('Edit Roundabout'),
        'new_item' => __('New Roundabout'),
        'view_item' => __('View Roundabout'),
        'search_items' => __('Search Roundabouts'),
        'not_found' =>  __('No Roundabouts found'),
        'not_found_in_trash' => __('No Roundabouts found in Trash'),
        'parent_item_colon' => ''
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
    	'rewrite' => true,
    	'taxonomies' => array( 'post_tag', 'category '),
        'supports' => array('title','editor','author','thumbnail','excerpt','comments', 'thumbnail')
      );
      register_post_type('roundabouts',$args);
    
    }
    ?>

    as you can see - I'm using the rewrite param and hooking the default taxonomies with it.

    I also found this comment with one of the readers of Justin Tadlock: http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress#comment-212586
    but am not sure how to implement.

    Please help.

  4. Devin Price
    Member
    Posted 3 years ago #

    I have a theme in the repo that handles all this:
    http://wordpress.org/extend/themes/portfolio-theme

    I basically used Jake Goldman's plugin as a drop in:
    http://wordpress.org/extend/plugins/simple-custom-post-type-archives/

  5. flick
    Member
    Posted 3 years ago #

    Thanks for this thread! :) I have been puzzling over this for days, and still can't get it working. Going by all the wonderful tutorials online, where people got it working just right,all I could think was that I must have done something wrong.

    I also have /%postname%/ as my permalink setting (have done so since forever) and this how I'd like the posts to be displayed.

    If I have permalinks disabled, so ?typename=postname is shown in the URL, then the custom post type displays perfectly.

    However, once I enable custom post type permalinks, I receive 404 errors, where the url is now /typename/postname.

    I'm still confused so any help is appreciated :)

  6. wpAdm
    Member
    Posted 3 years ago #

    add_action('init', 'my_rewrite');
    function my_rewrite() {
        global $wp_rewrite;
        $wp_rewrite->add_permastruct('typename', 'typename/%year%/%postname%/', true, 1);
        add_rewrite_rule('typename/([0-9]{4})/(.+)/?$', 'index.php?typename=$matches[2]', 'top');
        $wp_rewrite->flush_rules(); // !!!
    }

    $wp_rewrite->flush_rules(); // !!!

  7. digimat1
    Member
    Posted 3 years ago #

    @micheal can you share your entire function.php file? I am having a similar problem but with multisite, would be greatly appreciated.

  8. michael
    Member
    Posted 3 years ago #

    @digimat1 - sorry for the late response. i'm still experiencing the error when the permalink structure is %postname%. it works fine with all the other structures - as long as you "resave" the permalink settings.

    <?php
    add_action('init', 'my_custom_post_type_init');
    function my_custom_post_type_init()
    {
      $labels = array(
        'name' => _x('Roundabouts', 'post type general name'),
        'singular_name' => _x('Roundabout', 'post type singular name'),
        'add_new' => _x('Add New', 'Roundabout'),
        'add_new_item' => __('Add New Roundabout'),
        'edit_item' => __('Edit Roundabout'),
        'new_item' => __('New Roundabout'),
        'view_item' => __('View Roundabout'),
        'search_items' => __('Search Roundabouts'),
        'not_found' =>  __('No Roundabouts found'),
        'not_found_in_trash' => __('No Roundabouts found in Trash'),
        'parent_item_colon' => ''
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 5,
        'menu_icon' => get_stylesheet_directory_uri() . '/images/arrow-circle-225.png',
        'rewrite' => array('slug' => 'roundabouts', 'with_front' => false),
        'taxonomies' => array( 'post_tag', 'category '),
        'supports' => array('title','editor','author','thumbnail','excerpt','comments', 'thumbnail')
      );
      register_post_type('roundabouts',$args);
    }
    ?>
  9. igeekout
    Member
    Posted 3 years ago #

    I think I posted something similar. So I understand I am having the same issue let me state my problem....

    If my posts are being pulled into a page named portfolio and when I click to view the full post, how do I get the url to be pagename/postname.

    Is this the same issue?

    Thanks

  10. jimmerd
    Member
    Posted 3 years ago #

    wpAdm's solution was PERFECT for me.
    When I changed the permalink structure, I was fine EXCEPT for custom taxonomies, which led to 404s.
    I just put wpAdm's code in functions.php and all is fine now.

  11. michael
    Member
    Posted 3 years ago #

    Thank you wpAdm. That worked for my theme. In case you want to download: http://fearlessflyer.com/2010/09/free-kick-ass-theme-built-for-wp-3-rotary/

  12. Taylor Baybutt
    Member
    Posted 3 years ago #

    can we have a little bit more instruction on how to use that code that wpAdm pasted? Are you supposed to put your cutom post type name in place of 'typename'? what does each parameter of the functions do? I am having trouble getting it to work.

  13. Taylor Baybutt
    Member
    Posted 3 years ago #

    Well I looked at Michael's code in functions.php of the theme he posted and there was no manipulation to that code at all. I have pasted that code in and even changed my custom_post_type to be registered in functions.php rather than a plugin to handle it all. It is still not working. I have manually flushed my permalinks a bunch of times. I'm at a loss.

  14. michael
    Member
    Posted 3 years ago #

    i actually skipped wpAdm's solution for the default rewrite param (within my_custom_post_type_init) which works with most types of permalink structure

  15. Taylor Baybutt
    Member
    Posted 3 years ago #

    I got it working. Using More Types plugin to create my custom post types, I had to disable permalink feature and instead, create the permalinks using the standard register post type function in functions.php

  16. alanchrishughes
    Member
    Posted 3 years ago #

    All I want to do is add a folder name (music) before all the different post types and taxonomies that fall into that section, like.....

    mysite.com/music/bands/post-name
    mystie.com/music/shows/post-name
    mysite.com/music/venues/post-name

    With a taxonomy....
    mysite.com/music/venues/out-door/post-name

  17. slymongoose
    Member
    Posted 3 years ago #

    i was also having trouble with the 404 error messages with the permalinks turned on, and i had been running in circles for the past several hours. the thing that finally fixed it for me was, when i registered my post type, i had hierarchical set to true ... changing the line to
    'hierarchical' => false,
    (and then visiting the permalinks page to flush the settings)
    fixed the problem for me. hope this helps someone

  18. rhysbwaller
    Member
    Posted 3 years ago #

    Im having a similar problem but none of the solutions have fixed it. There are no clashes with any other objects but still the 404s. Man this should be simple and its just head banging stuff

Topic Closed

This topic has been closed to new replies.

About this Topic