CustomPress Permalinks not working as expected.

Okay, so I have created a custom post type with the name "article" and then my permalink is set as /%category%/%post-name%/ So basically, I created an "article" using custompress, and added 12 articles, these 12 articles has its own categories, however after publish, the breadcrumbs/permalinks shows http://www.mysite.com/articles/aba-therapy-behavior (example) I want it to be something like this mysite.com/articles/behavior/aba-therapy so adding "behaviour" <-- the category.

  • Ivan

    Hi Alex !

    It seems it's a standard behavior of WP custom post type. You can change it - please, create MU plugin ( for example, create {wordpress_folder}/wp-content/mu-plugins/wpmu_custom_press_permalinks.php file via FTP ) with the following code

    <?php
    function wpmu_custom_press_permalinks( $post_link, $post, $leavename, $sample ) {
    	if ( !empty( $post->post_type ) && 'article' === $post->post_type && strpos( $post_link, '/article/' ) ) {
    		$cats = get_the_category($post->ID);
    		if ( $cats ) {
    			$cats = wp_list_sort( $cats, array(
    				'term_id' => 'ASC',
    			) );
    
    			$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
    
    			$category_object = get_term( $category_object, 'category' );
    			$category = $category_object->slug;
    		}
    		if ( empty($category) ) {
    			$default_category = get_term( get_option( 'default_category' ), 'category' );
    			if ( $default_category && ! is_wp_error( $default_category ) ) {
    				$category = $default_category->slug;
    			}
    		}
    		$post_link = str_replace( '/article/', '/article/' . $category . '/', $post_link );
    	}
    	return $post_link;
    }
    
    add_filter( 'post_type_link', 'wpmu_custom_press_permalinks', 10, 4 );
    
    function wpmu_custom_press_add_rule($wp_rewrite) {
        $rules = array();
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => false,
        ) );
    
        $post_type = 'article';
        foreach ( $terms as $term ) {
            $rules['article/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&category=$matches[1]&name=$matches[1]';
        }
        // merge with global rules
        $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    }
    add_filter('generate_rewrite_rules', 'wpmu_custom_press_add_rule');

    If you want to apply it for all custom post types created Custom Press - you can try this code instead of the previous

    <?php
    function wpmu_custom_press_permalinks( $post_link, $post, $leavename, $sample ) {
    	$post_types = get_option( 'ct_custom_post_types' );
    	$post_types = is_array( $post_types ) ? array_keys( $post_types ) : array();
    	if ( !empty( $post_types ) && !empty( $post->post_type ) && in_array( $post->post_type, $post_types ) ) {
    		$cats = get_the_category($post->ID);
    		if ( $cats ) {
    			$cats = wp_list_sort( $cats, array(
    				'term_id' => 'ASC',
    			) );
    
    			$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
    
    			$category_object = get_term( $category_object, 'category' );
    			$category = $category_object->slug;
    		}
    		if ( empty($category) ) {
    			$default_category = get_term( get_option( 'default_category' ), 'category' );
    			if ( $default_category && ! is_wp_error( $default_category ) ) {
    				$category = $default_category->slug;
    			}
    		}
    		$post_link = str_replace( '/' . $post->post_type . '/', '/' . $post->post_type . '/' . $category . '/', $post_link );
    	}
    	return $post_link;
    }
    
    add_filter( 'post_type_link', 'wpmu_custom_press_permalinks', 10, 4 );
    
    function wpmu_custom_press_add_rule($wp_rewrite) {
    	$post_types = get_option( 'ct_custom_post_types' );
    	$post_types = is_array( $post_types ) ? array_keys( $post_types ) : array();
        $rules = array();
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => false,
        ) );
    
    	foreach ( $post_types as $post_type ) {
    		foreach ( $terms as $term ) {
    			$rules[ $post_type . '/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&category=$matches[1]&name=$matches[1]';
    		}
    	}
        // merge with global rules
        $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    }
    add_filter('generate_rewrite_rules', 'wpmu_custom_press_add_rule');

    Best regards,
    Ivan.

  • Ivan

    Hi Alex !

    Yes, MU plugins work and for single sites and for multisites, like plugins, but you can't deactivate them via Admin area - only delete them from FTP.
    Are you knowledgeable with FTP? If yes - just create one file as I mentioned it before. If no - provide me with additional login credentials, I can do it.

    https://premium.wpmudev.org/contact/#i-have-a-different-question
    Please visit our private Contact page and complete the form with the following information:

    Subject: "Attn: Ivan Svyrskyi"

    In the Message box, please provide the following:

    - link back to this thread for reference
    - any other relevant urls
    - do you want to apply it only for 'article' post type or for all post types created CustomPress?

    - Admin login:
    Admin username
    Admin password
    Login url

    - FTP/SFTP credentials
    host
    username
    password
    (and port if required)

    Note: Don't send any credentials via this forum because it's public forum.

    Best regards,
    Ivan.

  • Ivan

    Hi Alex !

    You can change category slugs on this page {your_domain}/wp-admin/edit-tags.php?taxonomy=category&post_type=articles.

    Note: I tried to do for you but it's not possible because you already have the behavior slug for another category.

    So, you can either reassign all posts from behavior-article category to behavior or delete category with behavior slug and then rename behavior-article slug to behavior.

    Best regards,
    Ivan.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.