• Resolved peterorpete

    (@peterorpete)


    Hi,

    I’m trying to rewrite a custom post type’s slug so that its taxonomy term appears first. So for example:

    post type: case_studies
    taxonomy: work
    terms: commercial, domestic

    so i want the url to end up being: website.com/domestic/case_studies

    Why does the code below give me a 404? I can make it case_studies/domestic easy enough but not the other way around.

    Apologies if this is simple stuff, quite new to this aspect of wordpress.

    Pete

    add_action( 'init', 'cptui_register_my_cpts' );
    function cptui_register_my_cpts() {
    	$labels = array(
    		"name" => "Case Studies",
    		"singular_name" => "Case Study",
    		);
    
    	$args = array(
    		"labels" => $labels,
    		"description" => "",
    		"public" => true,
    		"show_ui" => true,
    		"has_archive" => true,
    		"show_in_menu" => true,
    		"exclude_from_search" => false,
    		"capability_type" => "post",
    		"hierarchical" => false,
    		"rewrite" => array( "slug" => "%work_type%/case_studies", "with_front" => false ),
    		"query_var" => true,
    		"supports" => array( "title", "thumbnail", "page-attributes" ),
    		"taxonomies" => array( "work" )
    	);
    	register_post_type( "case_studies", $args );
    
    // End of cptui_register_my_cpts()
    }
    
    add_action( 'init', 'cptui_register_my_taxes' );
    function cptui_register_my_taxes() {
    
    	$labels = array(
    		"name" => "Work Type",
    		"label" => "Work Type",
    		);
    
    	$args = array(
    		"labels" => $labels,
    		"hierarchical" => true,
    		"label" => "Work Type",
    		"show_ui" => true,
    		"query_var" => true,
    		"rewrite" => array( 'slug' => 'work_type'),
    		"show_admin_column" => true,
    	);
    	register_taxonomy( "work", array( "testimonial", "case_studies" ), $args );
    
    	unset($labels);
    	unset($args);
    
    // End cptui_register_my_taxes
    }
    
    add_filter('post_type_link', 'work_types_permalink_structure', 10, 4);
    function work_types_permalink_structure($post_link, $post, $leavename, $sample) {
        if (false !== strpos($post_link, '%work_type%')) {
            $work_types_type_term = get_the_terms($post->ID, 'work');
            if (!empty($work_types_type_term))
                $post_link = str_replace('%work_type%', array_pop($work_types_type_term)->
                slug, $post_link);
            else
                $post_link = str_replace('%work_type%', 'uncategorized', $post_link);
        }
        return $post_link;
    }
Viewing 1 replies (of 1 total)
  • Thread Starter peterorpete

    (@peterorpete)

    fixed, was a stupid error, called the taxonomy work instead of work_type. Works a treat now

    add_action( 'init', 'cptui_register_my_cpts' );
    function cptui_register_my_cpts() {
    	$labels = array(
    		"name" => "Case Studies",
    		"singular_name" => "Case Study",
    		);
    
    	$args = array(
    		"labels" => $labels,
    		"description" => "",
    		"public" => true,
    		"show_ui" => true,
    		"has_archive" => true,
    		"show_in_menu" => true,
    		"exclude_from_search" => false,
    		"capability_type" => "post",
    		"hierarchical" => false,
    		"rewrite" => array( "slug" => "%work_type%/case_studies", "with_front" => false ),
    		"query_var" => true,
    		"supports" => array( "title", "thumbnail", "page-attributes" ),
    		"taxonomies" => array( "work_type" )
    	);
    	register_post_type( "case_studies", $args );
    
    // End of cptui_register_my_cpts()
    }
    
    add_action( 'init', 'cptui_register_my_taxes' );
    function cptui_register_my_taxes() {
    
    	$labels = array(
    		"name" => "Work Type",
    		"label" => "Work Type",
    		);
    
    	$args = array(
    		"labels" => $labels,
    		"hierarchical" => true,
    		"label" => "Work Type",
    		"show_ui" => true,
    		"query_var" => true,
    		"rewrite" => array( 'slug' => 'work_type'),
    		"show_admin_column" => true,
    	);
    	register_taxonomy( "work_type", array( "testimonial", "case_studies" ), $args );
    
    	unset($labels);
    	unset($args);
    
    // End cptui_register_my_taxes
    }
    
    add_filter('post_type_link', 'work_types_permalink_structure', 10, 4);
    function work_types_permalink_structure($post_link, $post, $leavename, $sample) {
        if (false !== strpos($post_link, '%work_type%')) {
            $work_types_type_term = get_the_terms($post->ID, 'work_type');
            if (!empty($work_types_type_term))
                $post_link = str_replace('%work_type%', array_pop($work_types_type_term)->
                slug, $post_link);
            else
                $post_link = str_replace('%work_type%', 'uncategorized', $post_link);
        }
        return $post_link;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Rewriting a custom post type slug issues’ is closed to new replies.