Title: Multiple $Get_Posts?
Last modified: August 20, 2016

---

# Multiple $Get_Posts?

 *  [ianbee](https://wordpress.org/support/users/ianbee/)
 * (@ianbee)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/multiple-get_posts/)
 * I have this plugin in WordPress that I’ve heavily modified. The purpose of the
   plugin is originally to display thumbnails for whatever category you tell it 
   to. As of right now, I’ve made it done much more than that. But anyways, here
   is the shortcode for the plugin..
 * `[categorythumbnaillist 7]`
    7 being the category ID of course. The plugin gets
   the posts for whatever category and orders them using this code that I made:
 *     ```
       $args=array(
                 'showposts'=>5,
       	  'category'=> $listCatId[1],
       	  'orderby'=> $categoryThumbnailList_OrderType,
       	  'order'=> $categoryThumbnailList_Order
   
               );
       	$myposts = get_posts( $args);
       ```
   
 * Now, I wanted the plugin to only show posts if it has has the tag “news” in it.
   So I did the news tag in like so:
 *     ```
       $args=array(
                 'tag' => 'news',
                 'showposts'=>5,
       	  'category'=> $listCatId[1],
       	  'orderby'=> $categoryThumbnailList_OrderType,
       	  'order'=> $categoryThumbnailList_Order
   
               );
       	$myposts = get_posts( $args);
       ```
   
 * $myposts = get_posts( $args );
    This sucesfully displays 5 posts if it has the“
   news” tag in it. But here is the problem…
 * I’m going to be using this plugin multiple times on one page. So when I use the
   shortcode listed above with a different category ID, the plugin doesn’t display
   posts for the respective category since there are no posts tagged with “news”
   other than in the news category. 🙁
 * I’m going to be using the plugin with my news category, photos category and audio
   category. I would like it to display each categories thumbnails via shortcode(
   like the plugin is intended to do) but also only display the news posts with 
   the tags “news”… for only the news category. How would I make it function properly
   and display only news posts with the tag “news” while still displaying the other
   category posts properly… all via shortcode like the plugin is supposed to do?
   So for example… I’d have..
 *     ```
       [categorythumbnaillist 3] (news category)
       [categorythumbnaillist 5] (photos category)
       [categorythumbnaillist 7] (audio category)
       ```
   
 * I’d like news to only display news posts with the tag “news”, photos to display
   the photos category posts and audio to display the audio category posts. Again,
   I’ve already figured out how to do one or the other, I just don’t know how to
   make the code do both.
 * Any help would truely be appreciated!!! 🙂

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

 *  Thread Starter [ianbee](https://wordpress.org/support/users/ianbee/)
 * (@ianbee)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/multiple-get_posts/#post-3234802)
 * I tried making two different functions for get_posts and then running an if /
   else statement like so…
 *     ```
       $args=array(
                 'tag' => 'news',
                 'showposts'=>5,
       	  'category'=> $listCatId[1],
       	  'orderby'=> $categoryThumbnailList_OrderType,
       	  'order'=> $categoryThumbnailList_Order
               );
   
       	$targs=array(
                 'showposts'=>5,
       	  'category'=> $listCatId[1],
       	  'orderby'=> $categoryThumbnailList_OrderType,
       	  'order'=> $categoryThumbnailList_Order
   
               );
   
       if ( tag == true ) {
       	$myposts = get_posts( $args);
       }
   
       else {
       	$myposts = get_posts( $targs);
       }
       ```
   
 * This sucesfully filtered the news posts to only show posts tagged with “news”
   in them.. but then it doesn’t get posts for any other categories. 🙁
 * Again… any help would be appreciated sooo much! 😀
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/multiple-get_posts/#post-3234825)
 * You’re on the right track with your if/else logic, but your conditional check
   of `tag == true` is bad syntax. What is ‘tag’, do you mean ‘$tag’? Where/how 
   is this set?. You should be getting the category id passed to your shortcode 
   handler and using that for a conditional test.
 *  Thread Starter [ianbee](https://wordpress.org/support/users/ianbee/)
 * (@ianbee)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/multiple-get_posts/#post-3234828)
 * Hey man, thanks for you help I really appreciate it… the following codes have
   actually filtered out the posts with the tag “news” successfully, but doesn’t
   show the other posts in different categories.
 *     ```
       if ( $tag = news ) {
       	$myposts = get_posts( $args);
       }
   
       else {
       	$myposts = get_posts( $targs);
       }
       ```
   
 * and this one works aswell suprisignly:
 *     ```
       if ( tag == true) {
       	$myposts = get_posts( $args);
       }
   
       else {
       	$myposts = get_posts( $targs);
       }
       ```
   
 * If I do something like:
 *     ```
       if ( $tag ) {
       	$myposts = get_posts( $args);
       }
   
       else {
       	$myposts = get_posts( $targs);
       }
       ```
   
 * It shows all the posts for every category but doesn’t filter out the news posts.
   I think its because I’m not defining a tag so it uses get_posts ($targs) where
   targs doesn’t filter out any posts.
 * I’ve tried using this code, but I’ll tell you why it isn’t working..
 *     ```
       if ( is_category('news') ) {
       	$myposts = get_posts( $args);
       }
   
       else {
       	$myposts = get_posts( $targs);
       }
       ```
   
 * I’ve also tried “in_category”.. but both don’t filter out the news at all because
   the posts aren’t really in the category news. I mean they are technically published
   under the news category but they’re not being displayed thru the news category,
   there being displayed through the plugin. I think that’s why the is_category 
   doesn’t work. lol!
 * But, I need to find a way to tell the plugin if posts are in the news category…
   then filter and only display posts with the tag “news”… and if there not it the
   news category.. just display them regardless of tags.
 * Thanks for the help so far, I think I’m close to figuring this out! Any more 
   help would be awesome! 🙂
 *  Thread Starter [ianbee](https://wordpress.org/support/users/ianbee/)
 * (@ianbee)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/multiple-get_posts/#post-3234829)
 * I tried changing the $args to only show the news category but now for some reason
   all the categories run $args, instead of the news just running $args and everything
   else running $targs… This is my code:
 *     ```
       $args=array(
                 'tag' => 'news',
                 'showposts'=> 5,
       	  'category'=> '3', <- "3" is my news category :)
       	  'orderby'=> $categoryThumbnailList_OrderType,
       	  'order'=> $categoryThumbnailList_Order
               );
   
       	$targs=array(
                 'showposts'=> 5,
       	  'category'=> $listCatId[1],
       	  'orderby'=> $categoryThumbnailList_OrderType,
       	  'order'=> $categoryThumbnailList_Order
   
               );
   
       if ( $tag = news ) {
       	$myposts = get_posts( $args);
       }
   
       else {
       	$myposts = get_posts( $targs);
       }
       ```
   
 *  [Chris](https://wordpress.org/support/users/zenation/)
 * (@zenation)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/multiple-get_posts/#post-3234834)
 * Maybe it’s just a typo but instead of
 *     ```
       if ( $tag = news ) {
       	$myposts = get_posts( $args);
       }
       ```
   
 * try
 *     ```
       if ( $tag == 'news' ) {
       	$myposts = get_posts( $args);
       }
       ```
   

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

The topic ‘Multiple $Get_Posts?’ is closed to new replies.

## Tags

 * [get](https://wordpress.org/support/topic-tag/get/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [multiple](https://wordpress.org/support/topic-tag/multiple/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 5 replies
 * 3 participants
 * Last reply from: [Chris](https://wordpress.org/support/users/zenation/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/multiple-get_posts/#post-3234834)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
