Risposte nei forum create
-
AutorePost
-
9 Maggio 2017 alle 4:18 in risposta a: Risultati di ricerca in ordine di numero di parole del post_title #121610UgoPartecipante
buongiorno
C’è ancora qualcosa che non va.
Elenco i passaggi realizzati finora:
1) Ho creato il campo custom ‘parole’ su un CPT nominato ‘Products’.
2) Su file function.php ho inserito la funzione:
function on_all_status_transitions( $new_status, $old_status, $post ){
if ( ‘publish’ == $new_status && ‘publish’ !== $old_status ) {
$numero_parole = sizeof (explode(” “, $post->post_title));
update_post_meta( $post->ID, ‘parole’, $numero_parole );
}
add_action( ‘transition_post_status’, ‘on_all_status_transitions’, 10, 3 );
3) Creo o aggiorno un CTP Products in cui visualizzo su backend il campo ‘parole’ (al momento vuoto)
4) Pubblico la pagina
5) Il campo ‘parole’ non viene aggiornato con il numero di parole riportato su post_title
qualche suggerimento?
grazie a tutti
1 Maggio 2017 alle 7:13 in risposta a: Risultati di ricerca in ordine di numero di parole del post_title #121592UgoPartecipantetutto quello che sta nella funzione si riduce banalmente a:
if ( ‘publish’ == $new_status && ‘publish’ !== $old_status ) {
$numero_parole = sizeof (explode(” “, $post->post_title));
// Aggiorna il metadato, il nome del campo lo devi scoprire tu o te ne crei uno ex novo non serve ACF
update_post_meta( $post->ID, ‘INOMEDELCUSTOMFIELD’, $numero_parole );
}
PS cercare di scrivere codice senza conoscere il PHP è come provare a fare un muro senza sapere come fare il cemento
Grazie del supporto.
Proverò appena possibile.
Per il muro…..hai perfettamente ragione ma lo faccio a tempo perso e c’è un mondo infinito di cose da imparare…
ciao
30 Aprile 2017 alle 16:14 in risposta a: Risultati di ricerca in ordine di numero di parole del post_title #121589UgoPartecipante1) post nuovi intercetti la action https://codex.wordpress.org/Post_Status_Transitions e quando passa in publish aggiorni il campo con il numero di parole del titolo
2) per i post esistenti direi che fai una semplice pagina con un tempalte ad hoc che conterrà solo del codice php che cerca tutti i post che NON hanno il campo impostato ne processa un po’ aggiungendo il campo col numero di parole e si ferma (potrebbe nonr iuscire ad aggiornare tutti i 2000 post in una volta solo, lo lanci manualmente un po’ di volte sino a che non hai più articoli senza il campo. Nel code sotto la wp_query vedrai che è possibile cercare anche post nei quali NON esiste un dato metadato.
Grazie molte del consiglio.
Ho provato un po’ ma devo dire che sono in difficoltà .
Conosco relativamente poco il PHP ed ogni volta inciampo in errori….
1) probabilemnte sbaglio qualcosa nel riscrivere il codice che mi hai indicato integrato con la funzione che ho riportato sopra.
function on_all_status_transitions( $new_status, $old_status, $post ) {
if ( $new_status != $old_status ) {
// A function to perform actions any time any post changes status.
/* Attiva l’output buffering */
ob_start();
/* Ottieni il contenuto del titolo */
the_title();
/* Preleva e ripulisci il contenuto del buffer */
$numero_parole= ob_get_clean();
/* Calcola il numero delle parole*/
return sizeof(explode(” “, $numero_parole));
}
}
add_action( ‘transition_post_status’, ‘on_all_status_transitions’, 10, 3 );
2) credo che ci proverò quando avrò risolto il 1)
puoi darmi una mano?
Grazie ancora
-
AutorePost