WordPress.org

Ready to get started?Download WordPress

Forums

Disabling sub category posts and alphabetizing posts by title (5 posts)

  1. skunkbad
    Member
    Posted 3 years ago #

    I'm using WordPress as a CMS, and have a need to alphabetize posts, and at the same time limit the posts to only the parent category (no sub category posts). I've been using the following code in my functions.php:

    http://wordpress.pastebin.com/X3kw8edG

    That works fine, but if I try to do something like this so that the posts are alphabetized, then I seem to get every post, even ones that aren't in the related category in any way:

    $posts = query_posts($query_string .
    '&orderby=title&order=asc&posts_per_page=-1');

    I got that little bit of code from here:
    http://codex.wordpress.org/Alphabetizing_Posts

    Anyone know what I'm doing wrong and how to fix it?

  2. David Gard
    Member
    Posted 3 years ago #

    What is being printed by $query_string? It may be that it's contents are not what you need and thus screwing up your query.

    To only include your parent category/categories use &category=1,2,3, where 1,2,3 is the comma seperated list of the categories you want to include.

  3. skunkbad
    Member
    Posted 3 years ago #

    If I var_dump $query_string right before the "$posts =" line, it returns NULL.

  4. skunkbad
    Member
    Posted 3 years ago #

    I actually checked all of that code inside the twentyten theme, and it works fine. I think the problem is with my sub category template redirect:

    function product_library_sub_template(){
    	if (is_category()){
    		if (cat_is_ancestor_of('3', get_query_var('cat'))){
    			load_template(TEMPLATEPATH . '/product-library-sub-template.php');
    			exit;
    		}
    	}
    }
    add_action('template_redirect', 'product_library_sub_template');

    The problem is, I need all of this to work like it is, just sorted alphabetically...

  5. skunkbad
    Member
    Posted 3 years ago #

    So, as it turns out, when using my sub-category templates through the function load_template, it breaks my ability to alphabetize posts via the method documented in the codex. I ended up intercepting the posts before the load_template function, and used php's uasort and strcasecmp functions to alphabetize them. It's working perfectly. For anyone else attempting to do this, check the code at pastebin:

    http://pastebin.com/raw.php?i=qfxHagXr

Topic Closed

This topic has been closed to new replies.

About this Topic