Advertencia de array_filter - wp-includes/post.php línea 3148

13 dic 2017, 15:02:57
Vistas: 2.1K
Votos: 1

Inserté el siguiente código, ver abajo, para enviar un post desde el front-end. Al enviarlo, aparece esta advertencia:

Warning: array_filter() expects parameter 1 to be an array, string given in [...]/wp-includes/post.php on line 3148

Y no puedo identificar de dónde viene la advertencia, ¿qué array es un string?

function ty_front_end_form() {

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

    <p><label for="title">Título del Post</label><br />

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

    </p>

    <p><label for="description">Descripción del Post</label><br />

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

    </p>

    <p><?php wp_dropdown_categories( 'show_option_none=Categoría&tab_index=4&taxonomy=category' ); ?></p>

    <p><label for="post_tags">Etiquetas del Post</label>

    <input type="text" value="" tabindex="5" size="16" name="post-tags" id="post-tags" /></p>

    <p align="right"><input type="submit" value="Publicar" 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 'Lo sentimos, tu nonce no se verificó.';
       exit;

    }

    else{ 

        // Validación básica del formulario para asegurar que hay contenido
        if (isset ($_POST['title'])) {
            $title =  $_POST['title'];
        } else {
            echo 'Por favor ingresa un título';
            exit;
        }
        if (isset ($_POST['description'])) {
            $description = $_POST['description'];
        } else {
            echo 'Por favor ingresa el contenido';
            exit;
        }

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

        // Agregar el contenido del formulario a $post como array
        $post = array(
            'post_title' => wp_strip_all_tags( $title ),
            'post_content' => $description,
            'post_category' => $_POST['cat'],  // También funciona para taxonomías personalizadas
            'tags_input' => $tags,
            'post_status' => 'draft',           // Opciones: publish, preview, future, etc.
            'post_type' => 'listings'  // Usar un tipo de post personalizado si se desea
        );
        wp_insert_post($post);  // http://codex.wordpress.org/Function_Reference/wp_insert_post

        $location = home_url(); // ubicación de redirección, debería ser la página de login 

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

}
1
Comentarios

Siempre deberías agregar el código, si es necesario para la pregunta. Si los usuarios lo leen más tarde y el enlace externo está roto, el q/a no es útil.

bueltge bueltge
13 dic 2017 15:19:00
Todas las respuestas a la pregunta 1
1

Si revisas la línea del aviso de error, deberías encontrar en WP 4.9.1 el uso de la función array_filter, que necesita un array como parámetro (ver el código). El valor dentro de tu array para un post necesita un array para 'post_category', actualmente es un string.

Desde el código fuente del núcleo de WP:

* @type array  $post_category         Array de nombres de categoría, slugs o IDs.
*                                     Por defecto toma el valor de la opción 'default_category'.

En tu ejemplo de código fuente deberías cambiar esto y debería funcionar, 'post_category' => array( $_POST['cat'] ),.

// Añade el contenido del formulario a $post como un array
$post = array(
    'post_title' => wp_strip_all_tags( $title ),
    'post_content' => $description,
    'post_category' => array( $_POST['cat'] ), // También usable para taxonomías personalizadas
    'tags_input' => $tags, // Necesita un array
    'post_status' => 'draft', // Opciones: publish, preview, future, etc.
    'post_type' => 'listings', // Usa un tipo de entrada personalizado si lo deseas
);
wp_insert_post( $post );
13 dic 2017 15:23:42
Comentarios

también tags_input es un array para las etiquetas, probablemente sea mejor usar explode con coma para aceptar más de una etiqueta

Shibi Shibi
13 dic 2017 15:31:29