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!
- The topic ‘Category Shortcode is not an Array’ is closed to new replies.