if ( have_posts() ) : while ( have_posts() ) : the_post();
the_title();
endwhile; else :
echo "Nessun post";
endif;
Funzione have_posts() controlla se ci sono i post pubblicati. the_post() imposta variabile globale del post cosi che le functions di post hanno datti del post che si cicla. ( qui si tratta per esempio della funzione the _title() che abbia il titolo del post corrente nel cilco)
Se dovessimo ciclare CPT (Custom Post Type):
global $post;
$query = new WP_Query( [
'posts_per_page' => 5,
'post_type' => 'page'
] );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_title();
}
} else {
echo "Nessun post";
}
wp_reset_postdata();
Come in wordpress fare la ricerca con WP_QUERY per detterminate record del custom post, dove record posso essere anche metabox?
Esempio: Supponiamo che il tuo custom post type sia corsi e abbia una meta box con chiave insegnante.
$args = array(
'post_type' => 'corsi',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'insegnante',
'value' => 'Mario Rossi',
'compare' => '='
)
)
);
$query = new WP_Query($args);
Se LIKE:
$args = array(
'post_type' => 'corsi',
'meta_query' => array(
array(
'key' => 'insegnante',
'value' => 'Mario',
'compare' => 'LIKE'
)
)
);
Ad esempio vuoi cercare "yoga" nel titolo, contenuto e "Mario" nella meta box.
$args = array(
'post_type' => 'corsi',
's' => 'yoga',
'meta_query' => array(
array(
'key' => 'insegnante',
'value' => 'Mario',
'compare' => 'LIKE'
)
)
);
Quindi:
Se il dato è nel titolo, contenuto o excerpt, usi il parametro s.
Se il dato è in una meta box (cioè un campo personalizzato salvato in wp_postmeta (DB)), usi meta_query.