How to display custom post type content with WP_Query ?
First you need register a custom post type via functions.php then you have to add WP_Query with your custom post type slug. Here is the example for WP_Query.
<?php
// WP_Query arguments
$args = array(
'post_type' => 'post', // slug for custom post type
'post_status' => 'publish', // Also support: pending, draft, auto-draft, future, private, inherit, trash, any
'posts_per_page' => '5', // use -1 for all post
'order' => 'DESC', // Also support: ASC
'orderby' => 'date', // Also support: none, rand, id, title, slug, modified, parent, menu_order, comment_count
);
// The Query
$query = new WP_Query($args);
// The Loop
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// do something
the_title();
the_content();
the_excerpt();
the_post_thumbnail(); }
} else {
// no posts found
get_template_part('template-parts/content', 'none');
}
// Restore original Post Data
wp_reset_postdata();
If you have a taxonomy in custom post type , then you can add tax_query within the WP_Query.
<?php
// WP_Query arguments
$args = array(
'post_type' => 'custom post type slug',
'post_status' => 'publish',
'posts_per_page' => '5',
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'custom-category', // taxonomy slug
'terms' => array(1, 2), // term ids
'field' => 'term_id', // Also support: slug, name, term_taxonomy_id
'operator' => 'IN', // Also support: slug, name, term_taxonomy_id
'include_children' => true,
),
),
);