Skip to:

WP-API create a new topic and assign a parent forum

  • pathardepavan


    Dear All,

    I have a mobile app.I am able to list the forums. Get all the threads under forum. Now i want to create a thread under a forum. I am able to create now with wp-api. I am not able to assign it to a parent forum.

    Tried with post_parent and parent_id with no luck.

    can any one help ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • That a nice project ! Any code you can share so we see what you try to do ?



    Please find the code below.. I am sure it must be a very small error

    add_action( 'rest_api_init', function() {
     register_api_field( 'topic',
           'get_callback'    => 'slug_get_post_meta_cb',
           //'update_callback' => 'slug_update_post_meta_cb',
           'update_callback' => 'slug_update_spaceship2',
           'schema'          => null,
    function slug_update_spaceship2( $value, $object, $field_name ) {
       return update_post_meta( $object->ID, $field_name, $value);
    function slug_get_post_meta_cb( $object, $field_name, $request ) {
        return get_post_meta( $object['id'], $field_name );



    @pathardepavan — post_parent is in the posts table rather than postmeta so instead of updating post meta, update the object in wp_posts. So your update callback function (update_topic_post_parent) would be something like:

    function update_topic_post_parent( $value, $object, $field_name ){
    $update_post_parent = array(
    'ID' => (int)$object->ID, // the ID of the topic you just created
    'post_parent' => (int)$value ); // whatever you want your post_parent to be as an int. You could use $field_name for 'post_parent' here but I tend to whitelist 
    wp_update_post( $update_post_parent )

    and you should be able to get the post_parent from the $object directly in your get callback I think.

    Also if you’re using one of the latest versions of the plugin, you should use register_rest_field as register_api_field has been deprecated.

    (I’m doing this without any testing, sorry, so it might be incorrect! But I think that’s somewhere close to the answer maybe 🙂 )



    Certainly! In the WordPress REST API, creating a new topic and assigning it to a parent forum involves utilizing the wp/v2 endpoints for both forums and topics. To achieve this, you can follow these steps:

    ### 1. Get the Forum ID
    Firstly, you’ll need to know the ID of the parent forum to which you want to assign the new topic. You can retrieve this ID by making a GET request to the forums endpoint, typically /wp/v2/forums.

    For example:
    GET /wp/v2/forums

    ### 2. Create a New Topic
    Make a POST request to the topics endpoint, usually /wp/v2/topics, providing the necessary parameters including title, content, and forum to link it to the specific forum.

    For example:
    POST /wp/v2/topics
    Content-Type: application/json

    “title”: “New Topic Title”,
    “content”: “Content of the new topic”,
    “forum”: <forum_id>
    Replace <forum_id> with the ID of the parent forum retrieved from step 1.

    ### Example Using cURL
    Using cURL, the process might look something like this:
    curl -X POST -H “Content-Type: application/json” -d ‘{“title”:”New Topic Title”,”content”:”Content of the new topic”,”forum”:1}’
    Replace with your actual WordPress site URL and 1 with the ID of the desired forum.

    Remember to authenticate the API request if your WordPress site requires authentication for creating new content.

    This approach allows you to create a new topic and assign it to a parent forum via the WordPress REST API. Adjust the endpoint URLs and parameters as needed based on your specific WordPress configuration.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar