• torbent

    (@torbentschechnegmailcom)


    Hello,
    I created a custom post type and a taxonomy. I want to use custom categories to group different items of my custom post type.

    However, I included the category in the permalink. That works, but when I change the category, the permalink changes as well, even when the post has been published.

    Is there a possibility to ensure, that the category in the permalink only changes if the post has not been made public yet?

    The code goes as follows:

    // Inspiration CPT
    if ( ! function_exists('register_custom_post_types') ) {
    
    	// Register Custom Post Type Kollektion
    	function register_custom_post_types() {
    
    		$labels = array(
    			'name'                  => _x( 'Inspirationen', 'Post Type General Name', 'genesis' ),
    			'singular_name'         => _x( 'Inspiration', 'Post Type Singular Name', 'genesis' ),
    			'menu_name'             => __( 'Inspiration', 'genesis' ),
    			'name_admin_bar'        => __( 'Inspiration', 'genesis' ),
    			'parent_item_colon'     => __( 'Parent Item:', 'genesis' ),
    			'all_items'             => __( 'Alle Eintr?ge', 'genesis' ),
    			'add_new_item'          => __( 'Neue Inspiration hinzufügen', 'genesis' ),
    			'add_new'               => __( 'Neu hinzufügen', 'genesis' ),
    			'new_item'              => __( 'Neu', 'genesis' ),
    			'edit_item'             => __( 'Editieren', 'genesis' ),
    			'update_item'           => __( 'Aktualisieren', 'genesis' ),
    			'view_item'             => __( 'Ansehen', 'genesis' ),
    			'search_items'          => __( 'Suche', 'genesis' ),
    			'not_found'             => __( 'Nicht gefunden', 'genesis' ),
    			'not_found_in_trash'    => __( 'Nicht im Papierkorb gefunden', 'genesis' ),
    			'items_list'            => __( 'Liste', 'genesis' ),
    			'items_list_navigation' => __( 'Liste Navigation', 'genesis' ),
    			'filter_items_list'     => __( 'Filter', 'genesis' ),
    		);
    		$args = array(
    			'label'                 => __( 'Inspiration', 'genesis' ),
    			'description'           => __( 'Inspirationen', 'genesis' ),
    			'labels'                => $labels,
    			'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', ),
    			'taxonomies'            => array( 'inspiration' ),
    			'hierarchical'          => true,
    			'public'                => true,
    			'show_ui'               => true,
    			'show_in_menu'          => true,
    			'menu_position'         => 5,
    			'show_in_admin_bar'     => true,
    			'show_in_nav_menus'     => true,
    			'can_export'            => true,
    			'has_archive'           => true,
    			'exclude_from_search'   => false,
    			'publicly_queryable'    => true,
    			'capability_type'       => 'page',
    			'rewrite'		=> array('slug' => 'inspiration/%inspirationen%','with_front' => false),
    		);
    		register_post_type( 'inspiration', $args );
    
    	}//function
    	add_action( 'init', 'register_custom_post_types');
    }
    
    // register two taxonomies to go with the post type
    function custom_register_taxonomy() {
    	// set up labels
    	$labels = array(
    		'name'              => 'Inspirationen Kategorien',
    		'singular_name'     => 'Inspiration Kategorie',
    		'search_items'      => 'Suche',
    		'all_items'         => 'Alle Kategorien',
    		'edit_item'         => 'Editieren',
    		'update_item'       => 'Aktualisieren',
    		'add_new_item'      => 'Neu hinzufügen',
    		'new_item_name'     => 'Neu hinzufügen',
    		'menu_name'         => 'Inspiration Kategorien'
    	);
    	// register taxonomy
    	register_taxonomy( 'inspirationen', 'inspiration', array(
    		'hierarchical' => true,
    		'labels' => $labels,
    		'query_var' => true,
    		'show_admin_column' => true,
    		'rewrite' => array(
    			'slug' => 'inspiration', // This controls the base slug that will display before each term
            ),
    	) );
    }
    add_action( 'init', 'custom_register_taxonomy' );
    
    /* Filter modifies the permaling */
    
    add_filter('post_link', 'category_permalink', 1, 3);
    add_filter('post_type_link', 'category_permalink', 1, 3);
    
    function category_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, '%inspirationen%') === FALSE) return $permalink;
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'inspirationen');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            	$taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = 'sonstiges';
    
        return str_replace('%inspirationen%', $taxonomy_slug, $permalink);
    }

  • The topic ‘Permalink – Custom Post Type – Freeze?’ is closed to new replies.