Created
July 13, 2012 14:07
-
-
Save ms-studio/3105070 to your computer and use it in GitHub Desktop.
Notes regarding the WP event manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Target: | |
******* | |
Perfect solution for a simple WP event manager | |
Needs: | |
****** | |
- we want the event dates to be linked to a normal WP post (or custom post type), so | |
that we have access to all other features, without any further work (be able to | |
attach images and documents, be able to use categories, taxonomies, custom fields, | |
featured images...). | |
- we want the plugin to be localizable in multiple languages. | |
- we should be able to restrict this function to a specific Custom Post Type, if we | |
want to. It's not necessary to have an admin interface for this, it's OK to paste | |
some code in our functions.php file. | |
Our idea for the implementation: | |
********************************* | |
- We will simply use custom fields, in order to save date and time in such a format: | |
2012-10-29 13:00. This will allow us to run specific queries and display the date | |
how we want, using strtotime() and date_i18n(). | |
- The design of the date-picker should be similar to the one that WP uses for the | |
publishing date. This keeps the interface clean and easy to understand. | |
Important: | |
************ | |
- We must have the option to show the time (or not), to show the end date (or not). | |
## RELATED: ## | |
************** | |
http://wordpress.stackexchange.com/questions/175/marking-future-dated-post-as-published | |
If you set the meta_value as a timestamp, you can order by the show time date still, | |
or select before / after a certain date: | |
*/ | |
$args = array( | |
'post_type' => 'events', | |
'meta_key' => '_show_time', | |
'meta_value' => strtotime( '+1 week' ), | |
'meta_compare' => '<', | |
'orderby' => 'meta_value' ); | |
$events = get_posts( $args ); | |
/* | |
This would get all "events" with a showtime no later then a week from the current date. | |
Note: The above is untested, but should work :) | |
### About meta_compare ### | |
************************** | |
Some good information about the meta_compare query: | |
http://core.trac.wordpress.org/ticket/9124 | |
*/ | |
query_posts( array( | |
'post_type' => 'beer', // Show posts from the beer post type | |
'paged' => $paged, | |
'meta_query' => array( | |
array( | |
'meta_key' => '_abv', | |
'meta_compare' => 'BeTwEeN', | |
'meta_value' => array(6,8) | |
), | |
array( | |
'meta_key' => '_ibu', | |
'meta_compare' => '>', | |
'meta_value' => 50 | |
), | |
) | |
) ); | |
/* | |
Another example, by Scribu: | |
http://scribu.net/wordpress/advanced-metadata-queries.html | |
Get all the products with a price greater than 100: | |
*/ | |
query_posts( array( | |
'post_type' => 'product', | |
'meta_key' => 'price', | |
'meta_value' => 100, | |
'meta_compare' => '>' | |
) ); | |
// Get all the products with a price between 100 and 200 and a description that | |
// doesn’t contain the string ’round’: | |
query_posts( array( | |
'post_type' => 'product', | |
'meta_query' => array( | |
array( | |
'key' => 'price', | |
'value' => array( 100, 200 ), | |
'compare' => 'BETWEEN', | |
'type' => 'numeric', | |
), | |
array( | |
'key' => 'description', | |
'value' => 'round', | |
'compare' => 'NOT LIKE' | |
) | |
) | |
) ); | |
/* | |
SEE ALSO: | |
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters | |
************** | |
## Some examples of strtotime() and date() ## | |
06-10-2011 14:28 | |
// 6 october 2011 2:28 pm | |
see : | |
http://php.net/manual/fr/function.strtotime.php | |
The strtotime() function parses an English textual date or time into a Unix timestamp | |
(the number of seconds since January 1 1970 00:00:00 GMT). | |
*/ | |
$date = "2011-10-05 14:28"; | |
echo $stamp = strtotime($date) . "<br />"; // outputs 1317904080 | |
echo date("d-m", $stamp); // | |
/* | |
WP-Date picker | |
## CODE ANALYSIS: ## | |
******************* | |
the relevant code for post dates is here: | |
/wp-admin/includes/meta-boxes.php | |
*/ | |
?> | |
<div class="misc-pub-section curtime misc-pub-section-last"> | |
<span id="timestamp"> | |
<?php printf($stamp, $date); ?></span> | |
<a href="#edit_timestamp" class="edit-timestamp hide-if-no-js" tabindex='4'><?php _e('Edit') ?></a> | |
<div id="timestampdiv" class="hide-if-js"><?php touch_time(($action == 'edit'),1,4); ?></div> | |
</div> | |
<?php | |
/* | |
NOTE: the function touch_time() does generate the time picker. | |
this function is located in | |
/includes/template.php | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UPDATE: did a few tests.
I created a custom field ("Date début"), into which I entered a date string such as :
2012-11-29
It's possible to retrieve this from WP in this way:
$date_debut = get_post_meta($post->ID, 'Date début', true);
echo date_i18n( "j F Y - H\hi", strtotime($date_debut));
Since my site locale is in French, the above function returns the following:
29 novembre 2011 - 00h00
So the functions work fine. For a date that has no timestamp, the date function will return 00h00.
If we want to enter a date using the custom field, it works if we write it like this:
2012-10-18 20:00
The same function as above will return:
18 octobre 2012 - 20h00
Cool! That's all we need to make the system work!