Verificare se un post esiste

18 dic 2016, 12:30:23
Visualizzazioni: 14.4K
Voti: 1

Come posso verificare se un post con nome ad esempio Meteo esiste? Se non esiste, vorrei crearlo.

function such_post_exists($title) {
global $wpdb;

// Pulisce il titolo per l'uso nel database
$p_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );

if ( !empty ( $title ) ) {
    // Verifica se esiste un post con questo titolo
    return (int) $wpdb->query("SELECT FROM $wpdb->posts WHERE post_title = $p_title");
}

return 0;
}
0
Tutte le risposte alla domanda 2
3

WordPress dispone di una funzione post_exists che restituisce l'ID del post oppure 0 in caso contrario. Quindi puoi fare

if ( 0 === post_exists( $title ) ) {
    **YOUR_CODE_HERE**
}
18 dic 2016 12:40:33
Commenti

E se cambio l'ordine: if( 0 == post_exists($post_name) )

kilogram kilogram
18 dic 2016 12:52:06

@kilogram Funzionerebbe in entrambi i modi

Tunji Tunji
18 dic 2016 12:53:35

Ma controlla anche i post cestinati?? Ci sono alternative che controllano solo bozze/pubblicati?

sanjeev shetty sanjeev shetty
27 mar 2020 11:11:53
0

Se il Post non esiste, crealo... SE esiste, aggiornalo.


$post_title = "Questo Titolo Fantastico";
$post_content = "Il mio contenuto su qualcosa di interessante.";
$post_status = "publish"; //publish, draft, etc
$post_type = "page"; // o qualsiasi tipo di post desiderato

/* Prova a trovare l'ID del post tramite il titolo se esiste */
$found_post_title = get_page_by_title( $post_title, OBJECT, $post_type );
$found_post_id = $found_post_title->ID;

/**********************************************************
** Controlla se la pagina non esiste, se vero, crea un nuovo post 
************************************************************/
if ( FALSE === get_post_status( $found_post_id ) ): 

      $post_args = array(
        'post_title' => $post_title,
        'post_type' => $post_type,
        'post_content'=> $post_content,
        'post_status'  => $post_status,
        //'post_author'  => get_current_user_id(),

        /* Se hai campi meta in cui inserire dati */ 
        'meta_input'   => array(
            'meta_key1' => 'il mio valore',
            'meta_key2' => 'il mio altro valore',
        ),
      );      

      /* Aggiunge un nuovo post wp nel db, restituisce l'ID del post */
      $returned_post_id = wp_insert_post( $post_args );  

      /* Aggiorna il template della pagina solo se usi "page" come post_type */ 
      update_post_meta( $returned_post_id, '_wp_page_template', 'my-page-template.php' ); 

      /* Aggiunge valori nei campi meta. Funziona con ACF CUSTOM FIELDS!! */
      $field_key = "My_Field_KEY";
      $value = "il mio valore personalizzato";
      update_field( $field_key, $value, $returned_post_id );

      $field_key = "My_Other_Field_KEY";
      $value = "il mio altro valore personalizzato";
      update_field( $field_key, $value, $returned_post_id );

      /* Salva un valore di checkbox o select */
      // $field_key = "My_Field_KEY";
      // $value = array("rosso", "blu", "giallo");
      // update_field( $field_key, $value, $returned_post_id );

      /* Salva in un campo repeater */
      // $field_key = "My_Field_KEY";
      // $value = array(
      //   array(
      //     "ss_name" => "Foo",
      //     "ss_type" => "Bar"
      //   )
      // );
      // update_field( $field_key, $value, $returned_post_id );

      /* Mostra una risposta! */
      echo "<span class='pg-new'><strong>". $post_title . " Creato!</strong></span><br>";
      echo "<a href='".esc_url( get_permalink($returned_post_id) )."' target='_Blank'>". $post_title . "</a><p>";


else:        
/***************************
** SE IL POST ESISTE, aggiornalo 
****************************/

      /* Aggiorna il post */
      $update_post_args = array(
        'ID'           => $found_post_id,
        'post_title'   => $post_title,
        'post_content' => $post_content,
      );

      /* Aggiorna il post nel database */
      wp_update_post( $update_post_args );

      /* Aggiorna i valori nei campi meta */
      $field_key = "My_Field_KEY";
      $value = "il mio valore personalizzato";
      update_field( $field_key, $value, $found_post_id );

      $field_key = "My_Other_Field_KEY";
      $value = "il mio altro valore personalizzato";
      update_field( $field_key, $value, $found_post_id );

      /* Mostra una risposta! */
      echo "<span class='pg-update'><strong>". $post_title . " Aggiornato!</strong></span><br>"; 
      echo "<a href='".esc_url( get_permalink($found_post_id) )."' target='_Blank'>Visualizza</a> | <a href='post.php?post=".$found_post_id."&action=edit'>". $post_title . "</a><p>";

endif;

10 feb 2019 23:56:14