Support » Plugin: Recent Posts Widget Extended » 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)
  • @toocoolone
    Thanks, exact what I need!

    @radektj
    Can you please update your plugin whith this code?
    And thank you for your good job 🙂

    @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.

    @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!

    @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?

    @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.

    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?

    Nope, doesn’t work, now I get no posts at all.

    • This reply was modified 2 years, 6 months ago by akedv.

    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 2 years, 6 months ago by toocoolone.
    • This reply was modified 2 years, 6 months ago by toocoolone.
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Category Shortcode is not an Array’ is closed to new replies.