Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Collecting and Sorting A Date Via Custom Fields WordPress

  • REFUNDED

Hello!
So here's the setup: I'm using custom post types to allow my client to create events which he can display on his website. I have everything working except a proper way to collect the start date of the events so I can then sort the events by the date.

To sort by the custom date field, I need it to be formatted like 090501' or '2009-05-01'. So what I'll need to do is collect the month, day, and year separately, and have them somehow combined into a single value for sorting. However, I'll need to have the month, day as separate values as well, so I can display them on the front end. Drop down menus would be nice for the client to select from, but not required if too difficult.

<strong>This is the function I will be using to sort the events:</strong>

<?php $loop = new WP_Query( array( 'post_type' => 'event', 'posts_per_page' => 10, 'orderby'=> 'meta_value', 'meta_key' => 'event_start', 'order' => 'DESC' ) ); if(have_posts()) : while ( $loop->have_posts() ) : $loop->the_post(); ?>


<strong>Below is my current method of collecting information from the meta boxes in the event post type (via functions.php).</strong>


<?php
//Event Meta Boxes //
add_action("admin_init", "add_event");
add_action('save_post', 'update_event_options');
function add_event(){
add_meta_box("event_details", "Event Info", "event_options", "event", "normal", "low");
}
function event_options(){
global $post;
$custom = get_post_custom($post->ID);
$event_start = $custom["event_start"][0];
?>
<table>
<tr>
<td><label>Event Start Date:</label></td>
<td><input name="event_start" value="<?php echo $event_start; ?>" /></td>
</tr>
</table>
<?php
}
// Saving Event Meta Boxes //
function update_event_options(){
global $post;
if(isset($_POST["event_start"])) update_post_meta($post->ID, "event_start", $_POST["event_start"]);
}
?>

Answers (5)

2010-07-19

Jignesh Patel answers:

Hello,

I guess you can use the jQuery datepicker to fill the date. Using that you can modify the format you want. When you post the form, you will get the date in that format. Save the date in DB in that format only. So having one DB field will make it easier to sort by it.

When displaying on the frontend, apply the explode() function & can separate the Date, Month and Year. Now you can display whatever way you want.


Mike McAlister comments:

Hi Jignesh,
These are good suggestions, but I'm looking for some actual working solutions here.

2010-07-19

Pippin Williamson answers:

Why not just use the Premium Events Calendar from Code Canyon. It is already built to do everything you need.

http://codecanyon.net/item/events-calendar-pro-wordpress-premium-plugin/109301


Mike McAlister comments:

Because this may be part of a wp theme I use in the future, and may sell. So I don't want the theme dependent on those plugins.

I know it's a great plugin though, I've used it on other projects.

2010-07-19

Utkarsh Kukreti answers:

<?php
//Event Meta Boxes //
add_action("admin_init", "add_event");
add_action('save_post', 'update_event_options');
function add_event(){
add_meta_box("event_details", "Event Info", "event_options", "event", "normal", "low");
}
function event_options(){
global $post;
$custom = get_post_custom($post->ID);
$event_start = $custom["event_start"][0];
?>
<table>

<tr>
<td><label>Event Start Day:</label></td>
<td><input name="event_start_day" value="<?php echo $event_start; ?>" /></td>
</tr>
<tr>
<td><label>Event Start Month:</label></td>
<td><input name="event_start_month" value="<?php echo $event_start; ?>" /></td>
</tr>
<tr>
<td><label>Event Start Year:</label></td>
<td><input name="event_start_year" value="<?php echo $event_start; ?>" /></td>
</tr>
</table>
<?php
}
// Saving Event Meta Boxes //
function update_event_options(){
global $post;
if(isset($_POST["event_start_day"])) update_post_meta($post->ID, "event_start_day", $_POST["event_start_day"]);
if(isset($_POST["event_start_month"])) update_post_meta($post->ID, "event_start_month", $_POST["event_start_month"]);
if(isset($_POST["event_start_year"])) update_post_meta($post->ID, "event_start_year", $_POST["event_start_year"]);

if(isset($_POST["event_start_day"]) && isset($_POST["event_start_month"]) && isset($_POST["event_start_year"]))
update_post_meta($post->ID, "event_start", sprintf("%04d-%02d-%02d", $_POST["event_start_year"], $_POST["event_start_month"], $_POST["event_start_day"]))
}
?>

2010-07-19

Rashad Aliyev answers:

Hello, contact with me please.

2010-07-22

Lawrence Krubner answers:

Just a reminder to all the experts on the site, we prefer it if the answers stay on the site, when possible. More details here:

[[LINK href="http://codewi.se/2010/07/21/conversations-offline-close-account/"]]http://codewi.se/2010/07/21/conversations-offline-close-account/[[/LINK]]


Mike McAlister comments:

Hi Lawrence,
I definitely didn't contact that user, as I know the site rules! However, no one seems to be hitting the mark here. It may be more work than I anticipated. How do I withdraw the question?

Mike


Lawrence Krubner comments:

Mike, thank you much for your consideration. If your time runs out and you do not get an answer, we will always refund your question, but since your question is new, I suspect assume you'll get more information.