Notificări la trimiterea unui post type personalizat din front-end

17 ian. 2012, 23:31:10
Vizualizări: 1.99K
Voturi: 0

Am un formular într-un template de pagină care permite utilizatorilor să trimită postări într-un post type personalizat din 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,
);

// Inserează povestea în baza de date
$post_success = wp_insert_post( $post );

Funcționează, dar primesc aceste notificări

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

Am analizat fișierele menționate și se pare că sunt legate de comment_status și ping_status ale postării trimise, însă presupun că valorile implicite pentru wp_insert_post ar trebui să completeze aceste valori. Chiar și când le setez în array-ul de argumente, primesc notificările. Cum pot rezolva această problemă pentru a elimina notificările?

0
Toate răspunsurile la întrebare 3
0

Problema este în utilizarea variabilei $post pentru argumente. După ce am schimbat-o cu ceva de genul $new_post nu mai primesc notificările.

Trebuie să fie un conflict cu global $post pe care WordPress îl folosește.

Am luat asta din Codex, dar uitându-mă înapoi la pagină, am realizat că $post se referea doar la numele argumentului și exemplul real din codex nu folosește $post pentru array-ul de argumente.

18 ian. 2012 20:33:55
0

Inserare Date în Postare Personalizată din Interfața Front-End

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

    $title     = $_POST['title'];
    $post_type = 'product';
    //array-ul de argumente care va fi inserat cu wp_insert_post
    $front_post = array(
    'post_title'    => $title,
    'post_status'   => 'publish',          
    'post_type'     => $post_type 
    );

    //inserăm postarea în baza de date folosind $new_post în wp_insert_post
    //stocăm ID-ul postării în variabila $pid
    $post_id = wp_insert_post($front_post);
    //folosim $pid (post id) pentru a adăuga metadatele postării
    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"]);

Cod HTML Aici

<form method="POST">
<label>Nume Produs</label>
        <input type="text" value="" class="input-xlarge" name='title'>
        <label>Descriere Produs</label>
        <textarea value="" rows="3" class="input-xlarge" name='short_description'>
        </textarea>
 <label>Preț</label>
        <input type="text" value="" class="input-xlarge" name='price'>
        <label>Dimensiuni (in):</label>
        <input type="text" value="" class="input-xlarge" name='length' placeholder="Lungime">
  <div>
            <button class="btn btn-primary">Adaugă Produs</button>
        </div>
        <input type="hidden" name="action" value="product" />
 </form>
14 iul. 2016 16:01:12
1
-1

Se pare că încă nu pot comenta, dar ai o virgulă în plus în array-ul tău.

 $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, //VIRGULĂ ÎN PLUS AICI
 );

Poate asta e problema ta.

 'post_author' => 4,

ar trebui să fie

 'post_author' => 4
17 ian. 2012 23:37:43
Comentarii

Mulțumesc, totuși virgula suplimentară nu este problema. Aceasta este validă într-un array PHP. Am rezolvat-o și voi reveni cu un răspuns după ce voi da șansa altora să răspundă.

jjeaton jjeaton
18 ian. 2012 00:09:56