Tagged: datepicker, extract values
- This topic has 11 replies, 7 voices, and was last updated 6 years, 3 months ago by
Mehdi Salem.
-
AuthorPosts
-
-
February 6, 2015 at 5:27 pm #3273
angeliqueMemberHello,
I have custom object “Event”. A custom box allows to enter a start date and an end date with the Piklist Date Picker.
On the Event archive page, I need to sort those Events by the custom “start date”. However, dates are saved in the format ‘M d, yy’ (which is fine for display).
What is the best way to sort elements by (datepicker) dates?Thanks,
Angélique -
February 6, 2015 at 7:29 pm #3274
JasonKeymasterNormally you’d be able to use the custom field parameters in WP_Query, but Piklist doesn’t store the dates in the necessary YYYY-MM-DD format. I’m not sure why. May be worth changing to make this possible!
-
February 6, 2015 at 9:48 pm #3275
SteveKeymaster@jason– Piklist stores the data the way you display it. Doesn’t make sense to store it differently.
@angelique– It’s very easy to manipulate the date with standard PHP functions. Here’s a post that should help. -
February 6, 2015 at 10:47 pm #3276
JasonKeymaster@Steve: That’s true, and does make sense. Still, it could be useful to store the date in one format and display it in another, as to make date queries possible. Could be a nice extension to the datepicker field.
-
February 7, 2015 at 12:00 pm #3279
angeliqueMemberThank you everyone 🙂
Well, it makes sense to store a date as a String if all you do is to display it. If you need those dates to do something, like comparing/sorting them, it’s a bit less convenient. I wouldn’t mind storing a datetime object and formatting it at display, but it’s not convenient with Piklist: the “dateformat” you choose for picklist is the format the date is going to be displayed in the html form after selecting the date in the calendar. Not super user friendly 🙂Anyway, this is the code I use the request sorting by start date:
/* Sort the events to appear sorted by starting date */ function get_events( $query ) { if ( is_post_type_archive('events') && $query->is_main_query()) { $query->query_vars['meta_key'] = 'starting_on'; $query->query_vars['orderby'] = 'meta_value'; $query->query_vars['order'] = 'ASC'; } return $query; } add_filter( 'pre_get_posts', 'get_events' );If I follow you, Steve, I should use a custom function here to sort my custom posts (= events) ?
Though if I do, each time the page is requested, if posts date is going to be processed for the sorting. Isn’t is consuming? -
February 8, 2015 at 11:49 am #3282
kattagamiMemberHello Angelique,
Why don’t you convert your String date in timestamp with some php functions and store this timestamp as an hidden custom field?
You could afterwards select your CPT on this timestamp custom field with WP_Query.
I did that in one of my project and it works like a charm.
If you need the code, tell me. I can recover it and post it for you. -
February 8, 2015 at 4:15 pm #3283
angeliqueMemberHello Kattagami,
It would be great if you could share the code! I’m not too sure how to proceed and it would help tremendously to have some code that already work as a reference! -
February 9, 2015 at 8:50 pm #3294
angeliqueMemberStill trying to figure out this thing.
I’m trying to turn the date into a timestamp, hooking on “save_post”. Basically, when the event is saved, the start-date and end-date are translated into timestamp and saved into the post-meta fields “timestamp-start-date” and “timestamp-end-date”.
In my metaboxes for events, I added two hidden fields.Now, the issue I have is:
– if I put the two hidden fields (named “timestamp-start-date” and “timestamp-end-date”), the timestamps are NOT saved (I wonder if they could be overridden with a null value after my function has just set them, but supposedly save_post is called AFTER the post has been saved).
– if I remove those hidden fields, the timestamps appear nicely in the database (but of course, disappear from the picklist form).I guess I could do without the hidden fields, but does that mean I need to destroy those timestamps when an event is deleted? Or does WP remove all “post_meta” associated with the deleted post?
Here is my code:
The hidden fields:
piklist('field', array( 'type' => 'hidden' ,'field' => 'start-date-timestamp' )); piklist('field', array( 'type' => 'hidden' ,'field' => 'end-date-timestamp' ));The hook to save the timestamp:
function save_events_meta( $post_id, $post, $update ) { $start_date = $_REQUEST['_post_meta']['event_dates']['start-date'][0]; $end_date = $_REQUEST['_post_meta']['event_dates']['end-date'][0]; if ( isset( $start_date ) ) { update_post_meta( $post_id, 'start-date-timestamp', strtotime($start_date)); } if ( isset( $end_date ) ) { update_post_meta( $post_id, 'end-date-timestamp', strtotime($end_date)); } } add_action( 'save_post_events', 'save_events_meta', 10, 3 );To sum-up: “save_events_meta” works if I don’t add the hidden field to the event metaboxes. Stop working if I put back the hidden fields.
Any help welcome 🙂
-
February 12, 2015 at 11:35 am #3304
kattagamiMemberHi,
I recover my code. In fact I didn’t use an hidden field, sorry for the wrong advice.
I just put this code in my metabox file :
/* Title: Date et lieu de l'événement Description: date et lieu Post Type: evenement Capability: administrator Context: normal Priority: high Order: 1 */ // Création d'un champ date piklist( 'field' , array( 'type' => 'datepicker', 'scope' => 'post_meta', 'field' => 'mr_evenement_date', 'label' => __( 'Date' , 'mr' ), 'description' => __( 'Cette information est obligatoire.' , 'mr' ), 'required' => true, 'attributes' => array( 'class' => 'evenement_date' ), 'options' => array( 'dateFormat' => 'd MM yy', 'firstDay' => '1', 'monthNames' => array( "janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre" ), 'dayNamesMin' => array( "Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa" ) ) ) ); // Création d'un champ texte lieu piklist( 'field' , array( 'type' => 'text', 'scope' => 'post_meta', 'field' => 'mr_evenement_location', 'label' => __( 'Lieu' , 'mr' ), 'attributes' => array( 'class' => 'evenement_location' ), 'columns' => '5', 'position' => 'wrap', ) ); $mr_evenement_date = get_post_meta( $post_id , 'mr_evenement_date' , true ); $mr_evenement_date_timestamp = get_post_meta( $post_id , 'mr_evenement_date_timestamp' , true ); if ( !empty( $mr_evenement_date ) ) { // Convert a French string date to timestamp /* Set local date to French. Needed fot strptime() */ setlocale ( LC_TIME, 'fr_FR.utf8' , 'fra' ); $mr_evenement_date = strptime( $mr_evenement_date , '%j %B %Y' ); $mr_evenement_date_timestamp_converted = mktime( 0 , 0 , 0 , $mr_evenement_date['tm_mon']+1 , $mr_evenement_date['tm_mday'] , $mr_evenement_date['tm_year'] + 1900 ); if ( empty( $mr_evenement_date_timestamp ) ) { add_post_meta( $post_id, 'mr_evenement_date_timestamp', $mr_evenement_date_timestamp_converted ); } else { update_post_meta( $post_id, 'mr_evenement_date_timestamp', $mr_evenement_date_timestamp_converted ); } }And the query to display my CPT by date is :
$args_query = array ( 'offset' => 0, 'post_type' => 'evenement', 'post_status' => 'public', 'meta_key' => 'mr_evenement_date_timestamp', 'orderby' => 'meta_value_num', 'order' => 'DESC', ); // The Query $the_query = new WP_Query( $args_query );Hope this code could help you 🙂
-
October 16, 2015 at 9:01 pm #4613
AnthonyMember@kattagami – This code above was a real help. Now that I am able to get the events to list with a little date stamp (see attachment). Is it possible to parse the_date() from the date picker to work in a fashion such as this?
<?php if (true == get_post_meta($the_query->ID, 'mr_evenement_date_timestamp', true) ) { ?> <p class="date"><?php echo get_post_meta($the_query->ID, 'mr_evenement_date_timestamp', true);?></p> <?phpI am trying to get the date to be consisting of just the day number and the month.
Attachments:
You must be logged in to view attached files. -
October 20, 2015 at 12:36 am #4633
KevinKeymasterHi Guys-
Sorry I just read this one again, for future reference its very easy to write some code for piklist to handle this. Check out the render_field method in includes/class-piklist-form.php. The following hooks for pre render of a field would allow you to convert a datetime stored in the system.
/** * piklist_request_field * Filter the request field * * @param array $field * * @since 1.0 */ $field = apply_filters('piklist_request_field', $field); /** * piklist_request_field_$scope_$field * Filter a specific request field * * The dynamic portions of the hook name, <code>$field['scope']</code> and <code>$field['field']</code>, * refer to the 'scope' and 'field' parameters, of an individual field. * * @param array $field * * @since 1.0 */ $field = apply_filters('piklist_request_field_' . $field['scope'] . '_' . $field['field'], $field);And the save hooks would allow you to format it back to a datetime after the save method.
/** * piklist_save_field * Fires after fields have been saved * * @param $type Field type. * * @since 1.0 */ do_action('piklist_save_field', $scope, $fields); /** * piklist_save_field-{$scope} * Fires after fields have been saved and is scope specific * * @param $type Field type. * * @since 1.0 */ do_action("piklist_save_field-{$scope}", $fields);Let me know if you guys have any questions.
Thanks,
Kevin
-
October 20, 2015 at 5:53 pm #4640
Mehdi SalemMemberHello Guys, I just remembered that I wrote a complete stable solution just with WordPress action hooks for this that works really well. You can include this in your plugin or theme’s function.php, just replace my field names and post types with yours. This module will insure that your date picker fields will be saved in timestamp format in the database so your plugins will be able to compare dates directly. It will also make sure that this data will be displayed as a clear date format wherever you want to display it, front & admin. Here is the code:
<?php if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly /** * START PIKLIST DATE PICKER ACTIONS & FILTERS: SAVE AS TIMESTAMP BUT DISPLAY AS DATE IN ADMIN EDIT */ // Change the meta data after a post is saved add_action( 'save_post', 'hdev_update_meta_data' ); function hdev_update_meta_data( $post_id ) { // If this is just a revision, don't do anything. if ( wp_is_post_revision( $post_id ) ) return; global $post; $post_type = $post->post_type; /* ******* Change piklist date picker saved date to timestamp format for easy date comparison when needed **** */ // Make sure any date will be saved in PDT LA Time timestamp format date_default_timezone_set('America/Los_Angeles'); /** USE YOUR DESIRED TIMEZONE HERE -- IMPORTANT TO AVOID YOUR DATE FROM BEING CHANGED TO A SLIGHTLY DIFFERENT ONE EACH TIME TIME YOU SAVE YOUR POST */ if($post_type =='new') // Apply changes only to desired post_types metadata { $listing_start_date_stamp = strtotime(get_post_meta($post_id, 'YOUR_listing-start-date', true)); $listing_end_date_stamp = strtotime(get_post_meta($post_id, 'YOUR_listing-end-date', true)); update_post_meta($post_id, 'YOUR_listing-start-date', $listing_start_date_stamp); update_post_meta($post_id, 'YOUR_listing-end-date', $listing_end_date_stamp); } if(in_array($post_type, array('page', 'post', 'YOUR_OTHER_POST_TYPES_HERE'))) // Apply changes only to desired post_types metadata { $event_listing_start_date_stamp = strtotime(get_post_meta($post_id, 'YOUR_event-listing-start-date', true)); $event_listing_end_date_stamp = strtotime(get_post_meta($post_id, 'YOUR_event-listing-end-date', true)); update_post_meta($post_id, 'YOUR_event-listing-start-date', $event_listing_start_date_stamp); update_post_meta($post_id, 'YOUR_event-listing-end-date', $event_listing_end_date_stamp); } /* *************************************************** */ } // Change the meta data just when displaying it on the admin edit page but keep it unchanged in the database add_filter( 'get_post_metadata', 'hdev_convert_timestamp_to_date', 99, 4 ); function hdev_convert_timestamp_to_date($metadata, $object_id, $meta_key, $single){ if(is_admin() && isset( $meta_key ) && in_array($meta_key, array('YOUR_listing-start-date', 'YOUR_listing-end-date', 'YOUR_event-listing-start-date', 'YOUR_event-listing-end-date'))) { // Remove this very same filter to avoid infinite loop remove_filter( 'get_post_metadata', 'hdev_convert_timestamp_to_date', 99 ); $meta_value = get_post_meta( $object_id, $meta_key, TRUE ); add_filter('get_post_metadata', 'hdev_convert_timestamp_to_date', 99, 4); // Convert meta value from timestamp to readable (MM d, yy) date format and return it if(hdev_is_timestamp($meta_value)) // Check to avoid passing a date to date() instead of a timestamp - case happens during above save_post hook on get_post_meta call while date not converted yet to timestamp... { return date("F d, Y", $meta_value); } } // Return original if the check does not pass return $metadata; } /** * Checks if a string is a valid timestamp. * * @param string $timestamp Timestamp to validate. * * @return bool */ function hdev_is_timestamp($timestamp) { $check = (is_int($timestamp) OR is_float($timestamp)) ? $timestamp : (string) (int) $timestamp; return ($check === $timestamp) AND ( (int) $timestamp <= PHP_INT_MAX) AND ( (int) $timestamp >= ~PHP_INT_MAX); } /** END PIKLIST DATE PICKER CUSTOM **********************************************/
-
-
AuthorPosts
- You must be logged in to reply to this topic.