Support » Plugins and Hacks » Edit get_calendar() function to allow show posts from specific category

Edit get_calendar() function to allow show posts from specific category

  • Hello,

    I’m trying to edit get_calendar() function / hack it, so it can accept new parameter – category. I need this because I need to show links in calendar only from specific (“events” category).

    Any ideas how to do it? I already tried editing the SQL in the core files, but it not worked for me.

    I’ve also find this plugin: http://www.qogo.com/wpplugins/cats-widgets.phps which is probably old and not works with the current WP tables structure.

    Any help will be really appreciated. If needed I can pay you as well.

Viewing 10 replies - 1 through 10 (of 10 total)
  • I’m trying to do the same thing too… I went into general-template.php, and it looks like we should be narrow the query parameters from the sql query. My calendar will be displaying in one place only to show upcoming events, so I’m not too concerned about making it too focused.

    Here’s the query starting on line 892: “/**
    * Display calendar with days that have posts as links.

    Line 916:

    $gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1");

    and then again between 949-962

    // Get the next and previous month and year with at least one post
    	$previous = $wpdb->get_row("SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_date < '$thisyear-$thismonth-01'
    		AND post_type = 'post' AND post_status = 'publish'
    			ORDER BY post_date DESC
    			LIMIT 1");
    	$next = $wpdb->get_row("SELECT	DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_date >	'$thisyear-$thismonth-01'
    		AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
    		AND post_type = 'post' AND post_status = 'publish'
    			ORDER	BY post_date ASC
    			LIMIT 1");

    Theoretically speaking, shouldn’t we just be able to ask for the catid from the posts here and display?

    I don’t know what the catid fieldname is in posts, else I would try it out.

    Hi GeekAcademic,

    I searched this forum and find some SQL queries then I glued it together and here is what I got: http://pastie.org/463933

    Its a copy of the get_calendar function from general template. I added lines: 117-118 and 123-124 where “17” is category ID I need to filter.

    Its very “hackish” and ugly (I should add rather a parameter for the category ID, but I’m traveling and I don’t had time to play around with it)
    The only issue I see is that I need somehow to edit the archive pages as well, because when I click on “event” in calendar then I see all posts, do you have any idea, which function/file I should edit for this?

    Sorry… one last place


    // Get days with posts
    	$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
    		FROM $wpdb->posts WHERE MONTH(post_date) = '$thismonth'
    		AND YEAR(post_date) = '$thisyear'
    		AND post_type = 'post' AND post_status = 'publish'
    		AND post_date < '" . current_time('mysql') . '\'', ARRAY_N);
    	if ( $dayswithposts ) {
    		foreach ( (array) $dayswithposts as $daywith ) {
    			$daywithpost[] = $daywith[0];
    	} else {
    		$daywithpost = array();
    	if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') !== false || strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari') !== false)
    		$ak_title_separator = "\n";
    		$ak_title_separator = ', ';
    	$ak_titles_for_day = array();
    	$ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom "
    		."FROM $wpdb->posts "
    		."WHERE YEAR(post_date) = '$thisyear' "
    		."AND MONTH(post_date) = '$thismonth' "
    		."AND post_date < '".current_time('mysql')."' "
    		."AND post_type = 'post' AND post_status = 'publish' "

    Hi I had the same situation on my blog.

    I modified code from a pastie on one of the posts regarding category calendar posts, made it work, and combined it with an archive template to achieve the desired effects.

    First, add this code to your functions.php file:

    Then, create or edit your date/archive.php file to exclude posts not within your category. For me, the category name is ‘blog’, which has the ID 7 (you can see me use ID 7 above when I call the custom calendar function).

    if (have_posts()) {
    	while (have_posts()) {
    		if (in_category('blog')) {
    			$link = get_permalink();
    			$title = the_title(NULL,NULL,FALSE);
    			$content = get_the_excerpt();
    			$post_id = $post->ID;
    			$results[] = Array('id' => $post_id, 'title' => $title, 'link' => $link, 'content' => $content);
    if ($results) {
    	foreach ( $results as $result ) {
    						<div class="feature">
    							<h2><a href="<?=$result['link'];?>" title="<?=$result['title'];?>"><?=$result['title'];?></a></h2>
    							<div class="clear"></div>
    						</div> <!-- div#.feature -->
    } else {
    	<p>Sorry, no posts found</p>

    You can see a working example here:

    The top carousel items are pulled from blog posts in the ‘frontpage carousel’ category, however the archive pages and the calendar only show posts from the ‘blog’ category.

    Example, include multiple categories:

    Let’s say you’re blog section has more than one category (6=blog featured, 7=blog normal). Gather your category IDs and category slugs.

    In functions.php:

    echo get_calendar_custom('6,7');

    This tells the calendar to draw from category IDs 6, and 7.
    *** Make sure you pass a string of comma seperated values ***
    YES: get_calendar_custom(‘1,5,12,16’);
    NO: get_calendar_custom(7,9,12);

    Next, edit your archive.php or date.php loop at the part where you say if in_category, add all the category slugs (you can also pass the category IDs or the proper name, I personally like slugs because I can see the name):

    if (in_category('blog,blog-featured')) {

    Let me know if this helps, it works for me I’m using WP 2.7.1

    hey jean, you are genius. I’m trying to achieve the same effect as depi so I’ve taken your code and added it to my theme. However, I don’t use the archive/date pages so I didn’t touch those. I’ve tried both adding the pastie to my wp-includes/functions.php and my theme/functions.php neither worked. Does this wind up adding a widget to the widget system or one that I need to include myself?

    Thanks a lot for your hard work!

    Yes you should add it to your theme’s functions.php file, and you should see a new widget in the widgets menu that you can add to a sidebar.

    You should see ‘Customized Calendar’ in the list of available widgets.

    Do you have anything else in your themes functions.php file?

    Things to take into account:

    If a month has 10 blog posts and 10 posts not in the ‘blog’ category, some posts won’t show.

    This is because the default amount of posts to be shown per page is 10.

    I’m still trying to figure out a workaround for this.

    Most likely, if you want to maximize the archive page you’re going to have to build a custom query that selects posts by month refined by category.

    After I figure that one out I’ll post my code here.

    Hi there Jean,

    thanks for the great solution! One small problem I have is that I can’t show future events on the calendar. Can you show me where to modify your code to include future dates as well?

    Thank you!


    It looks like the author of this Pastie may have abandoned it. It is true, there is a mistake in the code (at least for WP 2.9.2) that doesn’t detect future “events” in the target category ID, so you don’t get the future month link.

    The programming is beyond me to debug, unfortunately, as this is a very useful modification to the default sidebar calendar widget which could be further customized once working.


Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Edit get_calendar() function to allow show posts from specific category’ is closed to new replies.
Skip to toolbar