WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Order posts by category? (7 posts)

  1. kingofmycastle
    Member
    Posted 6 years ago #

    Hi! I was hoping that I could display all my posts in category order, i.e., show all posts in cat 1, all in cat 2 etc.... I'm using WordPress 2.5

    Is there a way of doing something like this but with no fixed category number?

    <?php $my_query = new WP_Query('cat=4');
    while ($my_query->have_posts()) : $my_query->the_post();
        the_title();
    endwhile; ?>'

    I've tried using

    $posts = query_posts('orderby=category');
     foreach($posts as $post) :
        setup_postdata($post);
        the_title();
    endforeach; ?>

    but that didn't work.

    Any suggestions?

  2. Austin Matzko
    Member
    Posted 6 years ago #

    This is isn't pretty, as the problem is that unless you specify category(s) (or tag(s)), WordPress won't query the terms tables at all, so there is no way to sort by category ID. And even if you do specify categories, you can't just pass an "order by" query variable to query_posts, as WordPress restricts what can come from a query variable and be used in the "order by" clause.

    Basically, the code below sets up filters to add a "join" clause to include the terms tables in the query, to restrict it to categories (not paying attention to tags), and then to sort by the category ID. And then it re-queries, using the existing query variables (such as page number, etc.). Your mileage may vary.

    <?php global $wp_query;
            add_filter('posts_join', create_function('$a', 'global $wpdb; return $a . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";'));
            add_filter('posts_where', create_function('$a', 'global $wpdb; return $a . " AND $wpdb->term_taxonomy.taxonomy = \'category\'";'));
            add_filter('posts_orderby', create_function('$a','global $wpdb; return "$wpdb->term_taxonomy.term_id ASC";'));
            query_posts($wp_query->query_vars);
    ?>
    <?php while ( have_posts() ) : the_post() ?>
  3. kingofmycastle
    Member
    Posted 6 years ago #

    Hi! I've copied the above code into my template, just above The Loop, but it doesn't work. Was there anything extra I should have done? (i.e. clever stuff?)

    It's not finding any posts at all.

  4. kingofmycastle
    Member
    Posted 6 years ago #

    Wow, a little tweaking and it's working. Thanks so much! I just removed the global entry and edited the query_posts($wp_query->query_vars); line like so:

    <?php   add_filter('posts_join', create_function('$a', 'global $wpdb; return $a . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";'));
            add_filter('posts_where', create_function('$a', 'global $wpdb; return $a . " AND $wpdb->term_taxonomy.taxonomy = \'category\'";'));
            add_filter('posts_orderby', create_function('$a','global $wpdb; return "$wpdb->term_taxonomy.term_id DESC";'));
    	query_posts('');
    ?>
    <?php while ( have_posts() ) : the_post() ?>
  5. NightWalker
    Member
    Posted 5 years ago #

    Hi!
    I've tested the code and it works.
    It orderes posts by Category ,but it shows the posts ordered ASC (the newest post at last).
    if i change the DESC to ASC in "posts_orderby" it orderes only the categorys by CategoryID not the posts by Date.
    Can you say me how can i order the posts?

    Thanks :)

  6. 11Mystics
    Member
    Posted 5 years ago #

    Guys does this code break in 2.7? I've inserted it using either way shown above and getting the error:

    Fatal error: Cannot break/continue 1 level in (path to my page template)

  7. Nonenix
    Member
    Posted 5 years ago #

    Hi thanks for this solution I'm using the code in the archive.php theme file, but it got one problem, its ignorig the selected categorie to view...

    its showing ALL post ordered by categorie and not all the posts under the current category sorted by categorie, i'have got some experience with sql but i just read a bit about joins so its hard to get

    ps: using 2.7.1

Topic Closed

This topic has been closed to new replies.

About this Topic