Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I change the publish date based on a custom field?

Author uses a date picker to choose the date for journal entry. The date is used to make a list of all the users journal entries. I can order these posts in a list with a query and orderby. But, then Next and Previous don't match the list.

I have read here, codex, and Google how to attempt to fix this, none have worked so far.

Maybe the best way would be to set the publish date to the same as the custom field from the date picker?

Does anyone know how this can be done?

share|improve this question
    
If you are going to alter the publish date, why not just alter the publish date? I don't see the point of using a custom field at all. –  s_ha_dum May 21 '13 at 16:12
    
Users publish from a form on the front end. They will not have access to change publish date in the admin. They may add multiple journal entry in one day out of sequence. The only place this is a problem is Next and Previous. I could not find a function that would allow me to "order by" for the Next and Previous, so I thought changing the publish date would be best. So, I have 3 options: 1. remove Next and Previous. 2. Find away to "order by" custom field. 3. change publish date on the front end. Only the first one I'm confident I can do :) –  user8514 May 22 '13 at 16:56
    
Yes, those are most of your options. #2 (ordering the query) is easy, getting the next and previous links behave with a non-post_data order query is tricky. #3 may not be as hard as you think though. Post the code that processes your front end submission. –  s_ha_dum May 22 '13 at 17:04

1 Answer 1

I don't know how your front end form saves its data, but presumably it is using wp_insert_post and wp_update_post. If so, there is a filter called wp_insert_post_data that can be used to alter the post data before the post is inserted or updated.

function reset_post_date_wpse_100021($data,$postarr) {
  var_dump($data,$postarr); die; //debug
  // your code
  return $data;
}
/**
 * runs before new posts have IDs !!!
 */
add_filter('wp_insert_post_data','reset_post_date_wpse_100021',99,2);

The second parameter is your $_POST data plus the old database data with the $_POST data overwriting the old data where they conflict.

If you look at the var_dump of that $postarr value you should see your datepicker data. You need to chop up your datepicker data and overwrite the date values in the $data array.

The issue I see is that WordPress (and MySQL) uses 0000-00-00 00:00:00-- Y-m-d H:i:s-- as the datetime format. That is what you need to provide, but you don't mention a time component at all. At the very least tack 00:00:01 onto your date.

That should be all you need.

share|improve this answer
    
Thank You @s_ha_dum for helping me with this. –  user8514 May 24 '13 at 1:24
    
I am using 3 plugins for logged in subscribers to post from the front end. Contact Form 7, Date Picker for CF7, and Form to Post. It all works great. The custom field name is "date-11", I can format the date, but not the time. The code that needs to happen is: IF category - journal, replace 'post_date' with custom field 'date-11'. I found a few examples of how to do this, but not work and it may be as you say, the time is not being included. I have used 'wp_insert_post_data' to add text to the title. That code snip will not work with this. –  user8514 May 24 '13 at 1:53
    
You can make up the time. You obviously won't have the hour:minute:second precision but it should work. –  s_ha_dum May 24 '13 at 2:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.