Error invalid parameters with REST API

Trying to use the WordPress API to create a blog post with tags/categories, etc. but running into some errors. I am running the PHP code below outside of my WordPress instance and get the following:

CODE

function CreatePost($title, $content, $tag){
    $username = 'username';
    $password = 'password';
    $category = 'test category words name test';
    $rest_api_url = "https://www.urlurlurlurl.com/wp-json/wp/v2/posts";

$data_string = json_encode([
    'title'    => $title,
    'content'  => $content,
    'status'   => 'publish',
    'tags' => 'test tag',
    'category' => $category
]);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rest_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string),
    'Authorization: Basic ' . base64_encode($username . ':' . $password),
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
echo $result;
curl_close($ch);
}

ERROR

{"code":"rest_invalid_param","message":"Invalid parameter(s): tags","data":{"status":400,"params":{"tags":"tags[0] is not of type integer."}}}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The error in question — “Invalid parameter(s): tags” and “tags[0] is not of type integer.“, means that you need to supply a list of tag IDs and not names or slugs. So for examples, 'tags' => 123 and 'tags' => [ 123 ] are both valid. (Comma-separated list is also accepted, e.g. 'tags' => '123,4,5'.)

And all that also apply to the default category taxonomy and custom taxonomies (e.g. my_tax), except that for category, you should use categories and not category. So for example, use 'categories' => 5 and not 'category' => 5.

From your comment:

is there a way for me to always use the name instead of the ID?

You can try one of these (or both for testing..):

  1. You can first create the tag/category using the REST API (e.g. /wp/v2/categories for categories) and get the tag/category ID from the API response, and then use it when creating your post.

    So you’d be making two REST API requests, one for creating the tag/category, and another for creating the post.

  2. On your WordPress site, you can register custom REST API fields like tags_name and categories_slug:
    // In your theme functions.php file:
    
    add_action( 'rest_api_init', 'my_register_rest_fields' );
    function my_register_rest_fields() {
        register_rest_field( 'post', 'tags_name', [
            'update_callback' => function ( $names, $post ) {
                return wp_set_post_tags( $post->ID, $names );
            }
        ] );
    
        register_rest_field( 'post', 'categories_slug', [
            'update_callback' => function ( $slugs, $post ) {
                $ids = [];
    
                foreach ( wp_parse_list( $slugs ) as $slug ) {
                    if ( $category = get_category_by_slug( $slug ) ) {
                        $ids[] = $category->term_id;
                    }
                }
    
                return ( ! empty( $ids ) ) ?
                    wp_set_post_categories( $post->ID, $ids ) : false;
            }
        ] );
    }
    

    Then when creating your post, in the API request body/data, use 'categories_slug' => 'cat-one, cat-two, etc' for categories, and 'tags_name' => 'one, two, etc' for tags. And remember, for categories, you need to use the category slug and not name.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x