WP_INSERT_POST自定义类型和自定义分类

时间:2016-11-27 作者:Billy

我有一个自定义的帖子类型books 和3个自定义分类附加到books. 自定义分类法包括:books-categories , location , services_c. 我应该如何编辑我的表单以使分类法正常工作;我花了三天时间试图解决这个问题,但我做不到。有人能帮我吗?

<?php
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // 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
    $new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'tags_input\'    => array($tags),
        \'post_status\'   => \'draft\',           // Choose: publish, preview, future, draft, etc.
        \'post_type\' => \'books\'  //\'post\',page\' or use a custom post type if you want to
    );
    //save the new post
    $pid = wp_insert_post($new_post); 

}

?>  

<!-- New Post Form -->
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">

<!-- post name -->
<p><label for="title">Title</label><br />
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>

<!-- post Category -->
<p><label for="Category">Category:</label><br />
<p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=books-categories\' ); ?></p>

<!-- post Location -->
<p><label for="Location">Location:</label><br />
<p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=location\' ); ?></p>

<!-- post Services -->
<p><label for="Services">Services:</label><br />
<p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=services_c\' ); ?></p>

<!-- post Content -->
<p><label for="description">Content</label><br />
<textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
</p>

<!-- post tags -->
<p><label for="post_tags">Tags:</label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>

<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
</div>

2 个回复
SO网友:TheDeadMedic

你的逻辑有点缺陷-你跑isset 检查字段,如titledescription, 但如果未设置,则不要停止后期插入。

您还可以不检查post_tags 索引将永远不存在,因为wp_dropdown_categories 将使用字段名cat 除非你把它设成别的。

更不用说了tags_input 专门用于分类post_tag, 因此,即使其他一切都在发挥作用,这些条款也永远不会得到适当的分配。

让我们重构并使用PHP filter AP当我们这样做的时候:

<?php

if ( \'POST\' === $_SERVER[\'REQUEST_METHOD\'] && filter_input( INPUT_POST, \'action\' ) === \'new_post\' ) {
    $errors      = [];
    $title       = filter_input( INPUT_POST, \'title\',       FILTER_SANITIZE_STRING );
    $description = filter_input( INPUT_POST, \'description\', FILTER_SANITIZE_STRING );

    if ( ! $title ) {
        $errors[] = \'Please enter a title\';
    }

    if ( ! $description ) {
        $errors[] = \'Please enter a description\';
    }

    if ( ! $errors ) {
        $new_post_id = wp_insert_post([
            \'post_title\'   => $title,
            \'post_content\' => $description,
            \'post_status\'  => \'draft\',
            \'post_type\'    => \'books\',
        ]);

        if ( ! $new_post_id ) {
            $errors[] = \'Oops, something went wrong\';
        } else {
            /**
             * Loop over taxonomies and attempt to set post terms.
             */
            foreach ([
                \'books-categories\',
                \'location\',
                \'services_c\',
            ] as $post_taxonomy ) {

                // Field name is the format "{taxonomy}_term_id"
                if ( $term_id = ( int ) filter_input( INPUT_POST, "{$post_taxonomy}_term_id" ) ) {
                    wp_set_object_terms( $new_post_id, $term_id, $post_taxonomy );
                }
            }
        }
    }

    foreach ( $errors as $error ) {
        echo "$error<br />";
    }
}
现在我们只需要设置正确的输入name 对于每个分类法下拉列表,我都使用了{taxonomy}_term_id 为了避免混淆WordPress(如果您只使用分类名称作为输入,它可能会与query_var 对于上述分类法)。

wp_dropdown_categories( \'name=books-categories_term_id&show_option_none=Category&tab_index=4&taxonomy=books-categories\' );
对其他人也这样做,你应该很乐意去做。

SO网友:Asha
  add_filter(\'single_template\', \'my_single_template\');
    function my_single_template() {
        global $post;
        $this_plugin_dir = WP_PLUGIN_DIR . \'/\' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__));
        //echo $post->post_type; exit;
        /* Checks for single template by post type */
        if ($post->post_type == \'articles\') {
            if (file_exists($this_plugin_dir . \'/templates/single-articles.php\')) {
                return $this_plugin_dir . \'/templates/single-articles.php\';
            }
        }
        return $single;
    }

相关推荐