WordPress Plugin Development Tutorial Manual - Customizing Post Types
WordPress custom post types are stored in the wp_psots datasheet and developers can register their own post types. In this section, we will discuss howSign up for custom article typesHowGetting custom post type content from the database and how to display that content to usersThe
Why Use Custom Post Types
Custom post types can also become custom content types, which can separate different types of posts for easy management.
Different post types tend to have different fields and different templates for displaying content, and WordPress makes a distinction between different types of posts in the program based on custom post types, and we can set up a separateCustomize MetaboxThe customizable templates can also be set up in a very convenient way by setting up a separate custom classification method.
Sign up for custom article types
WordPress has 5 post types by default: post, page, attachment, revision, menu. when we develop a plugin, we may need to create a custom post type, for example, "product" in an e-commerce site, "homework" in an e-learning site or "movie" in a video site.
We can use the register_post_type() function to register a custom post type, after registration, WordPress will automatically help us add a new post type menu to the background, we can use this menu to manage and create posts.
The following example will create a custom article type "wporg_product" with the name "product" in the database.
function wporg_custom_post_type() {
register_post_type( 'wporg_product',
[
'labels' => [
'name' => __( 'Products' ), [ 'labels' => [ 'products' ], [ 'labels' => [
'singular_name' => __( 'Product' ), .
],
'public' => true,
'has_archive' => true, .
]
);
}
add_action( 'init', 'wporg_custom_post_type' );
Please refer to the function description of register_post_type() to see all parameters.
Naming Best Practices
It's a better practice to prefix the post type name with a theme or plugin name to avoid conflicts with other plugins and to let other developers know where the post type came from.
web address
If we need to set an alias for a custom post type, we can add a key => value key-value pair to the rewrite parameter of register_post_type(). This is shown below:
function wporg_custom_post_type() {
register_post_type( 'wporg_product',
[
'labels' => [
'name' => __( 'Products' ), [ 'labels' => [ 'products' ], [ 'labels' => [
'singular_name' => __( 'Product' ), .
],
'public' => true,
'has_archive' => true,
'rewrite' => [ 'slug' => 'products' ], // my custom slug
]
);
}
add_action( 'init', 'wporg_custom_post_type' ); }
The URL structure for the custom post type created using the code above is: http://example.com/products/%product_name%
Using custom post types
We can create templates for custom post types and also display post content and archive using single.php and archive.php, the custom post type templates are created as follows:
- single- {post_type} .php - template for custom post type type content
- archive- {post_type} .php - template for displaying a list of posts with custom post types
where {post_type} is the $post_type parameter of the register_post_type() function.
For example, we can create a single-wporg_product.php article type content template and an archive-wporg_product.php archive template for the "wporg_product" article type.
Alternatively, we can use in any template file the is_post_type_archive() function to check if the current page is an archive page for the specified post type, and then use the post_type_archive_title() function to display the custom post type title.
Querying articles via custom article type parameters
We can use the post_type parameter to query for posts of a specified post type via WP_Query, e.g.:
$args = [
'post_type' => 'product',
'posts_per_page' => 10,
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post(); ?
? >
<div class="entry-content">
</div>
<?php
}
The above query fetches the latest 10 product articles and displays their titles and content one by one.
Modify the main query
Registering a custom post type doesn't automatically add it to the main query, so if we need to display the custom post type on the default archive, or include it on other post type archive pages, we can use the pre_get_posts The Action hook modifies the main query to add the custom article type parameter to the main query as follows:
function wporg_add_custom_post_types( $query ) {
if ( is_home() && $query->is_main_query() ) {
$query->set( 'post_type', [ 'post', 'page', 'movie' ] );
}
return $query.
}
add_action( 'pre_get_posts', 'wporg_add_custom_post_types' );