最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to checked selected category checkbox when my custom post inserted from frontend

programmeradmin1浏览0评论
if ( isset( $_POST['save'] )  ) {

    if(wp_verify_nonce($_REQUEST['_nonce'], 'submit-post')){

      // $userid = get_current_user_id();

            $atts = array( 
            'post_content'   => $_POST['content'], 
            'post_title'   => $_POST['title'],
           //'post_status'   => 'draft', /* It coulde be publish*/
            // 'post_excerpt'          => '',
            'post_category' =>  array('movies' => $_POST['movies']),
             //'comment_status'        => $_POST['comments'],
            'post_type'     => 'movies'
       // 'post_category' => array($_POST['movies'])


            );

           // $id = wp_insert_post( $postarr, $wp_error );
           $id = wp_insert_post( $atts, $wp_error );

           $fields = [
                    'movie_name',
                    'movie_release_date',
                   'movie_producedby',
                    'movie_starcast',
                    'movie_type',
                    'movie_genre',
                    'number_of_stars'
                   // 'movies'
            ];

            foreach ( $fields as $field ) {
                if ( array_key_exists( $field, $_POST ) ) {
                    update_post_meta( $id, $field, sanitize_text_field( $_POST[$field] ) );
                }
            }
    }
             else{ die("check seccurity");}

  }  //else{ die("check seccurity");}


            ?>     
                <form id="mvshortcode" name="mvshortcode" method = "post" action="" >

                <?php include plugin_dir_path( __FILE__ ) . './movieShortcodeForm.php';?>
                    <!-- <label for="title">Movie Title:</label>
                        <input type="text" name="title">                     
                    -->             
                    <input type="hidden" name="_nonce" value="<?php echo wp_create_nonce('submit-post') ?>">

                  <input type="submit" name="save">



                </form>
            <?php

}
if ( isset( $_POST['save'] )  ) {

    if(wp_verify_nonce($_REQUEST['_nonce'], 'submit-post')){

      // $userid = get_current_user_id();

            $atts = array( 
            'post_content'   => $_POST['content'], 
            'post_title'   => $_POST['title'],
           //'post_status'   => 'draft', /* It coulde be publish*/
            // 'post_excerpt'          => '',
            'post_category' =>  array('movies' => $_POST['movies']),
             //'comment_status'        => $_POST['comments'],
            'post_type'     => 'movies'
       // 'post_category' => array($_POST['movies'])


            );

           // $id = wp_insert_post( $postarr, $wp_error );
           $id = wp_insert_post( $atts, $wp_error );

           $fields = [
                    'movie_name',
                    'movie_release_date',
                   'movie_producedby',
                    'movie_starcast',
                    'movie_type',
                    'movie_genre',
                    'number_of_stars'
                   // 'movies'
            ];

            foreach ( $fields as $field ) {
                if ( array_key_exists( $field, $_POST ) ) {
                    update_post_meta( $id, $field, sanitize_text_field( $_POST[$field] ) );
                }
            }
    }
             else{ die("check seccurity");}

  }  //else{ die("check seccurity");}


            ?>     
                <form id="mvshortcode" name="mvshortcode" method = "post" action="" >

                <?php include plugin_dir_path( __FILE__ ) . './movieShortcodeForm.php';?>
                    <!-- <label for="title">Movie Title:</label>
                        <input type="text" name="title">                     
                    -->             
                    <input type="hidden" name="_nonce" value="<?php echo wp_create_nonce('submit-post') ?>">

                  <input type="submit" name="save">



                </form>
            <?php

}
Share Improve this question edited Apr 8, 2019 at 12:19 Karun 1,3471 gold badge9 silver badges22 bronze badges asked Apr 8, 2019 at 8:15 sunshinesunshine 113 bronze badges 1
  • Please add more explanation and write more about what you did and what you wanna do. – Amirition Commented Apr 9, 2019 at 9:27
Add a comment  | 

1 Answer 1

Reset to default 0
  // set Selected Post CATEGORY cheked 
    wp_set_object_terms( $post_id, array((int)$_POST['movies_cat']), 'movies' );
} else{ die("check seccurity");}  

// This bellow given code add on design page

<p class="meta-options hcf_field"> <label for="movies">The Categories</label> <?php wp_dropdown_categories( 'taxonomy=movies&name=movies_cat&show_option_all=Select a category' ); ?> </p>

发布评论

评论列表(0)

  1. 暂无评论