ARRAY_FILTER WARNING-wp-Includes/post.php第3148行

时间:2017-12-13 作者:Enzo

我插入the following code, 请参见下文,从前端提交帖子。提交时,会出现以下警告:

警告:array\\u filter()要求参数1为数组,字符串在[…]中给出/wp包括/后。php在线3148

我不知道警告来自哪里,哪个数组是字符串?

function ty_front_end_form() {

    ?>
    <form id="custom-post-type" name="custom-post-type" method="post" action="">

    <p><label for="title">Post Title</label><br />

    <input type="text" id="title" value="" tabindex="1" size="20" name="title" />

    </p>

    <p><label for="description">Post Description</label><br />

    <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>

    </p>

    <p><?php wp_dropdown_categories( \'show_option_none=Category&tab_index=4&taxonomy=category\' ); ?></p>

    <p><label for="post_tags">Post 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="post-type" id="post-type" value="custom_posts" />

    <input type="hidden" name="action" value="custom_posts" />

    <?php wp_nonce_field( \'name_of_my_action\',\'name_of_nonce_field\' ); ?>

    </form>
    <?php

    if($_POST){
        ty_save_post_data();
    }

}
add_shortcode(\'custom-post\',\'ty_front_end_form\');

function ty_save_post_data() {

    if ( empty($_POST) || !wp_verify_nonce($_POST[\'name_of_nonce_field\'],\'name_of_my_action\') )
    {
       print \'Sorry, your nonce did not verify.\';
       exit;

    }

    else{ 

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

        if(isset($_POST[\'post_tags\'])){
        $tags = $_POST[\'post_tags\'];
        }else{
        $tags = "";
        }

        // Add the content of the form to $post as an array
        $post = array(
            \'post_title\' => wp_strip_all_tags( $title ),
            \'post_content\' => $description,
            \'post_category\' => $_POST[\'cat\'],  // Usable for custom taxonomies too
            \'tags_input\' => $tags,
            \'post_status\' => \'draft\',           // Choose: publish, preview, future, etc.
            \'post_type\' => \'listings\'  // Use a custom post type if you want to
        );
        wp_insert_post($post);  // http://codex.wordpress.org/Function_Reference/wp_insert_post

        $location = home_url(); // redirect location, should be login page 

        echo "<meta http-equiv=\'refresh\' content=\'0;url=$location\' />"; exit;
    } // end IF

}

1 个回复
最合适的回答,由SO网友:bueltge 整理而成

如果检查错误通知中的行,则应在WP 4.9.1中找到该函数的用法array_filter, 需要一个数组作为参数(see the code).您的post数组中的值需要一个数组\'post_category\', 当前为字符串。

来自WP核心的来源:

* @type array  $post_category         Array of category names, slugs, or IDs.
*                                     Defaults to value of the \'default_category\' option.
在您的源代码示例中,您应该更改它,它应该可以工作,\'post_category\' => array( $_POST[\'cat\'] ),.

// Add the content of the form to $post as an array
$post = array(
    \'post_title\' => wp_strip_all_tags( $title ),
    \'post_content\' => $description,
    \'post_category\' => array( $_POST[\'cat\'] ), // Usable for custom taxonomies too
    \'tags_input\' => $tags, // Need an array
    \'post_status\' => \'draft\', // Choose: publish, preview, future, etc.
    \'post_type\' => \'listings\', // Use a custom post type if you want to
);
wp_insert_post( $post );

结束

相关推荐

Array Formation Issue

我正在运行SQL查询,以获取当前登录用户的post ID。我的查询按设计工作。$userGadgets = $wpdb->get_results(\"SELECT item_id FROM mg_gd_mylist WHERE user_id = \'$user_id\'\", ARRAY_A); 我面临的问题与返回的数组有关。array( 16 ) { [0]=> array( 1 ) { [\\\"item_id\\\"]=>