question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add categories, tags and custom fields when creating posts?

See original GitHub issue

This doesn’t seem to work for me

function createPost(){
    wp.posts().create({
        title: data.title,
        content: data.subtitle,
        tag: [ 'vegan', 'dinner' ],
        category: [ 'berlin', 'copenhagen' ],

    }).then(function( response ) {
        // "response" will hold all properties of your newly-created post,
        // including the unique `id` the post was assigned on creation
        console.log("post created with id: " + response.id );
    });
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
kadamwhitecommented, Jul 26, 2016

The chaining methods .category and .tag are only for filtering collections; if you’re creating a post, you’ll want to include the .tags or .categories arrays in the post data object, as you had it in the initial post up above. So, if we imagine ‘vegan’ is tag ID #1, ‘dinner’ is ID #2, category ‘berlin’ is ID #3, and ‘copenhagen’ is #4:

wp.posts().create({
    title: data.title,
    content: data.subtitle,
    tags: [ 1, 2 ],
    categories: [ 3, 4 ]
}).then(function( response ) {
    console.log( 'Post created with ID ' + response.id );
    console.log( 'Post categories: ' + response.categories.join() );
    console.log( 'Post tags: ' + response.tags.join() );
});

Hope that helps!

0reactions
kadamwhitecommented, Aug 5, 2016

@mrkrumhausen I do not believe so, but that’d be a good question to ask over on the main WP REST API repository.

I’m going to close this out, thanks for raising the issue and let me know if you run into any further trouble!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Add Categories to a Custom Post Type in WordPress
Add Categories to a Custom Post Type with a Plugin. If you're a beginner, we recommend using the Custom Post Type UI plugin...
Read more >
Creating Custom Content in WordPress: Custom Post Types
You can use the same key again and again for multiple posts, but each will have a unique value. WordPress also gives each...
Read more >
Display Custom Fields on Category, Tags and Taxonomy
I want to Add Custom Field on All Tags, Category, and Custom Post Type (Taxonomy) but with the below code get custom field...
Read more >
WordPress Custom Post Types: The All-In-One Guide ... - Kinsta
And once you have created a custom post type, how do you add to it, ... You can add categories and tags to...
Read more >
Convert custom fields to post categories
Most parameters might be added to the query like meta_query , category . wp_create_category() , will create a category once. wp_set_post_terms() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found