Add Listings via REST API

Hi guys,

Thought I’d share a way to add listings via WP REST API.

First you have to add this PHP snippet:

function update_hp_listing_post_type($args, $post_type) {
    
    // Check if the current post type is 'hp_listing'
    if ('hp_listing' === $post_type) {
        
        // Set 'show_in_rest' property to true
        $args['show_in_rest'] = true;
    }
    
    // Return the modified arguments
    return $args;
}

// Hook into 'register_post_type_args'
add_filter('register_post_type_args', 'update_hp_listing_post_type', 10, 2);

This alters the existing arguments for listing post type to set ‘show_in_rest’ => true.

Now you can use https://example.com/wp-json/wp/v2/hp_listing endpoint to add listings via REST API.

You can turn this snippet off when you are done using the REST API, or edit listing page in WP Admin will look ugly.

2 Likes

Thanks for sharing!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.