How to find what actions/filters/hooks are triggered on creating a New Question or Answer?

4.66K viewsGeneral
0

Hi Rahul,

As you know, sometimes to post new questions on my site, I use customized function, which creates CPT – Questions and Answers (code snippet posted below). They both are created and posted fine with my function. But they are missing the relevant meta activities. So my questions are –

  1. What are different meta activities used by AnsPress while creating/updating a question/answer, specially how to use “__ap_activity” with my function?
  2. How to find all the functions/meta activities (by different plugins) in general (not localized to anspress only), which are triggered on creating a new question/answer? [I am particularly interested in finding what is triggered in “Next Scripts: Social Networks Auto Poster”, because, I found that this plugin does work with AnsPress, but only when I create the new question from AnsPress itself, it does NOT trigger when I create the new question from my customized function. I want it to trigger from my function, but I couldn’t figure out how to do that?]
  3. OR, is there any way, by which wordpress can automatically process, all the functions etc. after creating the new post with `
    wp_insert_post

    ` just like it does automatically when new post is created from admin section?

Relevant part of my function –

https://gist.github.com/atultiwari/01c130e0da0cb58d51b3a990d031e404

<?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);
}
?>

Thanks.

I am unhealthy today will answer soon

Hi, no problem. Take proper rest. Good night.

1

In 3.0.0 this issue was addressed. From now you can use:

ap_save_question()

https://github.com/anspress/anspress/blob/master/includes/ask-form.php#L255

This new function take care of all post meta. Try and let me know.

commented on answer

Hi, how’s your health now?
I (kind of) reverse tracked to your mentioned function on anspress GitHub. From there i learned a lot about this process.. and finally after so many failures, i have successfully made my own plugin (though it’s very premitive, like pre-pre alpha) to do many of my desired functions from WordPress itself. Previously i was calling my functions from outside of WordPress… and apparently somehow that was working with anspress but not nextscript… after migrating to plugin mode, everything seems to be working fine… just few minor bugs, which i am planning to post tomorrow night. Seriously, anspress helped me a lot to learn.

Thanks, I am feeling better now. Sure, let me know if you stuck again, I will be happy to help.