How to Create Custom Post Types and Permalinks in WordPress

Home » Snippets » How to Create Custom Post Types and Permalinks in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you using WordPress to create custom post types, but struggling with setting up the right permalinks for them? Custom post types allow you to organize and display content in a unique way, but getting the permalinks to work correctly can be a challenge. In this article, we will guide you through the process of creating custom post types and setting up custom permalinks that perfectly suit your needs. Whether you’re a beginner or an experienced WordPress user, this article will help you understand and implement the right permalink structure for your custom post types. Let’s dive in!

					function wpturbo_register_custom_post_type() {
    $args = array(
        'labels' => array(
            'name' => __( 'Custom Post Types', 'wpturbo' ),
            'singular_name' => __( 'Custom Post Type', 'wpturbo' ),
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'custom-post-type' ),
    );
    register_post_type( 'custom_post_type', $args );
}
add_action( 'init', 'wpturbo_register_custom_post_type' );

function wpturbo_update_permalink_structure() {
    global $wp_rewrite;
    
    // Get the current permalink structure
    $permalink_structure = get_option( 'permalink_structure' );
    
    // Check if the custom post type is registered
    if ( wpturbo_is_custom_post_type_registered() ) {
        // Add the custom post type slug to the permalink structure
        $permalink_structure = '/%year%/%monthnum%/%day%/%custom_post_type%/' . $permalink_structure;
    }
    
    // Update the permalink structure
    $wp_rewrite->set_permalink_structure( $permalink_structure );
    $wp_rewrite->flush_rules();
}
add_action( 'init', 'wpturbo_update_permalink_structure' );

function wpturbo_is_custom_post_type_registered() {
    $custom_post_type = 'custom_post_type';
    $args = get_post_type_object( $custom_post_type );
    return (bool) $args;
}
				

The code snippet provided is for registering a custom post type in WordPress and updating the permalink structure to include the custom post type slug.

Let’s start with the first function, wpturbo_register_custom_post_type(). This function is responsible for registering a new custom post type. It takes an array of arguments that define the labels, visibility, archive settings, and the rewrite rules for the custom post type.

In the $args array, we specify the labels for the custom post type using the name and singular_name keys. These labels will be used to display the custom post type in the WordPress admin interface.

The public key is set to true, which allows the custom post type to be displayed in the admin menu and to be publicly queryable on the front end.

The has_archive key is set to true, which enables the archive page for the custom post type.

The rewrite key is set to an array with the slug key set to ‘custom-post-type’. This defines the URL slug that will be used for the custom post type. You can change the slug to any desired value.

After defining the arguments, we use the register_post_type() function to register the custom post type with the provided arguments.

Next, we have the wpturbo_update_permalink_structure() function. This function is responsible for updating the permalink structure to include the custom post type slug.

First, we get the current permalink structure using the get_option() function and store it in the $permalink_structure variable.

Next, we check if the custom post type is registered by calling the wpturbo_is_custom_post_type_registered() function. This function checks if the custom post type exists and returns a boolean value.

If the custom post type is registered, we add the custom post type slug to the permalink structure. In this example, we have added %custom_post_type% to the permalink structure, which will be replaced with the custom post type slug defined in the registration function.

Finally, we use the $wp_rewrite global variable to set the permalink structure using the set_permalink_structure() method, passing in the modified $permalink_structure. We then call the flush_rules() method to flush the rewrite rules and update the permalink structure.

The last function, wpturbo_is_custom_post_type_registered(), is a helper function that checks if the custom post type is registered. It takes the custom post type slug as a parameter and uses the get_post_type_object() function to retrieve the post type object. If the object exists, the function returns true; otherwise, it returns false.

By using these functions together, you can easily register a custom post type in WordPress and update the permalink structure to include the custom post type slug.

Register an account to save your snippets or go Pro to get more features.