如何在wordpress

时间:2015-07-04 15:59:09

标签: php jquery wordpress checkbox

我需要完成完全与WP Backend中的“更新”按钮相同,但是使用PHP

wp_update_post 应该是正确答案,但事实并非如此。

问题:

  • 我有很多帖子,包含自定义字段。
  • 所有帖子中的所有自定义字段都是复选框。
  • 如果我查看所有帖子,请在WP Post Edit屏幕中查看全部内容 复选框已正确“选中”(!)

如果您查看FrontEnd中的同一帖子,情况就不是这样了。 然后复选框显示为“未选中”

更新后(使用更新按钮)但没有更改,只需单击WP Post Editor中的更新(快速编辑也不起作用),复选框在前面显示为“已检查”端。

由于我无法手动为数百个帖子执行此操作,我需要执行php操作。

wp_update_post不工作。 由于我的经验,它在我看来并不像UPDATE按钮那样执行相同的动作。

是否还有任何其他可能与UPDATE(按钮)相同的完全相同吗?

根据要求添加代码。

这个代码是整个“前端”FORM,它也插入了新帖子的新post_meta,它在WP ADMIN中正确显示但不在前端显示

//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);

1 个答案:

答案 0 :(得分:0)

您遇到的问题是wp_update_post仅更新帖子本身。要更新自定义字段,您需要为每个自定义字段使用update_post_meta

update_post_meta

已添加以回复发布的代码:

我已经接受了你的代码,我已经清理了相当多的不必要的操作。主要是你经常拿一个帖子的id来获得帖子本身,然后使用该帖子来获取id。相当循环,完全没用。

为了我自己的理解,我简化了一些变量名称。我不完全确定您的培训考试,家长考试和问题的等级,但我会假设_wpcf_belongs_exam_id是正在生成考试的post->post_parent,同样{ {1}}与"母亲"相同考试的_wpcf_belongs_training_id价值。如果是这样,你可以通过使用它来节省一些头痛。

我主要是猜测最后一部分的目的,因为如果没有自定义帖子类型代码可视化很困惑(你忘了至少描述它。

如果CRED显示的数据与您的WP后端不同,那么它可能对您分配的自定义字段ID反应不佳。在WP中,如果您为带有前缀下划线post_parent的元键指定它,则会变为“#34; protected"”。 Read More您可能已经添加了下划线,CRED无法看到"""因为这种受保护的状态。

无论如何,这里可能是一个更短,更简单,更直接的代码库。我不确定的唯一一行是基于我看到你所取得的进展的最终_something,我认为update_post_meta($new_exam_id, $meta->meta_key, addslashes($meta->meta_value));是更新元数据的正确帖子,但你可以真正改变它案子。

$new_exam_id