2

I need to list completed (meta_value) projects for each year (meta_value), like this:

2006
- Project 1
- Project 2
...

2005
- Project 3 ...

I found (here) a great piece of code to deal with I'm after:

<?php // List posts by a Custom Field's values
$meta_key = 'year';  // The meta_key of the Custom Field
$sql = "
   SELECT p.*,m.meta_value
   FROM $wpdb->posts p
   LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)
   WHERE p.post_type = 'post'
      AND p.post_status = 'project'
      AND m.meta_key = '$meta_key'
   ORDER BY m.meta_value, p.post_date DESC
";
$rows = $wpdb->get_results($sql);
if ($rows) {
   foreach ($rows as $post) {
      setup_postdata($post);
      if ($post->meta_value != $current_value) {
         echo "<h3>$post->meta_value</h3>";
         $current_value = $post->meta_value;
      }
      // Put code here to display the post
      the_title();
   }
}
?>

But I still have (1) a further request on the code and (2) a problem with it, which are:

  1. I need to filter the query by another meta_key (key state: value completed).

  2. I use WPML and the post list displays both the post and it's translation (it should only get the current language posts)

1 Answer 1

1

You should use some of WordPress's built in functions, there is even a new meta compare parameter. You can create, for example:

$state =  get_post_meta($post->ID, 'meta_state', true);  //the meta value to compare
$query = new WP_Query
              ( array( 
               'meta_key' => 'project', 
               'meta_value' => '$state', 
               'meta_compare' => '<=', 
               'post_type' => 'projects' ) );

//spit them out into yearly dates using a conditional tag
3
  • Can you elaborate a little on how the conditional spitting would go? Thank you.
    – AnaRita
    Apr 19, 2011 at 16:29
  • Well this grabs all your posts with meta key project for instance, then you can write some php for the output, something like -->if the_date= 2006 ...echo"something";
    – Wyck
    Apr 19, 2011 at 22:24
  • I ended up making a query with meta_query and repeated it for each year. I'm still hoping, 'though, for a dynamic solution.
    – AnaRita
    Apr 20, 2011 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.