J'essaie d'obtenir seulement un message et celui-ci doit être le dernier . Voici donc ce que je fais:
$basic_args = $wp_query->query_vars;
$basic_args['tax_query'] = $wp_query->tax_query;
$aditional_args = array(
'post__in' => get_option('sticky_posts'),
'ignore_sticky_posts' => 1,
'order' => 'desc',
'post_status' => 'publish',
'posts_per_page' => 1
);
$normal_args = array_merge($basic_args, $aditional_args);
$normal_query = new WP_Query($normal_args);
$prev_post_ids = array();
if (!$normal_query->have_posts()) {
$normal_args = array_merge($basic_args, array('posts_per_page' => 1));
$normal_query = new WP_Query($normal_args);
}
echo $normal_query->post_count; // returns 25
if ($normal_query->have_posts()) {
while ($normal_query->have_posts()) {
....
// here I got 25 results instead of just one
}
}
Je sais que posts_per_page
devrait limiter le nombre de messages que je vais recevoir mais ne fonctionne pas pour moi et ne sait pas pourquoi. Comme vous pouvez le lire, je reçois 25 messages au lieu de un. Quelqu'un peut-il me donner des conseils sur ce sujet? J'ai lu ceci et ceci mais ne m'aide pas du tout.
Update1
Je débogue le $normal_query->request
comme suggéré par @ pieter-goosen et je reçois cette erreur requête SQL:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (6) AND tt1.term_taxonomy_id IN (6) ) AND wp_posts.post_type IN ('opinion', 'post', 'especiales') AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 25
Où la LIMIT
est modifiée?
Update2
J'ai aussi débogué le $basic_args
qui a $wp_query->query_vars
et aussi $normal_args
et voici le résultat:
echo '<pre>';
print_r($basic_args);
echo '</pre>';
Array
(
[category_name] => internacionales
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[tag] =>
[cat] => 409
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[post_type] =>
[posts_per_page] => 10
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
[0] => Array
(
[taxonomy] => category
[terms] => Array
(
[0] => internacionales
)
[field] => slug
[operator] => IN
[include_children] => 1
)
)
[relation] => AND
[table_aliases:protected] => Array
(
[0] => wp_term_relationships
)
[queried_terms] => Array
(
[category] => Array
(
[terms] => Array
(
[0] => internacionales
)
[field] => slug
)
)
[primary_table] => wp_posts
[primary_id_column] => ID
)
)
echo '<pre>';
print_r($normal_args);
echo '</pre>';
Array
(
[category_name] => internacionales
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[tag] =>
[cat] => 409
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[ignore_sticky_posts] => 1
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[post_type] =>
[posts_per_page] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => desc
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
[0] => Array
(
[taxonomy] => category
[terms] => Array
(
[0] => internacionales
)
[field] => slug
[operator] => IN
[include_children] => 1
)
)
[relation] => AND
[table_aliases:protected] => Array
(
[0] => wp_term_relationships
)
[queried_terms] => Array
(
[category] => Array
(
[terms] => Array
(
[0] => internacionales
)
[field] => slug
)
)
[primary_table] => wp_posts
[primary_id_column] => ID
)
[post_status] => publish
)
Comme vous pouvez le constater, post_per_page
est défini sur 1
ici, alors pourquoi utiliser ces 25?
Mise à jour 3
Apparemment, c'est le code problématique à functions.php
:
function change_wp_search_size($query)
{
if ($query->is_search) {
$query->query_vars['posts_per_page'] = 25;
} // Change 25 to the number of posts you would like to show
return $query; // Return our modified query variables
}
add_filter('pre_get_posts', 'change_wp_search_size');
Cela ne devrait-il pas fonctionner uniquement pour la recherche? Pourquoi agit-on sur tous les query_vars tout le temps?
pre_get_posts
modifie toutes les requêtes, back end et front end, peu importe.
Il y a deux contrôles très importants qui manquent à un grand nombre de personnes, à savoir le contrôle !is_admin()
qui cible uniquement le front-end, puis le contrôle le plus important, is_main_query()
, qui modifiera uniquement la requête principale et non les requêtes personnalisées.
Votre déclaration devrait ressembler à quelque chose comme ce qui suit
if ( !is_admin()
&& $query->is_main_query()
&& $query->is_search()
) {
// Your arguments to set
}
Ok, après les suggestions de @ pieter-goosen , j'ai trouvé une solution, en ajoutant simplement cette remove_all_filters( 'pre_get_posts' )
avant d'exécuter que WP_Query
corrige le problème, j'espère que cela aide à autres