Issue with inserting questions programmatically

7.71K viewsIssuesAnspress issue
0

Hi,

I tried to insert question programmatically, like you mentioned in your FAQ. In fact, I used almost the same sample code you provided (except that I changed Tag Id and names).

Unfortunately it threw me an error.

Link to my sample gist –

https://gist.github.com/atultiwari/2f0a877e9e63c0c4ed334ecf50189308

 

I have already downloaded the latest one from master branch (and confirmed that manual mode etc are there).

 

Similarly I tried to post answer, it threw me error – “the server responded with a status of 500 (Internal Server Error)”‘

Link to Gist file –

https://gist.github.com/atultiwari/a7c5cfb43324b08125a2cbec6bd15a02

Answer is getting posted with –

$post_id = wp_insert_post($values);

but not with Hook.

I tried to look for where am I making mistake for hours now, but I am unable to figure it out.

 

Edit 1

After the fixed pushed by @Rahul, questions are getting inserted programmatically, as described. However still there are some issues, such as –

  1. Tags are not getting inserted (I tried to find out if it’s with me only or it’s an issue with the script, but I couldn’t), while question and category is inserted correctly
  2. Question metas and activity didn’t get inserted when using sample code provided in FAQ –>I think it’s because of $qameta was never used. I found these working if I use the functions “ap_insert_qameta” and “ap_update_post_activity_meta” on $qameta, however I am not sure if I am using the correct method.
  3. When trying similar method to insert Answer, it didn’t insert post_parent and answer got orphan. While reading the source code for Answer form and related submit function, I observed that here post_parent  is passed under the variable named $question_id , so I tried to pass it like this, but still it failed. var_dump revealed that while passing the value for $question_id it’s storing them in array, first is boolean and in 2nd one it’s strong the parent_post id, which is never used. While if we use the same function on default answer form (when answering the question from GUI), $question_id stores the parent post id. How? I couldn’t understand it.
  4. Even if submitting answer programmatically started working, it will also need some metas and activity update for answer as well to complete the all steps.
  5. I think in complete source section of the FAQ line 15 you forgot to put ; therefore it’s not working at all (out of the box)

Regards,

Atul

Attachments

err1.png
0

Hello Dr Atul,

I will check it again and update our FAQ with full working code.

commented on answer

Ok. I will wait for your update, as whatever I tried couldn’t solve all issues, resulting in waste of time only.

Hi, were you able to check it?

I am checking now.

Ok.. waiting for your update

0

Hello Guys,

Just made a commit to fix the issue. @atultiwari codes from our FAQ will work now.

commented on answer

Hi Rahul, Since I am still having difficulties in using code sample, I have highlighted some issues I faced and updated the question. Please go through Edit 1 of this question. Thanks.

0

Since the originally posted method in FAQ didn’t work for me. Even the Anspress Ask Form stopped working, I reverted back to last officially published version (4.1.4).

After seeing the e.g. posted in FAQ, I modified the `submit_question_form` function and removed the redirect part. It’s working for me. I don’t know if I am on the right track or not. Rahul please have a look at the gist (and also why your modified function is not working?) –

https://gist.github.com/atultiwari/4bc9fd36c5fb2a18081762189fd6a347

 

Also, it appears that in the e.g. posted in your FAQ, you forgot to implement `$qameta`. Right now, I am implementing following method (it’s present in linked gist) –

` ap_insert_qameta( $post_id, $qameta );ap_insert_qameta( $post_id, $qameta );

$activity_type = ‘new_question’;

/** Following should have worked, but not working –

ap_activity_add( array( ‘q_id’   => $post_id, ‘action’ => $activity_type, ) );

**/

// Following function appears to be deprecated, but it was the only thing working for me –

ap_update_post_activity_meta( $post_id, $activity_type, get_current_user_id() );  `

 

Please comment if this is the correct way, or something else should be done (especially `ap_activity_add` didn’t work, while `ap_update_post_activity_meta` worked for me).

 

Regards,

1

I noticed a similar issue with 4.1.5 yesterday. Upgraded to it (as you do..), and found I couldn’t post anything anymore, so had to roll back.

commented on answer

Its fixed. I am adding a unit test case so that this should not happen anymore.