从Frontend WordPress插入帖子时,也保存自定义元框

发布于 2025-01-24 15:27:41 字数 953 浏览 4 评论 0原文

$student = array(
            'post_title' => wp_strip_all_tags( $student_name ),
            'post_status' => 'private', 
            'post_type' => 'student'  
        );

        wp_insert_post($student);

这就是我从前端插入帖子的方式。正常工作。 但是我还需要从Frontend和帖子中保存一些自定义字段,并且需要在后端显示值(仅在输入字段中以文本格式为单位)。因此,是否存在类似的东西“ my_custom_filed” => 'custom_value':

$student = array(
            'post_title' => wp_strip_all_tags( $student_name ),
             'my_custom_filed' => 'custom_value'
            'post_status' => 'private', 
            'post_type' => 'student' 
           
        );

        wp_insert_post($student);

因此,我可以在后端打印自定义字段的值

 function studentDataMetaBoxStructure(){
        global $post;
        $my_custom_filed = get_post_meta($post->ID,'my_custom_filed',true);

        echo $my_custom_filed;

    }

,但我在$ my_custom_filed中什么也没得到;

$student = array(
            'post_title' => wp_strip_all_tags( $student_name ),
            'post_status' => 'private', 
            'post_type' => 'student'  
        );

        wp_insert_post($student);

This is how I'm inserting post from the frontend. it is working fine.
But I need to save some custom fields also from frontend along with post and need show the values in backend(just in text format not in input field). So is there anything exists like this 'my_custom_filed' => 'custom_value':

$student = array(
            'post_title' => wp_strip_all_tags( $student_name ),
             'my_custom_filed' => 'custom_value'
            'post_status' => 'private', 
            'post_type' => 'student' 
           
        );

        wp_insert_post($student);

So I can print the value of custom fields in backend

 function studentDataMetaBoxStructure(){
        global $post;
        $my_custom_filed = get_post_meta($post->ID,'my_custom_filed',true);

        echo $my_custom_filed;

    }

But I'm getting nothing in $my_custom_filed;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2025-01-31 15:27:41

这就是您在插入后保存元箱的方式。

 $student = array(
                    'post_title' => wp_strip_all_tags( $student_name ),
                     'my_custom_filed' => 'custom_value'
                    'post_status' => 'private', 
                    'post_type' => 'student' 
                   
                );
        
               $the_post_id =  wp_insert_post($student);
               update_post_meta( $the_post_id, 'custom_meta_box_key', 'custom_meta_box_value' );

This is how you save meta box while post insertion.

 $student = array(
                    'post_title' => wp_strip_all_tags( $student_name ),
                     'my_custom_filed' => 'custom_value'
                    'post_status' => 'private', 
                    'post_type' => 'student' 
                   
                );
        
               $the_post_id =  wp_insert_post($student);
               update_post_meta( $the_post_id, 'custom_meta_box_key', 'custom_meta_box_value' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文