Title: Category Shortcode is not an Array
Last modified: August 7, 2017

---

# Category Shortcode is not an Array

 *  [toocoolone](https://wordpress.org/support/users/toocoolone/)
 * (@toocoolone)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/)
 * I didn’t see a solution to this issue in the forum already. But here is what 
   I did when the plugin only showed one category and I asked for a list of categories
   to be displayed…
 * When the shortcode is processed for a list of categories to display, the shortcode
   is pushed into the WP_Query as a string instead of an array. As a result, only
   one category is displayed. For example:
 * `[rpwe cat="4"]`
 * This works as expected and returns the articles from category ID 4. But if we
   list a group of categories:
 * `[rpwe cat="4,6,3,7,9"]`
 * This returns only the first category. When I dove into the code, in functions.
   php on line 252 there is a section that limits posts based on category. This 
   section produces a string value for $query[‘category__in’] which according to
   the codex should be an array. I applied a quick fix by adding a check to see 
   that the $args[‘cat’] is an array and then exploding a comma separated value 
   list if it is not. This is crude, but it works for me. Maybe this issue can be
   addressed better in a future version.
 *     ```
       // Limit posts based on category.
       if ( ! empty( $args['cat'] ) ) {
       	if( ! is_array( $args['cat'] )) {
       		$args['cat'] = explode( ',', $args['cat'] );
       	}
       	$query['category__in'] = $args['cat'];
       }
       ```
   
 * This seems like a bug to me since I should be able to list multiple categories
   and ignore others if I wish. I hope this helps!
 * By the way: To the developer — the code is well written with good comments and
   easy to read…thank you for that! Keep up the good work!

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

 *  [romchik777](https://wordpress.org/support/users/romchik777/)
 * (@romchik777)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-9681107)
 * [@toocoolone](https://wordpress.org/support/users/toocoolone/)
    Thanks, exact
   what I need!
 * [@radektj](https://wordpress.org/support/users/radektj/)
    Can you please update
   your plugin whith this code? And thank you for your good job 🙂
 *  [akedv](https://wordpress.org/support/users/ak71/)
 * (@ak71)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-10207680)
 * [@toocoolone](https://wordpress.org/support/users/toocoolone/): great work, helped
   me big time!
 * but is there any way I can exclude categories with this as well? I have posts
   belonging to more than one category, but I want to exclude all belonging to a
   specific one. unfortunately just adding a “-cat-id” didn’t work.
 *  Thread Starter [toocoolone](https://wordpress.org/support/users/toocoolone/)
 * (@toocoolone)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-10207969)
 * [@ak71](https://wordpress.org/support/users/ak71/)
    The easiest way to exclude
   the category IDs listed in the “cat” shortcode would be to change line 257 to
   read:
 * `$query['category__not_in'] = $args['cat'];`
 * Notice that we’ve changed `category__in` to `category__not_in` This change causes
   the query to ignore instead of include the category IDs you have listed.
 * Be careful when applying updates to this plugin as if the author updates the 
   plugin it will wipe out these changes!
 *  [akedv](https://wordpress.org/support/users/ak71/)
 * (@ak71)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-10208821)
 * [@toocoolone](https://wordpress.org/support/users/toocoolone/), thanks a lot,
   but that isn’t what I’m looking for (right now, maybe later 😉 )
 * my case:
 * I have a post belongin to cat1, cat2 & cat3 and another belonging to cat1 and
   cat2
 * I want to display all posts belonging to cat1 & cat2 but NOT any which is also
   member of cat3! so in this case just the second post should be displayed, not
   the first, is this possible?
 *  Thread Starter [toocoolone](https://wordpress.org/support/users/toocoolone/)
 * (@toocoolone)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-10211304)
 * [@ak71](https://wordpress.org/support/users/ak71/), maybe I wasn’t clear on one
   detail: This change does this as long as you update the shortcode to equal the
   category you want _hidden_. By changing `category__in` to `category__not_in` 
   the query ignores any posts that are in the category provided in the shortcode(
   the category ID you want to hide). This will only show the second post in the
   example above. The behavior is the inverse of what the plugin does normally.
 *  [akedv](https://wordpress.org/support/users/ak71/)
 * (@ak71)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-10212807)
 * I know, but there ary many many more categories which have to be excluded, that’s
   why I asked. These changes are not only for the shortcode, but for the widget
   as well, right?
 *  [akedv](https://wordpress.org/support/users/ak71/)
 * (@ak71)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-10213847)
 * Nope, doesn’t work, now I get no posts at all.
    -  This reply was modified 8 years, 1 month ago by [akedv](https://wordpress.org/support/users/ak71/).
 *  Thread Starter [toocoolone](https://wordpress.org/support/users/toocoolone/)
 * (@toocoolone)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-10223553)
 * In your example you only indicated one. The original modification I suggested
   at the start allows you to pass a list of IDs in to the plugin.
 * `[rpwe cat="4,6,3,7,9"]`
 * When you make the first modification I gave above (that breaks a list down into
   an array), list the categories you want excluded in `cat=""` and add the `category__not_in`
   line it will hide any post with at least one of the categories on the list. It
   tested working when I tried it.
 * The second question you asked: yes, these changes are for the widget. The shortcode
   relies on the core code you’re changing here so the behavior will be the opposite
   of what it normally is.
    -  This reply was modified 8 years, 1 month ago by [toocoolone](https://wordpress.org/support/users/toocoolone/).
    -  This reply was modified 8 years, 1 month ago by [toocoolone](https://wordpress.org/support/users/toocoolone/).

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

The topic ‘Category Shortcode is not an Array’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/recent-posts-widget-extended_3384ca.
   svg)
 * [Recent Posts Widget Extended](https://wordpress.org/plugins/recent-posts-widget-extended/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/recent-posts-widget-extended/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/recent-posts-widget-extended/)
 * [Active Topics](https://wordpress.org/support/plugin/recent-posts-widget-extended/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/recent-posts-widget-extended/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/recent-posts-widget-extended/reviews/)

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [list](https://wordpress.org/support/topic-tag/list/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 8 replies
 * 3 participants
 * Last reply from: [toocoolone](https://wordpress.org/support/users/toocoolone/)
 * Last activity: [8 years, 1 month ago](https://wordpress.org/support/topic/category-shortcode-is-not-an-array/#post-10223553)
 * Status: not a support question