Escludi l'ID del post da wp_query
4 risposta
- voti
-
- 2012-09-14
Suppongo chefossepesante,maper rispondere allatua domanda originale,ho raccoltotuttigli ID deipostin un arraynelprimo cicloedescluso queipost dal secondo ciclo usando 'post__not_in' che si aspetta un array di IDpost
<?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; ?>
Ilprimo ciclomostratuttii postin una categoriae raccogliegli ID deipostin un array.
Il secondo ciclomostratuttii post,esclusii post delprimo ciclo.
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.
-
In un'altranota,c'è unmodoper aggiungere wp-pagenavi alla seconda query?On another note, Is there a way to add wp-pagenavi to the 2nd query?
- 0
- 2012-09-15
- Dean Elliott
-
Nel casoin cui rivedessi latua risposta: correggiilmarkup/intending del codice.Grazie.In case you ever revisit your answer: Please fix your code markup/intending. Thanks.
- 1
- 2014-10-12
- kaiser
-
- 2013-05-10
Ilparametro che stai cercando è
post__not_in
(kaiser ha unerrore dibattituranella sua risposta).Quindiil codicepotrebbeessere:<?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;
-
Sai,ci sono [modifica]per correggereglierrori dibattitura :)You know, there are [edit]s for correcting typos :)
- 3
- 2014-10-12
- kaiser
-
@Ziki,la virgolanell'arraynon è unerrore dibattitura è una sintassi PHP valida,se è questo cheintendi.@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 -no,originariamente aveva "posts__not_in"invece di "post__not_in",guarda la cronologia della sua risposta.Il coma vabene@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
Devi definire l'argomento
post__not_in
come array.Ancheper un singolo valore.Eperfavorenon sovrascrivere le variabiliprincipaliglobali conelementitemporanei.<?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
Codici alternativi;
Escludipost di 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 )); } }
Rimuovii post dallapagina della homepage
<?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)); } }
Comepossoescludere unpost specifico da una query WP_Query?(Adesempio,mostratuttii posttranne unpost con ID 278)
Hoprovato l'argomentopost__not_inma rimuove solotuttii messaggi ..
Qualsiasi aiuto sarebbefantastico.
Ecco lamia domanda attuale
Grazie