Avvisi durante l'invio di un custom post type dal front-end

17 gen 2012, 23:31:10
Visualizzazioni: 1.99K
Voti: 0

Ho un form in un page template che permette agli utenti di inviare post in un custom post type dal front-end:

$post = array(
    'post_status' => 'draft',
    'post_type' => 'stories',
    'post_content' => wp_kses_post( $story_content ),
    'post_title' => esc_attr( wp_kses( $story_title, array() ) ),
    'post_author' => 4,
);

// Inserisce la storia nel database
$post_success = wp_insert_post( $post );

Funziona, ma ricevo questi avvisi:

PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/comment-template.php on line 776
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/comment-template.php on line 793
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/general-template.php on line 1645
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/link-template.php on line 1106
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/link-template.php on line 1148
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/link-template.php on line 1106
PHP Notice:  Trying to get property of non-object in /Users/username/Sites/the_site/wp-includes/link-template.php on line 1148

Ho esaminato i file referenziati e sembra essere relativo a comment_status e ping_status del post inviato, tuttavia, presumo che i valori predefiniti di wp_insert_post dovrebbero inserire valori per questi. Anche quando li imposto nel mio array di argomenti, ricevo comunque gli avvisi. Come posso risolvere per eliminare questi avvisi?

0
Tutte le risposte alla domanda 3
0

Il problema sta nell'utilizzo della variabile $post per gli argomenti. Dopo averla cambiata in qualcosa come $new_post non ricevo più i notice.

Deve esserci qualche conflitto con il global $post che WordPress utilizza.

Ho preso questo dal Codex, ma rileggendo la pagina, mi sono reso conto che $post si riferiva solo al nome dell'argomento e l'esempio effettivo nel codex non utilizza $post per l'array degli argomenti.

18 gen 2012 20:33:55
0

Inserisci Dati nel Custom Post dal Front End

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

    $title     = $_POST['title'];
    $post_type = 'product';
    //l'array di argomenti da inserire con wp_insert_post
    $front_post = array(
    'post_title'    => $title,
    'post_status'   => 'publish',          
    'post_type'     => $post_type 
    );

    //inserisce il post nel database passando $new_post a wp_insert_post
    //memorizza l'ID del post in una variabile $pid
    $post_id = wp_insert_post($front_post);
    //ora usiamo $pid (post id) per aggiungere i metadati del post
    update_post_meta($post_id, "short_description", @$_POST["short_description"]);
    update_post_meta($post_id, "price", @$_POST["price"]);
    update_post_meta($post_id, "length", @$_POST["length"]);

Codice HTML Qui

<form method="POST">
<label>Nome Prodotto</label>
        <input type="text" value="" class="input-xlarge" name='title'>
        <label>Descrizione Prodotto</label>
        <textarea value="" rows="3" class="input-xlarge" name='short_description'>
        </textarea>
 <label>Prezzo</label>
        <input type="text" value="" class="input-xlarge" name='price'>
        <label>Dimensioni (pollici):</label>
        <input type="text" value="" class="input-xlarge" name='length' placeholder="Lunghezza">
  <div>
            <button class="btn btn-primary">Aggiungi Prodotto</button>
        </div>
        <input type="hidden" name="action" value="product" />
 </form>
14 lug 2016 16:01:12
1
-1

Sembra che ancora non riesca a commentare, ma hai una virgola in più nel tuo array.

 $post = array(
     'post_status' => 'draft', // bozza
     'post_type' => 'stories', // storie
     'post_content' => wp_kses_post( $story_content ), // contenuto della storia
     'post_title' => esc_attr( wp_kses( $story_title, array() ) ), // titolo della storia
     'post_author' => 4, //VIRGOLA IN PIÙ QUI
 );

Forse è questo il tuo problema.

 'post_author' => 4, // autore del post

dovrebbe essere

 'post_author' => 4 // autore del post
17 gen 2012 23:37:43
Commenti

Grazie, tuttavia la virgola aggiuntiva non è il problema. È valida in un array PHP. Ho risolto, e risponderò dopo aver dato agli altri la possibilità di rispondere.

jjeaton jjeaton
18 gen 2012 00:09:56