Customizing Post Sort Order Using the Category Posts Widget

I use the Category Posts widget on a lot of sites to show a list of post titles beneath a category name in the sidebar. It automatically displays posts in descending order from the most recent post to the oldest posts. To change this order, you need to edit the cat-posts.php file in the category-posts plugin directory in the plugins directory.

Here’s the line of code to change:

The Category Posts Widget is very good for showing a certain # of posts in a particular category using a widget that displays on every page. However, if you only want to show the posts for the current category in the sidebar, then add this code to your sidebar.php file:

<?php
$sidebar_related_query = new WP_Query('cat=' . $sidebar_cat_id . '&showposts=5&offset=0&orderby=post_date&order=desc');
while ($sidebar_related_query->have_posts()) : $sidebar_related_query->the_post();
$do_not_duplicate = $post->ID;
?>

<div id=‚ÄĚpost-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
</div>

<?php endwhile; ?>

I got this from: http://www.finaldesign.co.uk/blog/wordpress/displaying-the-latest-current-category-articles-in-the-sidebar.html

9 comments on “Customizing Post Sort Order Using the Category Posts Widget

  1. Thanks for the tip! I changed “date” to “title” and was able to sort alphabetically by post name, which is exactly what I needed.

    Do you know of any way to display the posts in a drop down list (rather than displaying as an unordered list) with this plugin?

  2. Hi Angela,
    and excuse for my last comment: i was a little tired and i do not realize that you solved my problem !
    Anyway i solved using conditional_tag in widget logic plug in.

    Another question:
    do you think i could limit number of listed post in a simple way?

  3. Hi Angela,

    I am trying to actually display a list of posts grouped by category in a page. Which I have working now, but in addition to that I would like to give the user the option to sort posts by title, by date or filter by tags.

    Have you come across anything like this?