WordPress.org

Ready to get started?Download WordPress

Forums

use taxonomy slug in custom posttype permalink (4 posts)

  1. thunderbug
    Member
    Posted 1 year ago #

    Hi there,

    I really need you help. :(

    I registered a custom post type called "guidepage" and a custom taxonomy called "guide-type" for this post type.

    No I want the permalink of guidepage to be like "/guides/%taxonomy-slug%/%postname%

    Here is my current code but the permalink still looks like this: http://d.pr/i/EPp4

    add_action('init', 'zug_initStuff' );
    function zug_initStuff() {
    	zug_createGuideTaxonomy();
    	zug_createGuidePostType();
    }
    
    function zug_createGuidePostType() {
      $labels = array(
        'name' => 'Guide Pages',
        'singular_name' => 'Guide Page',
        'add_new' => 'Add New Guide Page',
        'add_new_item' => 'Add New Guide Page',
        'edit_item' => 'Edit Guide Page',
        'new_item' => 'New Guide Page',
        'all_items' => 'All Guide Pages',
        'view_item' => 'View Guide Page',
        'search_items' => 'Search Guide Pages',
        'not_found' =>  'No guide pages found',
        'not_found_in_trash' => 'No guide pages found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Guides'
      );
    
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
    	'_builtin' => false,
        'show_in_menu' => true,
        'menu_position' => 25,
        'query_var' => true,
        'rewrite' => array(
    						'slug' => 'guides/%guidetype%',
    						'with_front' => false,
    						'hierarchical' => true
    					),
        'capability_type' => 'page',
        'has_archive' => true,
        'hierarchical' => false
      ); 
    
      register_post_type( 'guidepage', $args );
      global $wp_rewrite;
    	$wp_rewrite->flush_rules();
    }

    The following lines are just for testing. I'll remove them later /put them in the activation hook.

    global $wp_rewrite;
    $wp_rewrite->flush_rules();

    Thanks for your help!

  2. bcworkz
    Member
    Posted 1 year ago #

    You can use a rewrite rule so the permalink you desire will work, but when you generate the permalink using something like the_permalink() in templates, the original style permalink will result. You would need to hook 'the_permalink' filter to have permalinks generated in the form you want.

  3. thunderbug
    Member
    Posted 1 year ago #

    Yeah but why doesn't my solution work? I don't want to use that complicate way if wordpress offeres me the "rewrite" argument.

  4. bcworkz
    Member
    Posted 1 year ago #

    You can write pages of rewrites and it will not alter how the_permalink() generates URLs. One is for input, the other for output, they do not interact so you must address both sides on your own.

    As for why simply providing the rules in the rewrite arguments for CPTs an taxonomies does not work, I'm not sure, seems like it should. I do know that requesting URLs as you desire results in a query that in part includes WHERE 1=1 AND 1=0 which will always result in a 404, as it obviously will never find matching records. I've no idea where this 1=0 crap comes from though, I did spend some time searching, but no luck. I've no idea why this 1=1 construct is even generated, it makes no sense.

Topic Closed

This topic has been closed to new replies.

About this Topic