I currently have 2 categories, <strong>Press</strong> and <strong>Exhibitions</strong>.
For each one, I have a separate sidebar where I need to filter the posts by year.
Please see the screenshot for the design guideline.
Here is a basic HTML:
<div class="sidebar">
<div class="widget widget_archive">
<ul>
<li class="current"><a href="#">2012</a></li>
<li><a href="#">2011</a></li>
</ul>
</div>
</div>
Ideally it will have a class of current.
I used the Archive widget, but it's mixing up the 2 categories when I go in a specific year, so I'd prefer php code.
Again, what it needs to do:
show posts only from category <strong>Press</strong> by year
AND
show posts only from category <strong>Exhibitions</strong> by year
Each code will go in different sidebars. If there is a way to filter those dynamically even better.
Manoj Raj answers:
You Should have come across this solution.
http://kwebble.com/blog/2007_08_15/archives_for_a_category
It worked for me.
Lucian Florian comments:
that works great and was easy to integrate. Any idea how can it set a class of current for the active year?
Manoj Raj comments:
Javascript solution for the active thing.. Add css accordingly.
$(document).ready(function(){
var str=location.href.toLowerCase();
$(".navigation li a").each(function() {
if (str.indexOf(this.href.toLowerCase()) > -1) {
$("li.highlight").removeClass("highlight");
$(this).parent().addClass("highlight");
}
});
})
Modify the above code accordingly..
Lucian Florian comments:
perfect. working great
Jerson Baguio answers:
try this code
<h2>Archives</h2>
<ul>
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date");
foreach($years as $year) : ?>
<li><a href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a></li>
<?php endforeach; ?>
</ul>
Jerson Baguio comments:
This code below order the dates as desc
<h2>Show all years</h2>
<ul>
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC");
foreach($years as $year) : ?>
<li><a href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a></li>
<?php endforeach; ?>
</ul>
Lucian Florian comments:
but the posts still get mixed up. Can we have a filter by category and year?
Francisco Javier Carazo Gil answers:
Hi Lucian,
To have a filter by categories do the next:
<?php
global $wpdb;
global $post;
$querystr = "
SELECT DISTINCT YEAR(wposts.post_date)
FROM $wpdb->posts wposts
LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.taxonomy = 'category';
?>
Lucian Florian comments:
I get an error in your syntax. Can you be more specific where I add the code and if I need to customize something?
Francisco Javier Carazo Gil comments:
Ok, I'm going to do it exactly. I try in my blog and I write here. Wait a moment.
Francisco Javier Carazo Gil comments:
Try this in your SQL query:
SELECT DISTINCT YEAR(wposts.post_date)
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_relationships.term_taxonomy_id)
WHERE wp_term_taxonomy.taxonomy = 'category';
Lucian Florian comments:
I tried this but nothing shows up anymore:
<ul>
<?php
$years = $wpdb->get_col("
SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC
SELECT DISTINCT YEAR(wposts.post_date)
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_relationships.term_taxonomy_id)
WHERE wp_term_taxonomy.taxonomy = category';
");
foreach($years as $year) : ?>
<li><a href="<?php echo get_year_link($year); ?> "><?php echo $year; ?></a></li>
<?php endforeach; ?>
</ul>
Francisco Javier Carazo Gil comments:
Lucian,
My query shows the years that have any post for one specified category. Once the user has choosen the year, you have to do another query:
SELECT wposts.*
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_relationships.term_taxonomy_id)
WHERE wp_term_taxonomy.taxonomy = 'my_category' AND YEAR(wposts.post_date) = '2011';
Change year and category for your need.
Lucian Florian comments:
Francisco,
it doesn't shows anything at all in the default category.php template.
The code resides in a sidebar that's being pulled in both single.php and category.php and would be awesome to get the code working when in a year automatically.
Francisco Javier Carazo Gil comments:
Lucian,
You have to pass the year and the category in the code. You can get them with GET for example.
Lucian Florian comments:
I increased the prize, hope it helps.
My category names are <strong>press</strong> and <strong>exhibitions</strong>
Can you get me the full code?
Please use: http://pastebin.com/
Francisco Javier Carazo Gil comments:
Lucian,
1. In sidebar you have to use the first query to show years. You have to make URL with GET variables: ?year=$year&category=$category
2. Use the second query to show queries using this vars:
$year = $_GET['year'];
$category = $_GET['category'];
Lucian Florian comments:
you got me lost. Can you past full code please?
Francisco Javier Carazo Gil comments:
Lucian,
The code approximately. In sidebar, one for each one and you have to insert your category:
$years = $wpdb->get_col("
SELECT DISTINCT YEAR(wposts.post_date)
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_relationships.term_taxonomy_id)
WHERE wp_term_taxonomy.taxonomy = 'category'");
foreach($years as $year) : ?>
<li><a href="archive.php?year=<?php echo $year; ?>&category=<?php echo $year; ?>"><?php echo $year; ?></a></li>
<?php endforeach; ?>
In the archive:
<?php
$year = $_GET['year'];
$category = $_GET['category'];
get_header(); ?>
<div id="content" class="narrowcolumn">
<?php
$querystr = "
SELECT wposts.*
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_relationships.term_taxonomy_id)
WHERE wp_term_taxonomy.taxonomy = 'my_category' AND YEAR(wposts.post_date) = '2011';
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endforeach; ?>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Lucian Florian comments:
I added the code but nothing shows in the category landing page: (I added my category)
[[LINK href="http://cl.ly/0k3M2n332y3i2P2g390K"]]http://cl.ly/0k3M2n332y3i2P2g390K[[/LINK]]
Another question, on archives.php can the year filter be made dynamic? Otherwise the solution is not good. I simply can't see this work when it needs adjustments for all future years.
Francisco Javier Carazo Gil comments:
Lucian,
The first query make the solution dynamic.
Any problems that maybe you are having:
1. Look at year and category in archive, maybe you are having problems setting data
2. Be sure that you have the correct category in your code
Lucian Florian comments:
still not working, sorry.
Manoj Raj found the solution I was looking for.