Skip to content

Instantly share code, notes, and snippets.

@atultiwari
Created May 26, 2016 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atultiwari/01c130e0da0cb58d51b3a990d031e404 to your computer and use it in GitHub Desktop.
Save atultiwari/01c130e0da0cb58d51b3a990d031e404 to your computer and use it in GitHub Desktop.
<?php
function AddQuestionToAnsPress($question_title, $question_content, $answer_content, $subject_id, $exam_id) {
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/wp-blog-header.php";
require($path);
$question_title = strip_tags($question_title);
$category_id = $subject_id;
$category = array($category_id);
$tag_subject_id = $subject_id;
$tags = array(25, $tag_subject_id);
wp_set_current_user(26);
$user_id_for_question = 26;
$question_array = array(
'post_title' => $question_title,
'post_author' => $user_id_for_question,
'post_content' => $question_content,
'post_type' => 'question',
'post_status' => 'publish',
'comment_status' => 'open'
);
$question_post_id = wp_insert_post($question_array);
$question_permalink = get_permalink($question_post_id);
wp_set_post_terms($question_post_id, $category, 'question_category');
wp_set_post_terms($question_post_id, $tags, 'question_tag');
update_post_meta($question_post_id, '_at_exam_id', $exam_id);
if ($question_post_id && $answer_content != "") {
wp_set_current_user(26);
$user_id_for_answer = 26;
$ans_array = array(
'post_author' => $user_id_for_answer,
'post_content' => $answer_content,
'post_type' => 'answer',
'post_status' => 'publish',
'post_parent' => $question_post_id,
'comment_status' => 'open',
);
$answer_post_id = wp_insert_post($ans_array);
}
if ($answer_post_id) {
update_post_meta($question_post_id, '_ap_selected', $answer_post_id);
update_post_meta($answer_post_id, '_ap_best_answer', 1);
}
$anspress_link = $question_permalink;
return array('anspress_link' => $anspress_link, 'question_post_id' => $question_post_id, 'answer_post_id' => $answer_post_id);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment