Home Wordpress Wordpress rest api to get custom post type posts

Wordpress rest api to get custom post type posts

by therichpost
Wordpress rest api to get custom post type posts

Hello friends, welcome back to my blog and today in this blog post, I am going to tell you, WordPress Rest API to get custom post type posts.

Friends here is the working code snippet for WordPress rest API to get custom post type posts and please use this into your theme’s functions.php file:

add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v2', '/cuspost/', array(
'methods' => 'GET',
'callback' => 'cuspost'
) );
} );

//callback function
function cuspost(){
   $args = array( 
    'post_type' => 'custom_post_type', 
    'post_status' => 'publish', 
    'nopaging' => true 
    $query = new WP_Query( $args ); // $query is the WP_Query Object
    $posts = $query->get_posts();   // $posts contains the post objects
    $output = array();
    foreach( $posts as $post ) {    // Pluck the id and title attributes
        $output[] = array( 'id' => $post->ID, 'title' => $post->post_title);
    wp_send_json( $output ); // getting data in json format.


2. API route will be:


Now we are done friends also and If you have any kind of query or suggestion or any requirement then feel free to comment below.

Note: Friends, I just tell the basic setup and things, you can change the code according to your requirements.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.



You may also like


deepak jerry November 3, 2022 - 11:36 am

Hello sir i also want category name

$args = array(
‘post_type’ => ‘custom_post_type’,
‘post_status’ => ‘publish’,
‘nopaging’ => true
‘taxonomy’ =>


therichpost November 3, 2022 - 12:21 pm

You want to add category names inside post query, right?


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.