Excluir Post ID de WP_Query
4 respostas
- votos
-
- 2012-09-14
Eu suponho queissofossepesado,maspara responder à suapergunta original,colecioneitodas asidentificações depostagensem umamatriznoprimeiro loope excluía aquelesposts do segundo loop usando 'post__not_in' queespera umamatriz deidentificaçãopós
<?php $args1 = array('category_name' => 'test-cat-1', 'order' => 'ASC'); $q1 = new WP_query($args); if($q1->have_posts()) : $firstPosts = array(); while($q1->have_posts()) : $q1->the_post(); $firstPosts[] = $post->ID; // add post id to array echo '<div class="item">'; echo "<h2>" . get_the_title() . "</h2>"; echo "</div>"; endwhile; endif; /****************************************************************************/ // array of post id's collected in first loop, can now be used as value for the 'post__not_in' parameter in second loops query $args $args2 = array('post__not_in' => $firstPosts, 'order' => 'ASC' ); $q2 = new WP_query($args2); if($q2->have_posts()) : while($q2->have_posts()) : $q2->the_post(); echo '<div class="item">'; echo "<h2>" . get_the_title() . "</h2>"; echo "</div>"; endwhile; endif; ?>
Oprimeiro loopexibetodos ospostsem uma categoriae coleta o ID dopostem umamatriz.
O segundo loopexibetodos osposts,excluindoposts doprimeiro loop.
I suppose this was heavy, but to answer your original question, I've collected all of the posts id's in an array in the first loop, and excluded those posts from the second loop using 'post__not_in' which expects an array of post id's
<?php $args1 = array('category_name' => 'test-cat-1', 'order' => 'ASC'); $q1 = new WP_query($args); if($q1->have_posts()) : $firstPosts = array(); while($q1->have_posts()) : $q1->the_post(); $firstPosts[] = $post->ID; // add post id to array echo '<div class="item">'; echo "<h2>" . get_the_title() . "</h2>"; echo "</div>"; endwhile; endif; /****************************************************************************/ // array of post id's collected in first loop, can now be used as value for the 'post__not_in' parameter in second loops query $args $args2 = array('post__not_in' => $firstPosts, 'order' => 'ASC' ); $q2 = new WP_query($args2); if($q2->have_posts()) : while($q2->have_posts()) : $q2->the_post(); echo '<div class="item">'; echo "<h2>" . get_the_title() . "</h2>"; echo "</div>"; endwhile; endif; ?>
The first loop displays all posts in a category, and collects the post id's into an array.
The second loop displays all posts, excluding posts from the first loop.
-
Em outranota,há umamaneira de adicionar wp-pagenavi à segunda consulta?On another note, Is there a way to add wp-pagenavi to the 2nd query?
- 0
- 2012-09-15
- Dean Elliott
-
Caso vocêjá revisite sua resposta: conserte suamarcação de código/intenção.Obrigado.In case you ever revisit your answer: Please fix your code markup/intending. Thanks.
- 1
- 2014-10-12
- kaiser
-
- 2013-05-10
Oparâmetro que vocêestáprocurando é
post__not_in
(Kaisertem umerro de digitaçãoem sua resposta).Então o códigopode ser como:<?php $my_query = new WP_Query(array( 'post__not_in' => array(278), 'post_type' => 'case-study', 'paged' => $paged, )); while ($my_query->have_posts()) : $my_query->the_post(); endwhile;
The parameter you are looking for is
post__not_in
(kaiser has a typo in his answer). So the code could be like:<?php $my_query = new WP_Query(array( 'post__not_in' => array(278), 'post_type' => 'case-study', 'paged' => $paged, )); while ($my_query->have_posts()) : $my_query->the_post(); endwhile;
-
Você sabe,existem [editar] spara corrigirtypos :)You know, there are [edit]s for correcting typos :)
- 3
- 2014-10-12
- kaiser
-
@Ziki A vírgulanamatriznão é umerro de digitação é válida de sintaxe PHP,se éisso que você quer dizer.@Ziki the comma in the array is not a typo it is valid PHP syntax, if thats what you mean.
- 0
- 2017-06-21
- Leo
-
@leonziyo - Não,ele originalmentetinha lá "Posts__not_in"em vez de "post__not_in",veja a história de sua resposta.Coma ébom@leonziyo - no, he originally had there "posts__not_in" instead of "post__not_in", see history of his answer. Coma is fine
- 1
- 2017-06-22
- Ziki
-
- 2012-09-14
Vocêtem que definir o
post__not_in
arg comomatriz.Mesmopor um único valor.Eporfavor,não sobrescreva as variáveis donúcleoglobal com coisastemporárias.<?php $query = new WP_Query( array( 'post_type' => 'case-study', 'paged' => $paged, 'post__not_in' => array( 1, ), ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // do stuff } // endwhile; } // endif; ?>
You have to define the
post__not_in
arg as array. Even for a single value. And please don't overwrite global core variables with temporary stuff.<?php $query = new WP_Query( array( 'post_type' => 'case-study', 'paged' => $paged, 'post__not_in' => array( 1, ), ) ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); // do stuff } // endwhile; } // endif; ?>
-
- 2019-03-22
códigos alternativos;
excluirposts de categoria
<?php add_action('pre_get_posts', 'exclude_category_posts'); function exclude_category_posts( $query ) { if($query->is_main_query() && $query->is_home()) { $query->set('cat', array( -22, -27 )); } }
Removerpostagens dapágina dapáginainicial
<?php add_action('pre_get_posts', 'wpsites_remove_posts_from_home_page'); function wpsites_remove_posts_from_home_page( $query ) { if($query->is_main_query() && $query->is_home()) { $query->set('category__not_in', array(-1, -11)); } }
Alternative codes;
Exclude category posts
<?php add_action('pre_get_posts', 'exclude_category_posts'); function exclude_category_posts( $query ) { if($query->is_main_query() && $query->is_home()) { $query->set('cat', array( -22, -27 )); } }
Remove posts from homepage page
<?php add_action('pre_get_posts', 'wpsites_remove_posts_from_home_page'); function wpsites_remove_posts_from_home_page( $query ) { if($query->is_main_query() && $query->is_home()) { $query->set('category__not_in', array(-1, -11)); } }
Comopossoexcluir umapostagemespecífica de uma consulta WP_Query?(Porexemplo,mostretodas aspostagens além de umpost com o ID 278)
Eutentei o argumentopost__not_in,mas apenas removetodos osposts ..
Qualquer ajuda seria ótima.
Aquiestá aminha consulta atual
obrigado