Skip to:
Content
Pages
Categories
Search
Top
Bottom

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


  • pathardepavan
    Participant

    @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 ?
    Thanks,
    Pavan

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


    pathardepavan
    Participant

    @pathardepavan

    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',
        'post_parent',
        array(
           '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 );
    }

    tharsheblows
    Participant

    @tharsheblows

    @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 🙂 )

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