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

  • http://www.macappguide.com Ryan

    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?

  • angela

    Hi Ryan,

    If you’d like to show posts as a drop down menu, you might try:

    http://www.dagondesign.com/articles/drop-down-post-list-plugin-for-wordpress/

    Angela

  • http://www.ilbigliettaio.it bigliettaio

    Hi.
    I would like to customer wp_category_psot in the same way i set widget setting in Theme Options.
    I use Atahualpa.
    Now w_category_post in implemented only in posts belongin in that category. http://www.ilbigliettaio.it/category/eventi-sportivi/rugby/
    and its category child.
    Visiting that category you can see theme setting widget: i use only recent posts.

    best regards

  • angela

    I updated the post above to include instructions for how to display the the latest posts of the current category in the sidebar.

    See original post from:

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

  • http://www.ilbigliettaio.it bigliettaio

    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?

  • Veronica

    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?

  • Angela

    Hi Veronica,

    I’ve come across one that I haven’t used. Give it a try and let me know. Tags are an automatic filter, meaning that if you click on a tag link, then you are only getting posts with that tag. I’d have to see your website and could maybe suggest ways to use the tags better.

    http://wordpress.org/extend/plugins/wp-post-sorting/

    Good luck,
    Angela

  • yuzer

    how can i sort categories in category widget in alphabetic order

  • Angela

    You should install the AVH Extended Categories widget http://wordpress.org/extend/plugins/extended-categories-widget/. It has lots of options.