I need to do the exact same what "update" button in the WP Backend does, but with PHP
wp_update_post
should be the correct answer, but it is not.
The issue:
This is not the case if you look at the same post in the FrontEnd. The Checkboxes then show as "not checked"
After UPDATE (with the update button) but NO Changes, just click update in the WP Post Editor (quick edit does also not work), the Checkboxes show as "checked" also in the front end.
Since I can not do this action for a hundreds of posts manually, I need a php action to do that.
wp_update_post
DOES NOT WORK. It does just NOT PERFORM THE SAME ACTION as the UPDATE button, as it seems to me, due to what I experience.
Is there any other possibility to do the EXACT same as UPDATE (button) does?
ADDING CODE AS REQUESTED.
THIS CODE IS THE ENTIRE "Front end" FORM, which is also inserting the new posts with the new post_meta, which gets CORRECTLY displayed in WP ADMIN but not on front end
//ALL IS WORKING JUST FINE
//ALL VALUES; POSTS; CF'S ARE VISIBLE IN THE WP BACKEND!!!
//BUT CRED DOES NOT "READ" THE CF VALUES WHICH COME FROM BY BELOW wpdb QUERY, (see line 94 to 105 )
function create_exams_save_data_action( $post_id, $form_data ) {
// the "generate exam form"
if ( $form_data['id'] == 1081 ) {
global $wpdb;
//get the curretn question post
$post = get_post($post_id);
//get current question's parent exam
$belongs_to_mother_parent_exam = get_post_meta($post_id, '_wpcf_belongs_exam_id', true);
//get the current question's parent (mother-exam) post and ID stuff
$mother_parent_exam_post = get_post($belongs_to_mother_parent_exam);
$mother_parent_exam_id = $mother_parent_exam_post->ID;
$mother_parent_exam_name = $mother_parent_exam_post->post_name;
//get the mother-exams parent (training)
$exam_belongs_to_training = get_post_meta($mother_parent_exam_id, '_wpcf_belongs_training_id', true);
//current user, new author
global $current_user;
get_currentuserinfo();
$new_exam_title = $current_user->user_login;
$new_exam_owner = $current_user->ID;
//insert the new exam, with parent the Training
$new_exam = array(
'comment_status' => $mother_parent_exam_post->comment_status,
'ping_status' => $mother_parent_exam_post->ping_status,
'post_author' => $new_exam_owner,
'post_content' => '',
'post_excerpt' => '',
'post_name' => $mother_parent_exam_name . '-' . $new_exam_title,
'post_password' => '',
'post_status' => 'private',
'post_title' => $mother_parent_exam_name . '-' . $new_exam_title,
'post_type' => 'exam',
'to_ping' => '',
'menu_order' => '');
//insert post
$new_exam_id = wp_insert_post( $new_exam, $wp_error );
//update post parent
update_post_meta($new_exam_id, '_wpcf_belongs_training_id', $exam_belongs_to_training);
//get the ID of this new exam to update the questions parent fields later on
$new_exam_id_ID = get_post($new_exam_id);
$new_belongs_to_new_parent_exam = $new_exam_id_ID->ID;
//query all questions (childs of Mother-Exam) and return only the ones with parent same as question in loop
$all_questions = get_posts(array(
'numberposts' => -1,
'post_type' => 'question',
'meta_key' => '_wpcf_belongs_exam_id',
'meta_value' => $belongs_to_mother_parent_exam)
);
//if it returns some, get data from those one for each (all data of all those questions), adn duplicate them with post_data
if( $all_questions ){
foreach( $all_questions as $single_question ){
//get each questions post data
$single_question_post = get_post($single_question);
$single_question_id = $single_question_post->ID;
$single_question_parent_exam = get_post_meta($single_question_post, '_wpcf_belongs_training_id');
$possible_score = get_post_meta($single_question_post, 'wpcf-possible-maximum-score');
//Duplicate (insert) each new question
$new_question = array(
'ID' => '',
'comment_status' => $single_question_post->comment_status,
'ping_status' => $single_question_post->ping_status,
'post_author' => $new_exam_owner,
'post_content' => '',
'post_excerpt' => '',
'post_name' => $single_question_post->post_name . '-' . $new_exam_title,
'post_password' => '',
'post_status' => 'publish',
'post_title' => $single_question_post->post_title . '-' . $new_exam_title,
'post_type' => 'question',
'to_ping' => '',
'menu_order' => '');
$new_question_id = wp_insert_post( $new_question );
update_post_meta($new_question_id, '_wpcf_belongs_exam_id', $new_belongs_to_new_parent_exam);
update_post_meta($new_question_id, 'wpcf-possible-maximum-score', $possible_score);
//Now duplicate all content of post meta of each new question
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$single_question_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_question_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
}
}
}
}
add_action('cred_save_data', 'create_exams_save_data_action',10,2);
The problem you are having is that wp_update_post
only updates the post itself. For updating custom fields, you need to use update_post_meta
for each of your custom fields.
Added in response to posted code:
I have taken your code and I have cleaned up quite a bit of unnecessary manipulations. Mainly you often took the id of a post to get the post itself and then use that post to get the id. Rather circular and not at all useful.
For my own understanding I simplified some variable names. I'm not entirely sure of the hierarchy of your training exam, parent exam and question but I'm going to assume that _wpcf_belongs_exam_id
is the post->post_parent
of the exam being generated and similarly _wpcf_belongs_training_id
is the same as the "mother" exam's post_parent
value. If so you might save a bit of headache by using this instead.
I'm mainly guessing at the purpose of the last part as it is highly confusing to visualize without the Custom Post Type code (you forgot to at least describe it.
If CRED is showing data differently from your WP backend then perhaps it is reacting poorly to the custom field ids you have assigned. In WP if you assign a meta key with the prefixed underscore _something
it becomes "protected". Read More It's possible that you've added the underscore and CRED can't "see" it because of this protected state.
Anyway here is what might be a shorter, simpler and more straightforward code base for you. Again the only line I am unsure of is the final update_post_meta($new_exam_id, $meta->meta_key, addslashes($meta->meta_value));
based on the progression I saw you taking, I think that $new_exam_id
is the correct post to update meta on, but you can change it as is truly the case.
//ALL IS WORKING JUST FINE
//ALL VALUES; POSTS; CF'S ARE VISIBLE IN THE WP BACKEND!!!
//BUT CRED DOES NOT "READ" THE CF VALUES WHICH COME FROM BY BELOW wpdb QUERY, (see line 94 to 105 )
function create_exams_save_data_action( $post_id, $form_data ) {
// the "generate exam form"
if ( $form_data['id'] == 1081 ) {
global $wpdb;
//get the curretn question post
$post = get_post($post_id);
//get current question's parent exam
//get the current question's parent (mother-exam) post and ID stuff
$parent_exam_id = get_post_meta($post_id, '_wpcf_belongs_exam_id', true);
$parent_exam_post = get_post($belongs_to_parent_exam);
$parent_exam_name = $parent_exam_post->post_name;
//get the mother-exam's parent (training)
$exam_belongs_to_training = get_post_meta($parent_exam_id, '_wpcf_belongs_training_id', true);
//current user, new author
global $current_user;
get_currentuserinfo();
$new_exam_title = $current_user->user_login;
$new_exam_owner = $current_user->ID;
//insert the new exam, with parent the Training
$new_exam = array(
'comment_status' => $parent_exam_post->comment_status,
'ping_status' => $parent_exam_post->ping_status,
'post_author' => $new_exam_owner,
'post_content' => '',
'post_excerpt' => '',
'post_name' => $parent_exam_name . '-' . $new_exam_title,
'post_password' => '',
'post_status' => 'private',
'post_title' => $parent_exam_name . '-' . $new_exam_title,
'post_type' => 'exam',
'to_ping' => '',
'menu_order' => '');
//insert post
$new_exam_id = wp_insert_post( $new_exam, $wp_error );
//update post parent
update_post_meta($new_exam_id, '_wpcf_belongs_training_id', $exam_belongs_to_training);
//query all questions (childs of Mother-Exam) and return only the ones with parent same as question in loop
$all_questions = get_posts(array(
'numberposts' => -1,
'post_type' => 'question',
'meta_key' => '_wpcf_belongs_exam_id',
'meta_value' => $belongs_to_parent_exam)
);
//if it returns some, get data from those one for each (all data of all those questions), and duplicate them with post_data
if( $all_questions ){
foreach( $all_questions as $single_question ){
//get each questions post data
$single_question_parent_exam = get_post_meta($single_question, '_wpcf_belongs_training_id');
$possible_score = get_post_meta($single_question->ID, 'wpcf-possible-maximum-score');
//Duplicate (insert) each new question
$new_question = array(
'ID' => '',
'comment_status' => $single_question->comment_status,
'ping_status' => $single_question->ping_status,
'post_author' => $new_exam_owner,
'post_content' => '',
'post_excerpt' => '',
'post_name' => $single_question->post_name . '-' . $new_exam_title,
'post_password' => '',
'post_status' => 'publish',
'post_title' => $single_question->post_title . '-' . $new_exam_title,
'post_type' => 'question',
'to_ping' => '',
'menu_order' => '');
$new_question_id = wp_insert_post( $new_question );
update_post_meta($new_question_id, '_wpcf_belongs_exam_id', $new_exam_id);
update_post_meta($new_question_id, 'wpcf-possible-maximum-score', $possible_score);
//Now duplicate all post meta of each new question
$post_meta_data = get_metadata('question', $single_question->ID)
if (count($post_meta_data)!=0) {
foreach ($post_meta_data as $meta) {
update_post_meta($new_exam_id, $meta->meta_key, addslashes($meta->meta_value));
}
}
}
}
}
}
add_action('cred_save_data', 'create_exams_save_data_action',10,2);