Example 1: wp_query get custom post type
<?php
$args = array(
'post_type' => 'custom_type',
'post_status' => 'publish',
'posts_per_page' => 8,
'orderby' => 'title',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_title();
the_excerpt();
endwhile;
wp_reset_postdata();
?>
Example 2: custom post type
function custom_post_type() {
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentytwenty' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentytwenty' ),
'menu_name' => __( 'Movies', 'twentytwenty' ),
'parent_item_colon' => __( 'Parent Movie', 'twentytwenty' ),
'all_items' => __( 'All Movies', 'twentytwenty' ),
'view_item' => __( 'View Movie', 'twentytwenty' ),
'add_new_item' => __( 'Add New Movie', 'twentytwenty' ),
'add_new' => __( 'Add New', 'twentytwenty' ),
'edit_item' => __( 'Edit Movie', 'twentytwenty' ),
'update_item' => __( 'Update Movie', 'twentytwenty' ),
'search_items' => __( 'Search Movie', 'twentytwenty' ),
'not_found' => __( 'Not Found', 'twentytwenty' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),
);
$args = array(
'label' => __( 'movies', 'twentytwenty' ),
'description' => __( 'Movie news and reviews', 'twentytwenty' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'genres' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'movies', $args );
}
add_action( 'init', 'custom_post_type', 0 );
Example 3: wordpress register post type
function book_setup_post_type() {
$args = array(
'public' => true,
'label' => __( 'Books', 'textdomain' ),
'menu_icon' => 'dashicons-book',
);
register_post_type( 'book', $args );
}
add_action( 'init', 'book_setup_post_type' );
Example 4: wp+get tags for custom post type
<?php
echo get_the_term_list( get_the_ID(), 'ecommerce_tag', '', ',' );
?>
Example 5: custom post type
.wp-block-code {
border: 0;
padding: 0;
}
.wp-block-code > div {
overflow: auto;
}
.hljs {
box-sizing: border-box;
}
.hljs.shcb-code-table {
display: table;
width: 100%;
}
.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}
.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}
.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}
.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}
.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}
.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}
.hljs.shcb-line-numbers .shcb-loc > span {
padding-left: 0.75em;
}
.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid
content: counter(line);
display: table-cell;
padding: 0 0.75em;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
function kp_workshops() {
$args = array(
'label' =>; __( 'Workshop', 'kp_workshops' ),
'description' =>; __( 'Workshop listing', 'kp_workshops' ),
'labels' =>; $labels,
'supports' =>; array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields' ),
'taxonomies' =>; array( 'category' ),
'hierarchical' =>; false,
'public' =>; true,
'show_ui' =>; true,
'show_in_menu' =>; true,
'menu_position' =>; 20,
'menu_icon' =>; 'dashicons-welcome-learn-more',
'show_in_admin_bar' =>; true,
'show_in_nav_menus' =>; true,
'can_export' =>; true,
'has_archive' =>; true,
'exclude_from_search' =>; false,
'publicly_queryable' =>; true,
'capability_type' =>; 'page',
'show_in_rest' =>; true,
);
register_post_type( 'workshops', $args );
}
add_action( 'init', 'kp_workshops', 0 );
Example 6: wordpress get 10 posts of each custom post type
function delete_front_page_query_results() {
delete_transient('post_data');
$query_cpt1 = array (
'posts_per_page' => 3,
'post_type' => 'cpt1'
);
$query_cpt2 = array (
'posts_per_page' => 3,
'post_type' => 'cpt2'
);
$query_cpt3 = array (
'posts_per_page' => 3,
'post_type' => 'cpt3'
);
$query_cpt4 = array (
'posts_per_page' => 3,
'post_type' => 'cpt4'
);
$query_cpt5 = array (
'posts_per_page' => 3,
'post_type' => 'cpt5'
);
$query_results[] = get_posts($query_cpt1);
$query_results[] = get_posts($query_cpt2);
$query_results[] = get_posts($query_cpt3);
$query_results[] = get_posts($query_cpt4);
$query_results[] = get_posts($query_cpt5);
$flatten_array =array();
foreach ($query_results as $data) {
foreach($data as $flatten_data) {
$flatten_array[] = $flatten_data;
}
}
function cpt_array_sort($a, $b) {
return strtotime($b->post_date) - strtotime($a->post_date);
}
usort($flatten_array, 'cpt_array_sort');
set_transient ( 'post_data', $flatten_array, 365*24*60*60);
}
add_action('publish_post', 'delete_front_page_query_results);