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.

It is recommended to create custom post types in the plugin instead of the theme, so that if we change the theme, the post types will still be there.

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.

We must call register_post_type() before the admin_init hook and after_setup_theme, a better option is the init hook.

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.

To ensure forward compatibility, do not use wp_ as a post type prefix, which is used by WordPress core. Make sure that the custom post type name is no more than 20 characters, as the column is a VARCHAR field of length 20 in the post_type database. If we use a name that is too generic - e.g.: product. it may conflict with other plugins or themes.

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 overly generic aliases may conflict with other themes or plugins.
Unlike custom post type identifiers, we can easily resolve post type slug conflicts by changing the slug for one of the conflicting post types. If the plugin author is smart enough, he or she can create a custom Filter hook using apply_filters() in the parameter, and then other developers can do this by overriding the register_post_type() parameter with this hook. There is no way to resolve the post type identifier conflict without disabling a conflicting post type.

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' );

We offer WordPress Themes and Plugins Custom Development Services

This site has long undertaken WordPress themes, plugins, WooCommerce-based store mall development business. We have 10 years of experience in WordPress development, if you want to Developing Websites with WordPress, please contact WeChat: iwillhappy1314 or email: amos@wpcio.com for inquiries.

发表回复

Your email address will not be published. 必填项已用 * 标注

*