Dorokhov.codes
15. Post Types
There are many different types of content in WordPress.
Internally, all of the Post Types are stored in the same place — in the wp_posts
database table — but are
differentiated by a database column called post_type
.
Different Post Types are displayed by different Template files.
Default Post Types
There are several default Post Types readily available to users or internally used by the WordPress installation. The most common are:
- Post (Post Type:
post
) - Page (Post Type:
page
) - Attachment (Post Type:
attachment
) - Revision (Post Type:
revision
) - Navigation menu (Post Type:
nav_menu_item
) - Block templates (Post Type:
wp_template
) - Template parts (Post Type:
wp_template_part
)
Creating a post type
Once a custom post type is registered, it gets a new top-level administrative screen that can be used to manage and create posts of that type.
To register a new post type, you use the register_post_type() function.
function register_my_custom_post_type() {
register_post_type( 'product',
array(
'labels' => array(
'name' => __( 'Products', 'textdomain' ),
'singular_name' => __( 'Product', 'textdomain' ),
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'register_my_custom_post_type' );
Custom slug
To set a custom slug for the slug of your custom post type all you need to do is add a key => value
pair to the rewrite
key in the register_post_type()
arguments array.
function wporg_custom_post_type() {
register_post_type('wporg_product',
array(
'labels' => array(
'name' => __( 'Products', 'textdomain' ),
'singular_name' => __( 'Product', 'textdomain' ),
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'products' ), // my custom slug
)
);
}
add_action('init', 'wporg_custom_post_type');
Using in a template
The following templates can display Custom post types:
single-{post-type}
archive-{post-type}
search
index
Add a new record
$post_data = array(
'post_title' => 'New Post Title',
'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
'post_status' => 'publish',
'post_type' => 'custom_post_type', // Replace with your custom post type slug
);
$post_id = wp_insert_post($post_data);
if (!is_wp_error($post_id)) {
// Post inserted successfully, you can perform additional actions here
echo 'New post added with ID: ' . $post_id;
} else {
// Handle the error
echo 'Error adding new post: ' . $post_id->get_error_message();
}
Queries
Use WP_Query class for such operations.
$query = new WP_Query( [
'title' => $name,
'post_type' => 'employer',
] );
if ( $query->have_posts() ) {
return true;
}
Show the existing post types
You can use this request to see the existing post types:
SELECT post_type, COUNT(*) AS total_posts
FROM wp_posts
GROUP BY post_type
ORDER BY total_posts DESC;
If you want to sort post types (post_type
) by the date of their first appearance (i.e., by the minimum ID), here’s the appropriate SQL query:
SELECT post_type, COUNT(*) AS total_posts, MIN(ID) AS first_post_id
FROM wp_posts
GROUP BY post_type
ORDER BY first_post_id ASC;