Combinazione di query con argomenti diversi per tipo di post
2 risposta
- voti
-
- 2012-11-05
Unmodo èpersonalizzare la query SQLeseguita utilizzando
posts_clauses
o altrifiltri simili.Pertrovarli,cercaposts_clauses
in "wp-includes/query.php" & amp;vedere la serie difiltri appenaprima di questa riga.Insieme sonoin grado dipersonalizzare qualsiasiparte della queryUn'altra cosa chepuoifare è uniremanualmentei postinterrogatinegli oggetti
$photos_query = new WP_Query( $photos ); $quotes_query = new WP_Query( $quotes ); $result = new WP_Query(); // start putting the contents in the new object $result->posts = array_merge( $photos_query->posts, $quotes_query->posts ); // here you might wanna apply some sort of sorting on $result->posts // we also need to set post count correctly so as to enable the looping $result->post_count = count( $result->posts );
One way is to customize the SQL query executed using
posts_clauses
or other such filters. To find them search forposts_clauses
in "wp-includes/query.php" & see the series of filters just before this line. These together are capable of customizing any part of the queryAnother thing you can do is manually merge the queried posts in the objects
$photos_query = new WP_Query( $photos ); $quotes_query = new WP_Query( $quotes ); $result = new WP_Query(); // start putting the contents in the new object $result->posts = array_merge( $photos_query->posts, $quotes_query->posts ); // here you might wanna apply some sort of sorting on $result->posts // we also need to set post count correctly so as to enable the looping $result->post_count = count( $result->posts );
-
Latua seconda soluzione (senza SQL) hafunzionato!Ora hoil controllo completo su cosa sta succedendonella queryfinaleprima dientrarenel ciclo.Grazieper l'aiuto!Your second solution (without the SQL) did the trick! Now I have complete control over what is going into that final query before going into the loop. Thanks for your help!
- 0
- 2012-11-05
- Andy Merskin
-
Ilprimo è difficilemapiùefficiente (nel secondo ci sono ancora 2 query al database).Direi che dipende dallepreferenzepersonaliThe first one is difficult but more efficient(in second there are still 2 database queries). I would say it comes down to personal preference
- 1
- 2012-11-05
- Mridul Aggarwal
-
Sareiestremamenteinteressato a unmodoper realizzare laprima soluzione!Ifiltrinecessari,ecc. Questo richiede una `UNION` di qualchetiponello sqlper ognipost_type?Would be extremely interested in a way to accomplish the first solution! The filters needed, etc. Does this call for a `UNION` of some sort in the sql for each post_type?
- 0
- 2017-10-27
- Solomon Closson
-
@SolomonClosson questofiltropuò aiutare- https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses@SolomonClosson this filter can help- https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses
- 0
- 2017-10-30
- Mridul Aggarwal
-
- 2013-10-28
@mridual aggarwal latua risposta èmoltobuonama sfortunatamentenon combina veramentei 2
wp_query
mostra soloi post dientrambiin ordine,intendo 5post dalprimo & amp; 5 dal secondomanon ordinatotuttoin uno,quindi ho questa soluzione & amp; ha raggiuntoesattamente l'obiettivoperme almeno<?php $term = get_term_by( 'slug', get_query_var( 'tag' ), "post_tag" ); $tagslug = $term->slug; $post_types = get_post_types('','names'); ?> <?php //first query $blogposts = get_posts(array( 'tag' => $tagslug, //first taxonomy 'post_type' => $post_types, 'post_status' => 'publish', )); //second query $authorposts = get_posts(array( 'bookauthor' => $tagslug, //second taxonomy 'post_type' => $post_types, 'post_status' => 'publish', )); $mergedposts = array_merge( $blogposts, $authorposts ); //combine queries $postids = array(); foreach( $mergedposts as $item ) { $postids[]=$item->ID; //create a new query only of the post ids } $uniqueposts = array_unique($postids); //remove duplicate post ids $posts = get_posts(array( //new query of only the unique post ids on the merged queries from above 'post__in' => $uniqueposts, 'post_type' => $post_types, 'post_status' => 'publish', )); foreach( $posts as $post ) : setup_postdata($post); ?> // posts layout <?php endforeach; ?> <?php wp_reset_postdata();?>
@mridual aggarwal your answer is very very good but unfortuntly it is not really combining the 2
wp_query
it is only shows the posts from both in arrange i mean 5 posts from the first & 5 from the second but not sorted all in one so i have this solution & it exactly achieved the goal for my self at least<?php $term = get_term_by( 'slug', get_query_var( 'tag' ), "post_tag" ); $tagslug = $term->slug; $post_types = get_post_types('','names'); ?> <?php //first query $blogposts = get_posts(array( 'tag' => $tagslug, //first taxonomy 'post_type' => $post_types, 'post_status' => 'publish', )); //second query $authorposts = get_posts(array( 'bookauthor' => $tagslug, //second taxonomy 'post_type' => $post_types, 'post_status' => 'publish', )); $mergedposts = array_merge( $blogposts, $authorposts ); //combine queries $postids = array(); foreach( $mergedposts as $item ) { $postids[]=$item->ID; //create a new query only of the post ids } $uniqueposts = array_unique($postids); //remove duplicate post ids $posts = get_posts(array( //new query of only the unique post ids on the merged queries from above 'post__in' => $uniqueposts, 'post_type' => $post_types, 'post_status' => 'publish', )); foreach( $posts as $post ) : setup_postdata($post); ?> // posts layout <?php endforeach; ?> <?php wp_reset_postdata();?>
Sto creando una sezione su un sitoin cui unisco due diversitipi dipost in un unico cicloe poi li visualizzoin modo casuale. Ilproblema è che ho difficoltà atrovare unmodoper limitareilnumero dipost per tipo.
Ecco cosa hoprovato:
Una query conpiùtipi dipostpuòessere ottenuta con un array:
...manonpuò limitarsi a un certonumero dipostpertipo.
Unione di due array di argomenti della queryprima dieseguire WP_Query su diesso:
Nessunafortuna su questo. Quello che succede è che l'ultima variabile
$quotes
sovrascrive$photos
e mostra solo le virgolette.Unione di due oggetti WP_Query tramitetypecasting:
...e così via.
Probabilmentepotrei usare una query SQL direttamentenel database,ma hobisogno diesserein grado di combinare questi duetipi dipost separatiper un ciclo,organizzatoin modo casuale E limitato a una certa quantità dipostpertipo.
Grazieperiltuo aiuto!