July 6, 2010 / admin

 

Objective: Display two sets of data on one page both of which will be from the same category.

We are using Flutter for this example. Flutter uses write panels to create posts. Whenever a post is added through a write panel, it is automatically placed in a specific category. Within this category are two types of posts, one is an ongoing opportunity; the other type has an expiration date. For the first type, we can do a database query checking whether one piece of information exists or not. These days when you’re using WordPress and you want to access database information while in the Loop[codex link], you use the query_posts function [link to codex].  When you add the Flutter to the mix, you have new host of parameters to add to query_posts.

Recently (a couple versions ago), WordPress added new parameters related to meta values or custom fields. So, you can access Flutter custom fields through these meta values. These parameters are all we need for the first set of data that displays ongoing opportunities. However, you can only access one key/value [link to PHP Array page] pair at a time. That throws a small wrench into things later. For now, this is all we need:

 

<?php
$thequery = "meta_key=ONGOING-OPPORTUNITY&meta_value=true";
$pageposts1 = query_posts($thequery);
?>

 

In the backend under the Flutter write panel we are accessing, is a checkbox labeled “ONGOING-OPPORTUNITY.” That is the key of our key/value pair. We are simply determining whether it is checked or not i.e. true or false.

I’ll explain why we use the variable $pageposts1 later. To display our recently queried data, do this:

<?php
foreach($pageposts1 as $post):
?>
<tr>
<td><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></td>
</tr>
<?php endforeach; ?>

Troubleshooting Tip: Echo the query which should give you SQL code. Use the SQL option in your database to run the query to see if anything is returned.

Now for the more complicated part. We are going to overwrite the previous query so that it does not interfere with our work going forward. We want to exclude the previous posts (so unchecked ONGOING-OPPORTUNITY) and include all those with expiration dates that are yet to have passed. Here’s what we have:

<?php
$thequery = "meta_key=ONGOING-OPPORTUNITY&meta_value=false&customorderby=x_OPPORTUNITY-EXPIRES&order=ASC";
$pageposts2 = query_posts($thequery);
?>

Here you see that ONGOING-OPPORTUNITY is false, but we have also added a Flutter-only parameter “customorderby=x.” We are custom ordering by another Flutter field called OPPORTUNITY-EXPIRES. This is a date field. Remember that. The order parameter set to “ASC” merely makes upcoming dates to appear at the top before dates further down the road.

The next step is preparation for displaying the content.

<?php
$fd = 0;
foreach($pageposts2 as $post):
$expire_date = get('OPPORTUNITY-EXPIRES');
if( (date('Ymd', strtotime($expire_date))) >= (date('Ymd')) ) {
$fd++;
?>

Lots to look at here. I’ll explain the variable $fd while I’m explaining $pageposts1. $expire_date uses a Flutter function to “get” the value in the OPPORTUNITY-EXPIRES field. The next line begins an if statement that first converts $expire_date to a manageable format then compares it to today’s date. So, if the expiration date of the post is greater than or equal to today’s date, it will be displayed (e.g. 20100408 [April 8, 2010] is greater than 20100301 [March 1, 2010]). We now just need to show our results and put our date back into a readable format.

<tr>
<td align="left" valign="top"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></td>
<td align="left" valign="top"><?php echo date('n/j/Y', strtotime($expire_date)); ?></td>
<?php } endforeach; ?>

And there it is. We have one more contingency to account for, and that is empty queries. Here’s where $pageposts1 and $fd come into play.

<?php
if(($pageposts1 == 0) && ($fd== 0)) { echo '<p>No current funding opportunities were found.</p>'; }
?>

$pageposts1 is a count of the first query; $fd is a count of the second. They are counted differently because the second query has to have the expiration date compared to today’s date. So $fd, instead of counting every post retrieved by the query, only counts those that will be displayed based on the aforementioned dates.

And that’s all there is to it. Here’s the full code:

<?php
$thequery = "meta_key=ONGOING-OPPORTUNITY&meta_value=true";
$pageposts1 = query_posts($thequery);

foreach($pageposts1 as $post):
?>
<tr>
<td><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></td>
</tr>
<?php
endforeach;

$thequery = "meta_key=ONGOING-OPPORTUNITY&meta_value=false&customorderby=x_OPPORTUNITY-EXPIRES&order=ASC";
$pageposts2 = query_posts($thequery);

$fd = 0;
foreach($pageposts2 as $post):
$expire_date = get('OPPORTUNITY-EXPIRES');
if( (date('Ymd', strtotime($expire_date))) >= (date('Ymd')) ) {
$fd++;
?>
<tr>
<td align="left" valign="top"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></td>
<td align="left" valign="top"><?php echo date('n/j/Y', strtotime($expire_date)); ?></td>
<?php
} //end if
endforeach;

if(($pageposts1 == 0) && ($fd== 0)) { echo '<p>No current funding opportunities were found.</p>'; }
?>

 

 

 
Posted In: Blog, Programming, WordPress