WP_Query che mostra TUTTI i post
Non riesco a capire perché il mio WP_Query
mostra sempre tutti i post pubblicati indipendentemente da ciò che inserisco negli argomenti.
<?php
$args = array('numberposts' => 1,
'meta_key' => 'display',
'meta_value' => 'about'
);
$about_preview_query = new WP_Query($args);
if ($about_preview_query->have_posts()) {
print "<h1>POST TROVATI</h1>";
}
while ($about_preview_query->have_posts()) {
$about_preview_query->the_post();
print "<h1>";
the_title();
print "</h1>";
}
?>
Sto facendo qualcosa di sbagliato? Dopo aver letto la documentazione su WP_Query()
non riesco a capire dove sbaglio, apprezzo qualsiasi aiuto possibile.
Aggiornamento
Ho provato questo codice e sto ancora ottenendo la stessa risposta. Tutti i post vengono restituiti nel loop.
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'display',
'value' => 'about',
'compare' => '=',
)
),
);
Aggiornamento 2
Sembra che se stampo $about_preview_query->found_posts
l'output sia 1. Quindi sospetto ci sia qualcosa che non va in come sto facendo il loop dei post:
<?php if ($about_preview_query->have_posts()): ?>
<h1>Ha <?php print $about_preview_query->found_posts ?> Post</h1>
<?php while ($about_preview_query->have_posts()): $about_preview_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>
È perché stai fornendo argomenti errati. Fornisci gli argomenti corretti per WP_Query()
.
Dai anche un'occhiata ai parametri dei campi personalizzati
Un'altra cosa, dovrai usare un codice come questo:
$args = array(
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'display',
'value' => 'about',
'compare' => '=', // (Leggi tutti gli operatori di confronto al link fornito)
),
),
);

Non esiste il parametro numberposts
. Usa invece posts_per_page
. Quindi il tuo codice diventerà...
<?php
$args = array(
'posts_per_page' => 1,
'meta_key' => 'display',
'meta_value' => 'about'
);
$about_preview_query = new WP_Query($args);
if ( $about_preview_query->have_posts() ) {
print "<h1>POST TROVATI</h1>";
}
while ( $about_preview_query->have_posts() ) {
$about_preview_query->the_post();
print "<h1>";
the_title();
print "</h1>";
}
?>
