Home › Forum › Problemi Vari con WP › Risultati di ricerca in ordine di numero di parole del post_title
-
AutorePost
-
-
30 Aprile 2017 alle 7:41 #31393UgoPartecipante
Buongiorno
Vi chiedo un aiuto per capire come sviluppare la ricerca secondo le mie necessità .
Esempio:
prodotti esistenti (post_title)
Mela verde
Torta di mela
Mela per torta
Mela
Mestolo per mescolare la mela e fare una torta
Ananas e Mela
Melatonina
se io cerco Mela il risultato dovrebbe essere (ordine crescente):
Mela
Melatonina
Mela verde
Torta di mela
Mela per torta
Ananas e Mela
Mestolo per mescolare la mela e fare una torta
se invece cerco Torta Mela troverò:
Torta di mela
Mela per torta
Mestolo per mescolare la mela e fare una torta
La ricerca standard di WP propone un ORDERBY in cui si può inserire diverse possibilità .
Ho settato la ricerca in modo da eseguirla solo sul post_title.
Inoltre ho sviluppato diversi campi personalizzati con ACF.
Per fare in modo di avere i risultati elencati per numero di parole presenti su post_title, pensavo di:
– utilizzare un nuovo campo ACF chiamato “numero_parole”
– inserire la funzione che scrive tale numero dopo averlo letto dallo stesso post_title (vedere funzione sotto).
– settare ORDERBY con il meta-value “numero_parole”
Ho trovato il seguente codice per il calcolo del numero di parole:
‘
// conta il numero di parole del post_title
function post_word_count(){
/* Attiva l’output buffering */
ob_start();
/* Ottieni il contenuto del titolo */
the_title();
/* Preleva e ripulisci il contenuto del buffer */
$content = ob_get_clean();
/* Calcola il numero delle parole*/
return sizeof(explode(” “, $content));
}
‘
Inserendo tale funzione su functions.php si ottiene facilmente il numero di parole del post_title tramite il codice
<?=post_word_count()?>
Ora però non riesco a fare in modo che WP scriva tale numero sul campo ACF “numero_parole”.
Credo che tale procedura dovrebbe essere fatta nel momento in cui si aggiorna/pubblica il post.
Ad ora ho circa 2500 post che dovrei aggiornare in tal senso.
Qulacuno può aitarmi?
Grazie 1000
-
30 Aprile 2017 alle 10:58 #121583SteveAglAmministratore del forum
1) 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.
-
30 Aprile 2017 alle 16:14 #121589UgoPartecipante
1) 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
-
30 Aprile 2017 alle 18:59 #121590SteveAglAmministratore del forum
tutto 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
-
1 Maggio 2017 alle 7:13 #121592UgoPartecipante
tutto 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
-
9 Maggio 2017 alle 4:18 #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
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.