Insert/import questions programmatically
Generally AnsPress question can be inserted from from wp-admin and ask form. But sometimes we wants to insert question without using a form.
For using this tutorial you must have basic knowledge of WordPress and PHP. This tutorial require AnsPress 4.1.5 or above. At the time of writing this tutorial 4.1.5 was not released and can be downloaded only from AnsPress master branch.
By default AnsPress form have this fields:
- Post title (field name: post_title).
- Post content (field name: post_content).
- Is private (field name: is_private).
- Anonymous name (field name: anonymous_name).
- Post ID (field name: post_id).
Also few other fields are made available when ad-dons are enabled.
- Category (field name: category).
- Tags (field name: tags)
Source can be found here: AP_Form_Hooks::question_form().
We also have to make sure that the other addons continue to work properly. So to retain expected behavior we will add all of sanitized values to AnsPress form instead of directly using values.
We’are all done Its time to save question. Call below method:
Above method will return WP_Error
object on error and post id on success.
Default qameta and activities will be added automatically once post is created. For inserting/updating custom meta and activities you can do this:
Complete code:
If you have any confusion then feel free to ask it on our support form.
Can you add an import/ export function that the user just paste the question information on excel then upload to the website, which will save so much time.
posting multiple questions together not working.
is there a rest api?