从FrontEnd表单添加标签到自定义帖子类型

时间:2018-03-07 00:00:33

标签: php wordpress

我创建了一个前端表单来上传网站上的新帖子。我正在更改它,以便我可以上传自定义帖子类型的信息。一切顺利,除了标签。提交表单后,除标签外都会创建所有字段。

这是我的function.php代码,用于插入表单

自定义帖子类型为'professional'

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && !is_admin(  )) {

    // Do some minor form validation to make sure there is content
    if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
    } else {
        echo 'Please enter a title';
    }
    if (isset ($_POST['description'])) {
        $description = $_POST['description'];
    } else {
        echo 'Please enter the content';
    }
    $tags = $_POST['post_tags'];

    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $title,
        'post_content'  => $description,
         // Deleted category line
        'tags_input'    =>  array($tags),
        'post_status'   => 'publish',           // Choose: publish, preview, future, etc.
        'post_type' => 'professionals'  // Use a custom post type if you want to
    );
    wp_insert_post($post);  // Pass  the value of $post to WordPress the insert function
                            // http://codex.wordpress.org/Function_Reference/wp_insert_post
  wp_redirect( home_url() );
  exit;
} // end IF

// Do the wp_insert_post action to insert it
do_action('wp_insert_post', 'wp_insert_post'); 

这是表单文件:

<div id="postbox">
   <form id="new_post" name="new_post" method="post" action="">
      <p><label class="form__title" for="title">Name and Professional Title</label><br />
         <input class="form__title-input" type="text" id="title" value="" tabindex="1" size="20" name="title" />
      </p>
      <p><label class="form__title" for="description">About You</label><br />
         <textarea class="form__content-area" id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
      </p>
      <p><label class="form__title" for="post_tags">Areas of Practice <small>(Separated by commas)</small> </label><br />
         <input class="form__title-input" type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" />
      </p>
      <p><input class="form__submit" type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
      <input type="hidden" name="post_type" id="post_type" value="professionals" />
      <input type="hidden" name="action" value="post" />
      <?php wp_nonce_field( 'new-post' ); ?>
   </form>
</div>

前端图片

Front End Form Image

这是我注册CPT和分类的代码。

function divorce_nation_custom_post_types() {
register_post_type( 'professionals', array(
    'rewrite'       => array('slug' => 'professionals'), // remeber to save permalink settings in admin
    'has_archive'   => true,
    'public'        => true,
    'taxonomies'    => array('post_tag') ,
    'labels'        => array(
        'name'          => 'Professionals',
        'add_new_item'  => 'Add New professional',
        'edit_item'     => 'Edit Professional',
        'all_items'     => 'Professionals',
        'singular_name' => 'Issue',
        'view_item'     => 'View Professional', 
        'view_items'    => 'View Professionals' 
    ),
    'menu_icon'     => 'dashicons-welcome-learn-more',
    'supports'      => array( 'title', 'editor', 'thumbnail', 'tags')
) );

register_taxonomy( 'area', 'professionals', array(
  'labels'  => array(
      'name'          => _x( 'Areas of Practice', 'taxonomy general name' ),
      'singular_name' =>  _x( 'Area', 'taxonomy singular name' ),
      'all_items'     => __( 'All Areas' ),
      'edit_items'     => __( 'Edit Area' ),
      'view_items'     => __( 'View Area' )
  ),
  'public'        => true,
  'show_ui'       => true,
  'show_in_menu'  => true,
  'description'   => 'Creates "Area of Practice" Tags'
) );
}
add_action('init', 'divorce_nation_custom_post_types');

“实践领域”输入应该在管理员中复制此结果。目前,我只能通过管理员添加标签。它的行为应该如此。但是,如果我通过前端表单添加标签,则无效。

enter image description here

1 个答案:

答案 0 :(得分:0)

要使用代码,您需要在使用'taxonomies' => array( 'post_tag' )注册自定义帖子类型时启用它们 - 从技术上讲,它们被视为分类,如您所见:

https://codex.wordpress.org/Function_Reference/register_post_type#taxonomies

如果他们未启用,您将无法使用wp_insert_post()将其添加到您的CPT。

<强>更新

听起来你真的想要tax_input => array()而不是tags_input => array()

由于“实践领域”是一种自定义分类法,您应该可以

tax_input => array(
    'taxonomyname' => 'term1',
    'taxonomyname' => 'term2',
)

如果不是这种情况,则表示插入帖子的当前用户没有assign_terms功能,此时您需要插入帖子后使用wp_set_object_terms()

wp_insert_post()将返回帖子ID,因此类似的内容非常常见:

$post_id = wp_insert_post( $my_post );
wp_set_object_terms( $post_id, array('term1', 'term2', 'taxonomyname' );