Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$20
Collecting and Sorting A Date Via Custom Fields
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.
This is the function I will be using to sort the events:
<?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(); ?>
Below is my current method of collecting information from the meta boxes in the event post type (via functions.php).
<?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"]);
}
?>
Mike McAlister | 07/19/10 at 2:18am
Edit
(5) Possible Answers Submitted...
Note: Mike McAlister felt their question was unanswered, so we granted them a refund.
Note: Mike McAlister requested a refund. They offered no explanation.
If no one challenges a refund request, then they are automatically granted and proccessed after 48 hours. Admins of this site only review refund requests if someone challenges the request. If you are curious about how we handled previous refund requests, you may read over all refund requests and their challenges.
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
07/19/10
2:34amJignesh Patel says: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.- 07/19/10 2:36am
Mike McAlister says:Hi Jignesh,
These are good suggestions, but I'm looking for some actual working solutions here.
- 07/19/10 2:36am
-

Last edited:
07/19/10
2:47amPippin Williamson says: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/109301Previous versions of this answer: 07/19/10 at 2:47am
- 07/19/10 2:50am
Mike McAlister says: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.
- 07/19/10 2:50am
-

Last edited:
07/19/10
3:36amUtkarsh Kukreti says:<?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"]))
}
?> -

Last edited:
07/19/10
7:39am -
Last edited:
07/21/10
10:37pmLawrence Krubner says: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:
http://codewi.se/2010/07/21/conversations-offline-close-account/- 07/21/10 11:06pm
Mike McAlister says: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 - 07/21/10 11:47pm
Lawrence Krubner says: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.
- 07/21/10 11:06pm
This question has expired.
Current status of this question: Refunded
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
