How to Get & Display Custom Post Type Data (Useful Examples)

In this article, we will take a look into getting different types of custom post type data and display them in the post template.

But before moving further, let’s assume I have a custom post type Events and its slug name is ‘events’. Now we will use this case for all the examples further in the article.

Get & Display Custom Post Type Data in WordPress

In general, To get the posts from a custom post type we use WP_Query with an array of arguments, and then use WordPress loop to get the desired results.

The code snippet given below is the standard WordPress loop to get data from the custom post type. You can use this along with the examples to get desired custom post type data.

<?php

$args = array(
	'post_type' => 'events',
	'posts_per_page' => 5,
	);

$query = new WP_Query($args);

if($query->have_posts()):
	while($query->have_posts()):
		$query->the_post();
	
	// Add example code here to get desired results.
	
	endwhile;

/* Restore original Post Data */
wp_reset_postdata();	

endif;

?>

Custom Post Type ID

To get the custom post type ID, you can use get_the_ID() function. this will return the ID of current custom post type item in WordPress loop.


// Previous standard WordPress Loop code

echo get_the_ID();

// Next standard WordPress Loop code

Custom Post Type Title

To get the custom post type title, you can use get_the_title() function. this will return the title of current custom post type item in WordPress loop.


// Previous standard WordPress Loop code

echo get_the_title();

// Next standard WordPress Loop code

Custom Post Type Permalink | URL

To get the custom post type permalink or URL, you can use get_the_permalink() function. this will return the URL of current custom post type item in WordPress loop.


// Previous standard WordPress Loop code

echo get_the_permalink();

// Next standard WordPress Loop code

Custom Post Type Excerpt

To get the custom post type excerpt, you can use get_the_excerpt() function. this will return the excerpt of current custom post type item in WordPress loop.


// Previous standard WordPress Loop code

echo get_the_excerpt();

// Next standard WordPress Loop code

Custom Post Type Content

To get the custom post type content, you can use get_the_content() function. this will return the content of current custom post type item in WordPress loop.


// Previous standard WordPress Loop code

echo get_the_content();

// Next standard WordPress Loop code

Custom Post Type Featured Image

To get the custom post type featured image, you can use get_the_post_thumbnail_url() function. this will return the featured image URL of current custom post type item in WordPress loop.


// Previous standard WordPress Loop code

echo '<img src="'. get_the_post_thumbnail_url().'"/>';

// Next standard WordPress Loop code

Custom Post Type Name

To get the custom post type name, you can use get_post_type() function. this will return the name of current custom post type.


// Previous standard WordPress Loop code

echo get_post_type();

// Next standard WordPress Loop code

Custom Post Type Label Name

To get the custom post type label name, you can use get_post_type_object() function by passing the custom post type name in it.

This will return the post object of current custom post type, and then you can use labels->singular_name and labels->name along with post object to get the label name of custom post type.


// Previous standard WordPress Loop code

// Get post type object
$post_type_obj = get_post_type_object( get_post_type() );

// Displays singular label name of custom post type: Event
echo $post_type_obj->labels->singular_name; 

// Displays plural label name of custom post type: Events
echo $post_type_obj->labels->name; 

// Next standard WordPress Loop code

Custom Post Type Slug

To get the custom post type slug, you can use get_post_type_object() function by passing the custom post type name in it. this will return the post object of your custom post type.

Then you can use rewrite[‘slug’] along with post object to get the custom post type slug.


// Previous standard WordPress Loop code

// Get post type object
$post_type_obj = get_post_type_object( get_post_type() );

// Displays custom post type slug
echo $post_type_obj->rewrite['slug']; 

// Next standard WordPress Loop code

Get & Display List of All Custom Post Type Posts

To get the list of all posts from a custom post type we use WP_Query with an array of arguments and WordPress loop.

To get all posts, make sure you set posts_per_page to -1. Otherwise, it will display the default number of posts set to be displayed under Settings >> Reading.

<?php

$args = array(
	'post_type' => 'events',
	'posts_per_page' => -1,
	);

$query = new WP_Query($args);

if($query->have_posts()):
	while($query->have_posts()):
		$query->the_post();
	
	echo '<ul>';	

	echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';

	echo '</ul>';
	
	endwhile;

/* Restore original Post Data */
wp_reset_postdata();	

endif;

?>
Owner of diveinwp.com, solely responsible for creating helpful, informative content about WordPress, a fulltime WordPress developer with 8+ years of hands-on experience in WordPress design and development.