post_status => publish non funziona
Ho un modulo frontend che permette agli utenti di inviare un post.
Questo è il modo in cui memorizzo i dati quando viene inviato un post:
if ( isset( $_POST['submitted'] )) {
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'publish'
);
$new_post = wp_insert_post( $post_information );
Il post non viene mostrato nella mia pagina dei post a meno che non vada nella dashboard e clicchi sul pulsante AGGIORNA.
QUESTO È IL MODO IN CUI ESEGUO LA QUERY DEI MIEI POST:
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
'meta_query' => array(
array( 'key' => '_wti_like_count','value' => 5, 'compare' => '<=','type' => 'numeric')
)
);
query_posts( $args );
Come posso fare in modo che i post inviati vengano pubblicati automaticamente?
Il post viene aggiunto e pubblicato, ma poiché hai la meta query e la meta key non viene aggiunta quando invii il post dal frontend, non viene visualizzato. Utilizza il seguente codice che aggiunge i metadati come richiesto.
if ( isset( $_POST['submitted'] ) ) {
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'publish'
);
$new_post = wp_insert_post( $post_information );
// Aggiungi i metadati del post
add_post_meta( $new_post, '_wti_like_count', 0, true );
add_post_meta( $new_post, '_wti_unlike_count', 0, true );
add_post_meta( $new_post, '_wti_total_count', 0, true );
}

Un'altra piccola cosa, amico mio! Se aggiungo un valore così add_post_meta( $new_post, '_wti_like_count', 1, true ); perché il valore 1 non viene visualizzato sul front end? Mostra solo 0

I dati che vedi sul frontend vengono estratti dalla tabella del plugin wti_like_post
. I meta dati vengono semplicemente aggiunti al post in modo che possano essere utilizzati in query personalizzate come quella che hai sopra. Per mantenere le cose pulite, viene sempre aggiunto 0 al post quando viene creato. Man mano che gli utenti votano per quel post, entrambi i valori (nella tabella del plugin e nei meta dati) vengono aggiornati di conseguenza.

Nota che ora puoi farlo all'interno di wp_insert_post usando l'attributo 'meta_input' (ad esempio meta_input => array('meta_field_name => 'meta field value')
)

Non sono sicuro, ma penso che sia perché non hai aggiunto l'autore nell'array $post_information
:
if( isset($_POST['submitted']) ):
global $user_ID;
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'publish',
'post_author' => $user_ID,
'post_date' => date('Y-m-d H:i:s')
);
$post_id = wp_insert_post($post_information);
if (!$post_id) {
wp_die('Errore');
}
endif;
