[EventCalendar] Re: ec3 with custom fields solved

David Garlitz dgarlitz at wesleyan.edu
Mon Mar 10 13:44:16 UTC 2008


OK, keep in mind that this is a modification of "big calendar patch  
5" by matthew, so some of these code examples might not match up to  
what you have in your php files, but the general idea (i hope) would  
be the same.

First step is to call your custom field in the sql query (in template- 
functions.php):

    $sql=
      "SELECT DISTINCT
         id,
         post_title,
         GREATEST(start,'$begin_date') AS start_date,
         LEAST(end,'$end_date') AS end_date,
         meta_value,
         allday,
         1 AS is_event
       FROM $wpdb->posts, $wpdb->postmeta
       INNER JOIN $ec3->schedule
        ON ($ec3->schedule.post_id=$wpdb->posts.id)
       $additional_joins
       WHERE post_status='publish'
         AND end>='$begin_date'
         AND start<'$end_date'
         AND $wpdb->postmeta.post_id=$wpdb->posts.id
	$additional_and_statements";

The content of a custom field is in your postmeta table in a field  
called meta_value, so I included that in the query. Then, you've got  
to tell sql that meta_value is in postmeta, and not posts, so I've  
just tacked on a comma join (could be better with another inner join,  
but I'll have to get to that later), indicating that the query should  
look for items in both wp_posts and wp_postmeta.

Then you've got to make sure that the query picks out the meta_value  
that goes with each post. Fortunately, custom fields are assigned the  
same id number as the post they are assigned to, which is handy, so I  
add an AND statement telling it to the meta_values whose post_id  
number corresponds to the id of each event post.

After that, you've got to do a bit of variable tracking - the  
information that's echoed onto the Big Calendar days uses some  
variables that are set up in the file called day.php. Here it is,  
with my additions:

class ec3_Day_Event
{
//Matthew: Adding this class for more flexibility in calendar displays.
  var $title;
  var $time;
  var $info;
  var $is_event;
  var $allday = 0;
  var $id;
  function ec3_Day_Event(){}

}

class ec3_Day
{
   var $is_event =False;
   var $events   =array(); //Matthew: Changed from "titles", as this  
now holds objects.  (this is an array of objects)
   function ec3_Day(){}
   function add_post($title,$time,$info,$is_event,$allday,$id) // 
Matthew: add allday and id
   {
     $safe_title=strip_tags($title);
     $safe_title=
       str_replace(
         array(',','@'),
         ' ',
         htmlspecialchars(
           stripslashes($safe_title),
           ENT_QUOTES,
           get_option('blog_charset')
         )
       );
     if($is_event)
     {
       //Matthew: BUGFIX: don't include "@ time" in title.
       //$safe_title.=' @'.$time;  //don't put times here anymore
       $this->is_event=True;
     }
     $nextEvent = count($this->events);
     //Matthew: set the (new) event object
     $this->events[$nextEvent] = new ec3_Day_Event();
     $this->events[$nextEvent]->title = $safe_title;
     $this->events[$nextEvent]->time = $time;
     $this->events[$nextEvent]->info = $info;
     $this->events[$nextEvent]->is_event = $is_event;
     $this->events[$nextEvent]->allday = $allday;
     $this->events[$nextEvent]->id = $id;
   }
   function get_titles()
   {
     //Matthew: update to use new array of objects
     $temporary_title_array = array();
     foreach ($this->events as $key=>&$val) { //Thus, $val becomes a  
pointer to the ec3_Day_Event object.
      //This loops through the events
      $temporary_title_array[] = $val->title . ' @ ' . $val->time .  
$val->info;
     }
     return implode(', ',$temporary_title_array);
   }
}

?>

I've simply added a new variable, called "info," to the list at the  
beginning, and then I've plugged in $info wherever the other  
variables, like $time and $title, are used.

Now that that's set up, I've just got to go back to template- 
functions.php and add in the appropriate values.

Here's where the function add_posts gets setup:

   $calendar_days[$day_id]->add_post($ent->post_title,$time,$ent- 
 >meta_value,$ent->is_event,$ent->allday, $ent->id); //Matthew:  
Changed internal function for big calendar

The variables have to be in the order specified on day.php, so I make  
sure to insert $ent->meta_value right after $ent->post_title, so that  
it will be interpreted as the value for my variable "info."

Then, later on this info is echoed onto the page using this:

      	echo '<p class="ec3_event_day_evt ec3_alt_class_'. 
$alt_class.'">';
     	echo '<a class="ec3_big_calendar_link" href="' . get_permalink 
($val->id) . '">' .  $val->title  . '</a>  ' . $val->time .'<br/>' .   
$val->info . '</p>';
//    	echo '<a class="ec3_big_calendar_link" href="' . get_permalink 
($val->id) . '">' .  $val->title . '</a>  ' . $val->time .'<br/>' .   
$val->info ) . '<hr/>';
//     	echo '</p>';

I've inserted $val->info with a br/ to make sure that my custom field  
is printed on its own line.


That's pretty much it. I know this might be a bit crude - there is  
probably a more elegant way to get this done. Any input from anyone?  
I apologize for not giving line counts, but you should be able to  
find your way around using some ctrl-F action...

Hope this makes life easier for someone out there!
Dave





On Mar 10, 2008, at 1:43 PM, firetree_ec3 at spamex.com wrote:

> Do tell.
>
> Enquiring Minds Want to Know...
>
> On Mar 10, 2008, at 7:35 AM, David Garlitz wrote:
>
>> * Replies will be sent through Spamex to eventcalendar at firetree.net
>> * For additional info click -> http://www.spamex.com/i/?v=17832931
>>
>> Well, I figured it out pretty quickly, actually. If anyone is  
>> interested in the custom fields solution, let me know...
>>
>> Dave
>>
>>
>> On Mar 10, 2008, at 11:17 AM, David Garlitz wrote:
>>
>>> Hi,
>>>
>>> I've been trying to figure out how to incorporate a query to a  
>>> post's custom field (i.e. $wpdb->postmeta.meta_value) in order to  
>>> include extra information about an event (like location, or  
>>> entrance fee, etc.). I know most solutions to this problem  
>>> involve using a custom loop, but I'd like these custom fields to  
>>> appear on the Big Calendar (Matthew's patch), so a custom loop  
>>> won't cut it.
>>>
>>> Any ideas about how it could be done?
>>>
>>> Dave
>>>
>>> On Feb 19, 2008, at 2:31 PM, Matthew Middleton wrote:
>>>
>>>> That's correct, I made it sort by start date only in
>>>> database queries done by the plugin itself.  This is
>>>> done when returning a list of events or drawing a
>>>> calendar.  When you use The Loop in a custom query,
>>>> you are querying the posts rather than the events.
>>>> (One post may have multiple events, or have no events)
>>>>
>>>> Stay tuned for "Big Calendar 6" - which I will
>>>> hopefully post soon.  It will have several new
>>>> features including recurring event support.  Since one
>>>> post may have hundreds of recurring events, you may
>>>> want an option for a custom query on events rather
>>>> than posts.  I haven't implemented this feature yet,
>>>> but I may do so in the future.  I did make some
>>>> changes to The Loop to support posts with recurring
>>>> events - I will provide a full changelog with the
>>>> patch.
>>>>
>>>> -Matthew
>>>>
>>>>
>>>> --- David Garlitz <dgarlitz at wesleyan.edu> wrote:
>>>>
>>>>> Hi Mathew - Thanks for your work on Big Calendar.
>>>>>
>>>>> I was excited to see that one of the fixes you
>>>>> implemented was :
>>>>>
>>>>> *Sort all events by event start time, not post time.
>>>>>
>>>>> However, I'm not getting this functionality in
>>>>> custom queries. Have I
>>>>> misunderstood what you mean by this? Is there
>>>>> something missing from
>>>>> my query? This is what I'm using in the loop:
>>>>>
>>>>> <?php $my_query = new WP_Query($query_string .
>>>>> 'cat=11&showposts=2&ec3_after=today&order=ASC');
>>>>>
>>>>> but my posts show ordered by post date. Am I missing
>>>>> something?
>>>>>
>>>>> Thanks in advance for your help!
>>>>> Dave>
>>>> _______________________________________________
>>>>> Blog: http://wpcal.firetree.net/
>>>>> EventCalendar at firetree.net mailing list
>>>>> Unsubscribe:
>>>>> http://penguin.firetree.net/eventcalendar
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Blog: http://wpcal.firetree.net/
>>>> EventCalendar at firetree.net mailing list
>>>> Unsubscribe: http://penguin.firetree.net/eventcalendar
>>>>
>>>
>>
>>
>> _______________________________________________
>> Blog: http://wpcal.firetree.net/
>> EventCalendar at firetree.net mailing list
>> Unsubscribe: http://penguin.firetree.net/eventcalendar
>
> catchall account
>
>
> _______________________________________________
> Blog: http://wpcal.firetree.net/
> EventCalendar at firetree.net mailing list
> Unsubscribe: http://penguin.firetree.net/eventcalendar
>




More information about the EventCalendar mailing list