How to replicate some of Drupal Views functionality in WordPress?

I admit, I still think in Drupal. (In choosing between developing in WordPress and Drupal, I still tend to favor Drupal, because I haven’t yet figured out a few things on the WordPress side.)

Can you help to figure out how to achieve in WordPress at least some of the Views functionality I know and love?

I can see that plugins that work with Custom Post Types are the WordPress equivalent of CCK. Now, is there a way (either through a UI or code) to:
1. display custom fields in columns and rows
2. Expose filters, that is, create drop-downs at the top, to allow the end user to select

Here’s a sample image of a single exposed field.
http://www.packtpub.com/sites/default/files/Article-Images/drupalviews-article1-timage18.png (from my book)

Variations would include filtering by more than one topic or custom field, being able to select multiple options, selecting on fields that are not just categories or tags, etc.

There’s one argument to be made that if I like Drupal so much, I should use it. But I think I could be a happy WordPress user too, if I could figure out how to display custom fields,and how to allow end-users to select through exposed filters. Any guidance welcome.


UPDATE:

I’m studiously working my way through two answers so far. And so far I’ve been able to create custom fields, and display them, one post at a time.

This is what I’ve added to my single.php. (It’s somewhat messy, and I welcome ideas for improvement.) But what I still have to do is get this out of the single.php context, so that I can display ALL post fields, and not just the current ID. (Also, my current css yields .meta_items displaying under the wrong .meta_table_header if there are blank fields. Seems setting the width doesn’t work if the element is empty, so somehow I have to populate it with a nobreak-space.)

I also installed the Custom Field Template plugin: http://wordpress.org/extend/plugins/custom-field-template/, and figured out how to comma-delimit the results of multiple checkbox selections. (I haven’t played much with Custom field Template yet, but I have the feeling I’m going to like it.)

 <div id="meta_table">
  <?php 
  $div_meta_row =  '<div class=\'row\'>';
  $div_meta_item ='<div class=\'meta_item\'> ';
  $div_meta_table_header="<div class =\"meta_table_header\'>';
  $div_end='</div>';

   echo $div_meta_row;
   echo $div_meta_table_header, "Breakfast", $div_end, $div_meta_table_header, "Currently Reading", $div_end, $div_meta_table_header, "Hours of Sleep", $div_end, $div_meta_table_header,'Favorite Fruits', $div_end;
   echo $div_end;

   echo $div_meta_row;
      $meta_values = get_post_meta($post->ID, 'Breakfast', $single);
      echo  $div_meta_item, $meta_values, $div_end;
      $meta_values = get_post_meta($post->ID, 'Currently Reading', $single); 
      echo $div_meta_item, $meta_values, $div_end;
      $meta_values = get_post_meta($post->ID, 'Hours of Sleep', $single);
      echo $div_meta_item, $meta_values, $div_end;
      $meta_values=implode(get_post_meta($post->ID, 'Favorite Fruits', 0), ', ');
      echo $div_meta_item, $meta_values, $div_end;
    echo $div_end;
 ?>  
 </div>

Here’s the accompanying style sheet, which I placed in header.php.

<style type="text/css">
#meta_table .row {
float:left;
margin:10px 0;
}
#meta_table .row .meta_item, .meta_table_header {
width:120px;
margin-right:10px;
float:left;
}
.meta_table_header {
font-weight:bold;
}
</style>

Once I get all the posts’ fields to display in a separate URL, I’ll work on the “custom fields parameters of query_posts.” So, I know more than I did when I first asked the question. Still a ways to go.

(Drupal Views offers a powerful UI, and so far this is almost all coding, but I’ll wait until I truly have this figured out in WordPress before making any pronouncements about which is easier. I’m hoping in the end to enable the user to filter by multiple custom fields in WordPress.

Here’s what this looks like so far.

Progress so far...

Aiming for something more like:

Goal

5 Answers
5

  1. display custom fields in columns and rows: get_post_meta to display them in your theme, and add_meta_box to customize the admin

  2. Expose filters, that is, create drop-downs at the top, to allow the end user to select:
    you may want to use the custom fields parameters of query_posts. E.g.:

Dropdown field in a form at the top:

<select name="custom_field" id="custom_field">
    <option value="value1">Value 1</option>
    <option value="value2">Value 2</option>
</select>

Corresponding custom query:

$value = $_POST['custom_field'];
query_posts("meta_key=custom_field&meta_value=$value");

You may want to see The Loop and get_posts for reference.

Hope this helps.

Leave a Comment