• Resolved laz0rama

    (@laz0rama)


    this plugin appears to do exactly what i want. except…

    i am simply trying to get a custom post type to show up on the front end of the site with a pretty url. i have all the basics (.htaccess, permalink settings, etc). i am not new to wordpress, but this is the first time i am using custom post types. please bear with me.

    i installed a brand new copy of wordpress (4.9.4). no plugins enabled. i have my permalink settings set to use custom structure of “/%category%/%postname%/”. i created 50 categories, one for each state in the u.s. the slugs for those are the state name, so alaska permalink is https://mysite.com/alaska. all good.

    i created a simple custom post type (city). i will paste that code below so you can see.

    i create a post of type city (say, “anchorage”), with the slug “anchorage-ak”. by default, when i edit the post in wp-admin, the permalink for the post is:
    https://mysite.com/city/anchorage-ak.

    i need the url to be: https://mysite.com/alaska/anchorage-ak

    so, i added a rewrite option to the plugin’s call to register_post_type() like so:

    ‘rewrite’ => array(‘slug’ => ‘%category%’),

    when i edit the post now, there is literally NO PERMALINK that shows below the post title. the label “Permalink:” is there, followed by the “edit” button.

    so, i installed this plugin and set the permastructure for city posts to “%category%/%postname%”. when i edit the post now, i see the permalink i want:
    https://mysite.com/alaska/anchorage-ak

    but when i try to access that url i get a 404.

    if i remove the
    ‘rewrite’ => array(‘slug’ => ‘%category%’),

    from the register_post_type() call, the permalink goes back to
    https://mysite.com/city/anchorage-ak.

    and if i disable the plugin, when i edit the post there is no permalink.

    so, why am i getting a 404 on a permalink that wordpress is displaying to me on the edit form?

    thanks VERY much in advance.


    here is the code for the plugin:

    add_action( ‘init’, ‘create_post_types’ );

    function create_post_types()
    {
    register_post_type( ‘city’,
    array(
    ‘labels’ => array(
    ‘name’ => __( ‘Cities’ ),
    ‘singular_name’ => __( ‘City’ )
    ),
    ‘description’ => __(‘City Posts represent a single city within a given state’),
    ‘public’ => true,
    ‘has_archive’ => false,
    ‘rewrite’ => array(‘slug’ => ‘%category%’),
    ‘menu_position’ => 5,
    ‘hierarchical’ => false,
    ‘supports’ => array(‘title’, ‘editor’, ‘author’, ‘trackbacks’, ‘custom_fields’, ‘comments’),
    ‘taxonomies’ => array(‘category’, ‘post_tag’),
    )
    );
    flush_rewrite_rules();
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Maciej Bis

    (@mbis)

    Hi,

    the rewrite tags such as %category% are not supported when added via ‘rewrite’ parameter with register_post_type() function, but they are supported by my plugin.

    You should go to “Permalink Manager -> Permastructures” settings and change the permalink format for “city” post type to:
    %category%/%city%

    After that you will need to regenerate the old city permalinks in “Permalink Manager -> Tools -> Regenerate/reset” section.

    PS. You should rather set-up a custom taxonomy for the states instead of using the native one (that should be used by posts only) – this may generate some issues with another 3rd party plugins in the future.

    If you decide to register a new custom taxonomy (eg. “state”) with register_taxonomy() your permastructure settings should be changed to:
    %state%/%city%

    Regards,
    Maciej

    Thread Starter laz0rama

    (@laz0rama)

    thanks very much for your quick response. i will definitely try your suggestion, and i’ll post back here with results.

    Plugin Author Maciej Bis

    (@mbis)

    You will also need to change rewrite parameter to: “true” to avoid the problems with canonical redirects. If you still encounter any problem with them, please send me URL of your website to contact /at/ maciejbis.net.

    add_action( ‘init’, ‘create_post_types’ );
    
    function create_post_types()
    {
    register_post_type( ‘city’,
    array(
    ‘labels’ => array(
    ‘name’ => __( ‘Cities’ ),
    ‘singular_name’ => __( ‘City’ )
    ),
    ‘description’ => __(‘City Posts represent a single city within a given state’),
    ‘public’ => true,
    ‘has_archive’ => false,
    ‘rewrite’ => true,
    ‘menu_position’ => 5,
    ‘hierarchical’ => false,
    ‘supports’ => array(‘title’, ‘editor’, ‘author’, ‘trackbacks’, ‘custom_fields’, ‘comments’),
    ‘taxonomies’ => array(‘category’, ‘post_tag’),
    )
    );
    }
    • This reply was modified 6 years, 11 months ago by Maciej Bis.
    Thread Starter laz0rama

    (@laz0rama)

    i really appreciate your help. i will hopefully get to try it out today.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘issues with cpt permalinks and 404s’ is closed to new replies.