0

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."}}}
4
  • The error "tags[0] is not of type integer means you need to provide tag IDs and not names, etc.
    – Sally CJ
    Oct 8 '20 at 2:39
  • Thank you @SallyCJ - I tried both 'category' => '5' and 'category' => 5 but neither seemed to work? No error, there is a category_id 5 for sure...
    – AAA
    Oct 8 '20 at 2:48
  • Also is there a way for me to always use the name instead of the ID? This is how it works with XMLRPC?
    – AAA
    Oct 8 '20 at 2:54
  • I've just posted an answer. Hope it helps.
    – Sally CJ
    Oct 8 '20 at 4:13
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.