How to Add A Custom Post Type In WordPress?

16 minutes read

To add a custom post type in WordPress, you can follow these steps:

  1. Open your WordPress theme's functions.php file. This file is located in the theme folder.
  2. Inside the functions.php file, you need to add the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
function custom_post_type() {
   $args = array(
      'public' => true,
      'label' => 'Custom Post Type',
      'rewrite' => array('slug' => 'custom-post-type'),
      'supports' => array(
         'title',
         'editor',
         'thumbnail',
         'excerpt',
         'custom-fields',
      ),
   );

   register_post_type('custom_post', $args);
}

add_action('init', 'custom_post_type');


In this code, 'public' defines whether the post type should be visible to users, 'label' is the name of your custom post type, 'rewrite' specifies the URL slug, and 'supports' lists the features you want to include for your custom post type.

  1. Save the changes made to the functions.php file.
  2. After adding the code, you can go to your WordPress dashboard and navigate to the "Plugins" menu.
  3. Click on "Editor" under the "Plugins" menu. This will open the code editor.
  4. On the right-hand side, you will see a list of files. Select "functions.php" from the list.
  5. Scroll down within the code editor to find the code you added earlier.
  6. Click on the "Update File" button to save the changes.


After following these steps, your custom post type named "Custom Post Type" will be added to your WordPress site. You can further customize and configure the post type according to your needs.

Best WordPress Books to Read in 2024

1
Building Web Apps with WordPress: WordPress as an Application Framework

Rating is 5 out of 5

Building Web Apps with WordPress: WordPress as an Application Framework

2
WordPress: The Missing Manual: The Book That Should Have Been in the Box

Rating is 4.9 out of 5

WordPress: The Missing Manual: The Book That Should Have Been in the Box

3
WordPress 5 Complete: Build beautiful and feature-rich websites from scratch, 7th Edition

Rating is 4.8 out of 5

WordPress 5 Complete: Build beautiful and feature-rich websites from scratch, 7th Edition

4
WordPress 5 Cookbook: Actionable solutions to common problems when building websites with WordPress

Rating is 4.7 out of 5

WordPress 5 Cookbook: Actionable solutions to common problems when building websites with WordPress

5
WordPress Plugin Development Cookbook: Explore the complete set of tools to craft powerful plugins that extend the world's most popular CMS, 3rd Edition

Rating is 4.6 out of 5

WordPress Plugin Development Cookbook: Explore the complete set of tools to craft powerful plugins that extend the world's most popular CMS, 3rd Edition

6
WordPress All-in-One For Dummies (For Dummies (Computer/Tech))

Rating is 4.5 out of 5

WordPress All-in-One For Dummies (For Dummies (Computer/Tech))

7
Professional WordPress: Design and Development

Rating is 4.4 out of 5

Professional WordPress: Design and Development

8
WordPress: Pushing the Limits

Rating is 4.3 out of 5

WordPress: Pushing the Limits


What is the 'publicly_queryable' argument used for when registering a custom post type?

The 'publicly_queryable' argument is used when registering a custom post type in WordPress to determine whether the post type can be queried publicly.


By default, this argument is set to true, allowing the post type to be accessed via public queries like the main query, post archives, or search results. When set to false, the custom post type will not be publicly accessible, meaning it cannot be queried by public visitors.


Here's an example of how to use the 'publicly_queryable' argument when registering a custom post type:

1
2
3
4
5
6
7
8
$args = array(
    'public' => true,
    'publicly_queryable' => true,  // Set to true by default
    'label' => 'Books',
    // other arguments...
);

register_post_type('book', $args);


In the above example, the custom post type 'book' is publicly queryable, which means it can be queried by the public.


How to display custom post type information in RSS feeds?

To display the custom post type information in RSS feeds, you can use the pre_get_posts action hook to modify the query for RSS feeds and include the custom post type posts. Here's an example code snippet that you can add to your theme's functions.php file:

1
2
3
4
5
6
7
8
function include_custom_post_type_in_feeds($query) {
    if ($query->is_feed() && !is_admin()) {
        $post_types = array('custom_post_type'); // Replace 'custom_post_type' with your actual custom post type slug(s)

        $query->set('post_type', $post_types);
    }
}
add_action('pre_get_posts', 'include_custom_post_type_in_feeds');


Make sure to replace 'custom_post_type' with the slug of your custom post type. Additionally, you can include multiple custom post types by adding their slugs to the $post_types array.


By adding this code, the custom post type posts will be included in the query for RSS feeds. You can then modify your RSS templates (feed-rss.php, feed-rss2.php, etc.) to display the custom post type information alongside the regular post content.


You can access the custom post type specific information using the regular WordPress template tags and functions within the RSS templates. For example, if you want to display the custom post type's title, you can use the_title() function:

1
2
3
4
<item>
    <title><?php the_title(); ?></title>
    <!-- Other post content goes here -->
</item>


Remember to update the RSS templates to match your theme's structure and styling preferences.


What are the default capabilities for a custom post type?

The default capabilities for a custom post type in WordPress are as follows:

  1. edit_posts: Allows users to edit their own custom posts.
  2. edit_others_posts: Allows users to edit custom posts created by other users.
  3. delete_posts: Allows users to delete their own custom posts.
  4. delete_others_posts: Allows users to delete custom posts created by other users.
  5. publish_posts: Allows users to publish their own custom posts.
  6. read_private_posts: Allows users to read private custom posts.
  7. read: Allows users to view custom post types.
  8. edit_private_posts: Allows users to edit their own private custom posts.
  9. edit_published_posts: Allows users to edit their own published custom posts.
  10. delete_private_posts: Allows users to delete their own private custom posts.
  11. delete_published_posts: Allows users to delete their own published custom posts.
  12. publish_postmeta: Allows users to publish metadata associated with custom posts.
  13. edit_postmeta: Allows users to edit metadata associated with custom posts.
  14. delete_postmeta: Allows users to delete metadata associated with custom posts.
  15. read_postmeta: Allows users to read metadata associated with custom posts.
  16. edit_content: Allows users to edit the content of custom posts.
  17. delete_content: Allows users to delete the content of custom posts.
  18. edit_attachments: Allows users to edit attachments associated with custom posts.
  19. delete_attachments: Allows users to delete attachments associated with custom posts.
  20. publish_attachments: Allows users to publish attachments associated with custom posts.


These capabilities can be customized and extended using plugins or by directly modifying the code.

Best WordPress Hosting Providers in 2024

1
DigitalOcean

Rating is 5 out of 5

DigitalOcean

2
AWS

Rating is 4.9 out of 5

AWS

3
Vultr

Rating is 4.8 out of 5

Vultr

4
Cloudways

Rating is 4.7 out of 5

Cloudways


How to add custom taxonomies to a custom post type?

To add custom taxonomies to a custom post type in WordPress, you can follow these steps:

  1. Register the custom taxonomy: Use the register_taxonomy() function to define the taxonomy. This function takes several parameters, including the taxonomy name, the post types it should be attached to, and its settings. function custom_taxonomy() { $labels = array( 'name' => _x( 'Custom Taxonomy', 'taxonomy general name' ), 'singular_name' => _x( 'Custom Taxonomy', 'taxonomy singular name' ), 'search_items' => __( 'Search Custom Taxonomy' ), 'all_items' => __( 'All Custom Taxonomy' ), 'parent_item' => __( 'Parent Custom Taxonomy' ), 'parent_item_colon' => __( 'Parent Custom Taxonomy:' ), 'edit_item' => __( 'Edit Custom Taxonomy' ), 'update_item' => __( 'Update Custom Taxonomy' ), 'add_new_item' => __( 'Add New Custom Taxonomy' ), 'new_item_name' => __( 'New Custom Taxonomy Name' ), 'menu_name' => __( 'Custom Taxonomy' ), ); $args = array( 'hierarchical' => true, // Set to true for a hierarchical taxonomy like categories, false for a flat taxonomy like tags 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'custom-taxonomy' ), ); register_taxonomy( 'custom_taxonomy', array( 'custom_post_type' ), $args ); } add_action( 'init', 'custom_taxonomy' );
  2. Register the custom post type: If you haven't already, register a custom post type using the register_post_type() function. Add the taxonomies parameter and specify the custom taxonomy. function custom_post_type() { $args = array( 'labels' => $labels, 'description' => 'Custom Post Type', 'public' => true, 'menu_position' => 5, 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ), 'taxonomies' => array( 'custom_taxonomy' ), // Specify the custom taxonomy here 'has_archive' => true, ); register_post_type( 'custom_post_type', $args ); } add_action( 'init', 'custom_post_type' );
  3. Save and activate the code: Add the above code to your theme's functions.php file or a custom plugin. Save the file and activate the theme or plugin.


After following these steps, you should see the new custom taxonomy when editing the custom post type, and you can assign terms to the taxonomy from there.


How to create custom permalinks for a custom post type?

To create custom permalinks for a custom post type in WordPress, follow these steps:

  1. Register your custom post type: If you haven't already registered your custom post type, you can use the register_post_type() function in your theme's functions.php file or in a custom plugin. Here's an example:
1
2
3
4
5
6
7
8
function custom_post_type() {
  $args = array(
    'rewrite' => array( 'slug' => 'custom-post' ), // Set the base slug for your custom post type
    // additional arguments for your custom post type
  );
  register_post_type( 'custom_post', $args );
}
add_action( 'init', 'custom_post_type' );


  1. Flush rewrite rules: After registering the custom post type, you need to flush the rewrite rules to apply the changes. Visit the WordPress dashboard -> Settings -> Permalinks and click on the "Save Changes" button. This will regenerate the rewrite rules.
  2. Adjust custom post type permalink structure: By default, WordPress will use the structure /%post_type%/%custom_post% for custom post types. To modify this structure, you can use the post_type_link filter to customize the permalink for your custom post type.
1
2
3
4
5
6
7
8
function custom_post_permalink( $permalink, $post ) {
  if ( false !== strpos( $permalink, '%custom_post%' ) ) {
    $custom_taxonomy = get_the_terms( $post->ID, 'custom_taxonomy' ); // If you have a custom taxonomy associated with the custom post type
    $permalink = str_replace( '%custom_post%', array_pop( $custom_taxonomy )->slug, $permalink );
  }
  return $permalink;
}
add_filter( 'post_type_link', 'custom_post_permalink', 10, 2 );


In the example above, the %custom_post% placeholder is replaced with the slug of the custom taxonomy associated with the custom post type. You can modify this code to fit your specific needs.

  1. Save the custom permalink structure: After customizing the permalink structure, you need to save the custom post type to refresh the permalinks. Visit the WordPress dashboard -> Settings -> Permalinks and click on the "Save Changes" button again.


Now, the URLs for your custom post type will use the custom permalink structure you have defined.


How to limit the number of posts displayed for a custom post type?

To limit the number of posts displayed for a custom post type in WordPress, you can use the posts_per_page parameter in the WP_Query class or the pre_get_posts filter. Here's how you can achieve it:

  1. Using the posts_per_page parameter in WP_Query:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$args = array(
    'post_type'      => 'your_custom_post_type',
    'posts_per_page' => 10, // Limit to 10 posts per page
);

$your_custom_query = new WP_Query( $args );

// Run your loop to display posts
if ( $your_custom_query->have_posts() ) {
    while ( $your_custom_query->have_posts() ) {
        $your_custom_query->the_post();
        // Display your post content
    }
    
    // Restore original post data
    wp_reset_postdata();
}


  1. Using the pre_get_posts filter in your theme's functions.php or a custom plugin:
1
2
3
4
5
6
function limit_posts_per_page( $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'your_custom_post_type' ) ) {
        $query->set( 'posts_per_page', 10 ); // Limit to 10 posts per page
    }
}
add_action( 'pre_get_posts', 'limit_posts_per_page' );


Note: Replace 'your_custom_post_type' with the name of your actual custom post type.


How to remove a custom post type from the admin menu?

To remove a custom post type from the admin menu in WordPress, you can use the remove_menu_page() function. Here's an example:

1
2
3
4
function remove_custom_post_type_from_menu() {
    remove_menu_page('edit.php?post_type=your_custom_post_type');
}
add_action('admin_menu', 'remove_custom_post_type_from_menu');


Replace 'your_custom_post_type' with the actual name of your custom post type.


This code should be added to your theme's functions.php file or in a custom plugin. Once added and activated, your custom post type will be removed from the admin menu.

Facebook Twitter LinkedIn Telegram

Related Posts:

To add a featured image to a post in WordPress, you can follow these steps:Go to your WordPress dashboard and click on &#34;Posts&#34; or &#34;Add New&#34; if you are creating a new post.If you are editing an existing post, find the post you want to add a feat...
To create a custom login page in WordPress, you will need to follow these steps:Create a new PHP file: Start by creating a new PHP file for your custom login page. You can name it according to your preference, like &#34;custom-login.php&#34;. Add the necessary...
To post data in PHP without using a form, you can make use of PHP&#39;s cURL library or the HTTP POST method. Here&#39;s how you can achieve it:Using cURL: First, initialize a cURL session using curl_init(). Set the URL to which you want to post the data using...