Home Forums Xi Theme Discussion & Support Exclude Category from Show Posts widget

This topic is: not a support question

This topic contains 8 replies, has 2 voices, and was last updated by  Robert DeGray 4 years, 3 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #3208

    Robert DeGray
    Participant
    Post count: 14

    We have one particular Category we want to exclude from our “All posts” display.

    I’ve tried editing the show-widgets.php file, los_showPosts() function to use one of the WordPress get_posts() ways of doing this, but instead of excluding that one category, it seems to mess up the whole query, returning very old posts, out-of-sequence (and explicitly specifying order_by doesn’t change that).

    What I tried was this:

    	$cat = $args['cat_id'];
    	if ($cat == 0) $cat = '-9';
    	    
    	$get_posts_args = array(
    		'cat' => $cat,
    		'numberposts' => $args['num_posts']
    	);

    Where 9 is the category ID I want to exclude.

    I also tried the variation of adding ‘category__not_in’ => array(9) , and this exhibited the exact same behavior.

    #3209

    themeweaver
    Keymaster
    Post count: 730

    The show posts code does not provide option to exclude a category.

    You need a custom query. By all means use show posts as a starting point but you should not edit the code. When the next theme update is released you will lose your changes.

    Custom code should be done in a child theme.

    #3210

    Robert DeGray
    Participant
    Post count: 14

    I understand I *should* do it in the child theme, and I know theme updates will overwrite this change. I’ve done the rest of our more permanent changes that way. But I need a quick way to do this for now, and I need to know how to make this work. If it doesn’t work editing the root page, I won’t know how to make it work in the child one either.

    So any idea why excluding the category this way doesn’t work at all?

    #3211

    themeweaver
    Keymaster
    Post count: 730

    This previous thread details all arguments used by los_showposts function: http://themeloom.com/forums/topic/template-with-page-and-post-category/

    It does not take an exclude category argument.

    In the end all this function does is use the standard WordPress functions for building custom queries. Most of the code is about manipulating the options provided by widgets and shortcodes built into our themes.

    If you need a custom query it is much simpler to use the wordpress get_posts() yourself. See http://codex.wordpress.org/Template_Tags/get_posts

    #3212

    Robert DeGray
    Participant
    Post count: 14

    The reason to use the widget is so that it maintains a consistent look with where the widget is used, and also because your widget have nice look and feel to them.

    In the end, I *am* using get_posts from within the widget, hardcoding a category to exclude, and I’m following the API documentation on how to exclude it. Unless you guys have overloaded get_posts, I suppose.

    #3213

    themeweaver
    Keymaster
    Post count: 730

    No, we have done nothing to get_posts. We simply pass the arguments collected by show posts, check and pass theme to get posts.

    If you are just adding a new argument to the array, should be no reason for it not to work. Sometimes combinations of arguments can cause unexpected behaviour with some of the WordPress functions. Check the WordPress docs that other parameters are not confusing things.

    #3228

    Robert DeGray
    Participant
    Post count: 14

    So while I still don’t know why the documented ways of excluding a category don’t work here…

    If I did want to extend your widget in my child theme, what’s the best way to do that? I’d rather not copy the entire file and just tweak it. At the most, I’d want to inherit all the other methods and simply copy and edit los_showPosts(). And I don’t know enough about WP theming at this point to know whether I need to do something in my file to import the parent show-widgets file or whether I need to put my widget file in the same directory hierarchy under my child theme.

    Also, of course, if there’s an easier way to inherit the rest of the function behavior than copying the whole thing, I’d prefer that too, but I suspect there is not?

    (Really, what I’d prefer is if you guys could eventually add to the widget yourselves the ability to exclude a category or the ability to multi-select which categories you want rather than only having the option of a single category or all categories.)

    #3231

    themeweaver
    Keymaster
    Post count: 730

    We have gradually added quite a number of options to the widgets over time. The problem is that this can start to get quite a complex set of options.

    We will consider adding the feature. But..

    There are other plugins around that implement every single option present in a WP query object, and we do want to keep things simple.

    Also, you could actually create another category for “show in widget” and add posts to the category. Posts can belong to more than one category. In this way an exclude option is not necessary.

    There isn’t really an easy way to ‘extend’ or ‘inherit’ the widget and code right now, without creating a completely custom version of custom-widgets.php.

    #3233

    Robert DeGray
    Participant
    Post count: 14

    Oh well, thanks anyway. I must be missing something about WordPress queries because it looks like the same API call you’re using is supposed to easily be able to exclude categories with a negative number, but as I said, this didn’t work.

    It’ll be too much ongoing work to make everyone who posts content always select an additional category, rather than impacting only those who post only to this one category. (It’s a category for documenting site development issues, which we don’t want the general public to be seeing prominently, although we’d sometimes want to direct non-admins there for feedback.)

    I guess we’ll keep our current hack for now, which is editing the widget file to in the loop skip posts which match the category we want to exclude.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.