Title: Advanced Query
Last modified: August 19, 2016

---

# Advanced Query

 *  Resolved [Nith](https://wordpress.org/support/users/nith/)
 * (@nith)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/)
 * Hello,
 * I have the following code which gets 2 posts from 3 different categories and 
   displays them:
 *     ```
       $taxonomy = 'category';
       						$param_type = 'category__in';
       						$term_args=array(
       						  'include'=> '3,4,11',
       						  'orderby' => 'name',
       						  'order' => 'ASC'
       						);
       						$terms = get_terms($taxonomy,$term_args);
       						if ($terms) {
       							foreach( $terms as $term ) {
       								$args=array(
       									"$param_type" => array($term->term_id),
       									'post_type' => 'post',
       									'post_status' => 'publish',
       									'posts_per_page' => 2,
       									'caller_get_posts'=> 1
       									);
       								$my_query = null;
       								$my_query = new WP_Query($args);
       								if( $my_query->have_posts() ) {
       									echo 'List of Posts in '.$taxonomy .' '.$term->name;
       									while ($my_query->have_posts()) : $my_query->the_post(); ?>
       ```
   
 * However, what I need to do is have the posts separated somehow so that I have
   2 posts in each
    -  element.
    - How would I go about doing this?
    - Thanks in advance.

Viewing 14 replies - 1 through 14 (of 14 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1531994)
 * The format of this post is whacked. You should start a new thread and try to 
   figure out what caused the problem.
    Please explain what you mean by ‘2 posts
   in each element’. What is an element?
 * If I am reading the code correctly, what you are getting is:
 * List of Posts in category category_name_1
    Post 1 in category_name_1 Post 2 in
   category_name_1 List of Posts in category category_name_2 Post 1 in category_name_2…
 * What do you want to get?
 * EDIT: I see that you want two posts in each `<li>` element. An un-backticked `
   <li>` is messing up the format.
 *  Thread Starter [Nith](https://wordpress.org/support/users/nith/)
 * (@nith)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532222)
 * Yeah, I want to be able to separate the posts returned into different `<li>` 
   elements.
 * How is this achievable?
 * Thanks again.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532224)
 * Please post a few more lines (maybe 10 or 15) after `if ( $my_query->have_posts()){`
   so I can tell more what you are doing for each post.
 *  Thread Starter [Nith](https://wordpress.org/support/users/nith/)
 * (@nith)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532241)
 * Ok, below is a bit more code.
 * Basically, I have a ‘slider’ system which uses jQuery. And each `<li>` item is
   each ‘slide’ so to speak.
 * If you go to nickythorne.com you’ll see what I mean by ‘slider’. I want to list
   a number of posts on each slide. This is where the scope of my knowledge with
   WordPress ends 🙂
 * See the commented bit.
 *     ```
       <div id="slider">
       	<ul>
   
       		<?php
       			$taxonomy = 'category';
       			$param_type = 'category__in';
       			$term_args=array(
       				'include'=> '3,4,11',
       				'orderby' => 'name',
       				'order' => 'ASC'
       			);
       			$terms = get_terms($taxonomy,$term_args);
       			if ($terms) {
       				foreach( $terms as $term ) {
       					$args=array(
       						"$param_type" => array($term->term_id),
       						'post_type' => 'post',
       						'post_status' => 'publish',
       						'posts_per_page' => 2,
       						'caller_get_posts'=> 1
       					);
       					$my_query = null;
       					$my_query = new WP_Query($args);
       					if( $my_query->have_posts() ) {
       						echo 'List of Posts in '.$taxonomy .' '.$term->name;
       						while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       							<li>
   
       								<div class="post-details">
   
       									<!-- It's here where i want the code which will display 5 posts
       										 So as it's a while loop, the <li> element will contain 5 posts in each slide -->
   
       								</div>
   
       							</li>
   
       					    <?php
       						endwhile;
   
       						}
       				}
       			}
   
       			wp_reset_query();?>
   
       	</ul>
       </div>
       ```
   
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532243)
 * I am not quite sure this is what you want, but it is worth a try. Change the ‘
   posts_per_page’ to 5 if the comment in the while loop is correct, and move the`
   <li> and </li>` tags outside the while loop:
 *     ```
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
          echo 'List of Posts in '.$taxonomy .' '.$term->name; ?>
          <li>
          <?phpwhile ($my_query->have_posts()) : $my_query->the_post(); ?>
   
             <div class="post-details">
   
                <!-- It's here where i want the code which will display 5 posts
                     So as it's a while loop, the <li> element will contain 5 posts in each slide -->
   
             </div>
   
          <?php endwhile; ?>
          </li>
       ```
   
 *  Thread Starter [Nith](https://wordpress.org/support/users/nith/)
 * (@nith)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532244)
 * Not quite.
 * Lets say I want to display 15 posts altogether, then the ‘posts_per_page’ would
   be 15.
 * _Below is the functionality I’m looking for _
 * This would mean that i’d have 3 slides or `<li>` elements with 5 posts in each(
   making up the 15 altogether).
 * I need the existing code to be there as I only want to select the posts from 
   certain categories.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532245)
 * I couldn’t post the complete code – the moderators will probably delete it if
   it gets too long. What I suggested will display 5 posts in each li tag for each
   of the categories 3,4, and 11.
 * Don’t eliminate the code you already have, just modify it by moving the li tags
   outside the while loop, and set the posts_per_page to the number of posts to 
   display in each category.
 *  Thread Starter [Nith](https://wordpress.org/support/users/nith/)
 * (@nith)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532248)
 * Oh right.
 * I have set the ‘_posts\_per\_page_‘ to 2 to test this and after trying what you
   said, only the first two posts are showing in each `<li>` element for some reason.
   The other posts in the category are not getting displayed (there are other posts
   in the category). [http://www.nickythorne.com/pages/blog/](http://www.nickythorne.com/pages/blog/)
 * I’ve pastebin’d the code: [http://pastebin.com/j38kzsq1](http://pastebin.com/j38kzsq1)
 * Thanks for your help.
 *  Thread Starter [Nith](https://wordpress.org/support/users/nith/)
 * (@nith)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532251)
 * Yeah, as I first thought, because I’ve moved the `<li>` tags outside the while
   loop, they are not getting duplicated (there is only ever one).
 * This is where I was running into difficulty in the beginning. The `<li>` needs
   to be inside the while loop in order to get replicated, but the issue is displaying
   the ‘_post\_per\_page_‘ in each `<li>`.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532257)
 * Something just isn’t adding up. In the code I see a line `echo 'List of Posts
   in '.$taxonomy .' '.$term->name; ?>`, but I don’t see that text on the page. 
   What am I missing.
 * As I read the code, you should get the query executed 3 times, once for each 
   category 3,4,11. If you have posts_per_page set to 2, and there are 2 or more
   posts in each category, and the li tags are outside the while loop, you should
   see this on the page:
 *     ```
       List of Posts in category name1
       <li>
       post 1 in category name1
       post 2 in category name1
       </li>
       List of Posts in category name2
       <li>
       post 1 in category name2
       post 2 in category name2
       </li>
       List of Posts in category name3
       <li>
       post 1 in category name3
       post 2 in category name3
       </li>
       ```
   
 * Is that what you want?
 *  Thread Starter [Nith](https://wordpress.org/support/users/nith/)
 * (@nith)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532264)
 * Oops. Yeah, I forgot to update the original code.
 * I’ve removed the `echo 'List of Posts in '.$taxonomy .' '.$term->name; ?>` line
   and also change line `'include'=> '3,4,11',` to `'include'=> '3',` to only return
   the posts in the ‘blog’ category.
 * So, I’m aiming in having the output be:
 *     ```
       <li>
       post 1 in the category 'blog'
       post 2 in the category 'blog'
       </li>
       <li>
       post 3 in the category 'blog'
       post 4 in the category 'blog'
       </li>
       <li>
       post 5 in the category 'blog'
       post 6 in the category 'blog'
       </li>
       ```
   
 * But it’s not happening. I’m guessing it’s because the ‘posts_per_page’ is set
   to 2, but removing that will just display all the posts in one `<li>` element
   right? And I want to ‘split’ the posts and have two of the posts in each element(
   like the output above).
 * Sorry for the confusion, my bad.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532267)
 * OK, change posts_per_page to the number you want for each category and try this:
 *     ```
       if( $my_query->have_posts() ) {
          $count = 0; ?>
          <li>
          <?phpwhile ($my_query->have_posts()) : $my_query->the_post(); ?>
             if ( ++$count > 1 && ($count - 1) % 2 == 0 ) echo '</li><li>' ; ?>
   
             <div class="post-details">
   
                <!-- It's here where i want the code which will display 5 posts
                     So as it's a while loop, the <li> element will contain 5 posts in each slide -->
   
             </div>
   
          <?php endwhile; ?>
          </li>
       }
       ```
   
 *  Thread Starter [Nith](https://wordpress.org/support/users/nith/)
 * (@nith)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532273)
 * Works like a charm 🙂
 * Thanks so much for your help/patience, appreciate it.
 * All the best.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532274)
 * You are welcome!

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Advanced Query’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [16 years ago](https://wordpress.org/support/topic/advanced-query/#post-1532274)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
