Hot answers tagged

51

See this special note about using the `the_date' SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() or get_the_date()...


40

I faced similar issue in past because I modified my date function. And then posts were displaying date if only each post has different date otherwise it returned blank. Try adding <?php echo get_the_date(); ?> instead.


26

Why does it not show? When you look at the source of the the_date() function, then you will notice two globals: global $currentday, $previousday; And then there's a rule if there's a date to display ... or not. The check is similar to the one done with is_new_day(): if ( $currentday != $previousday ) { // show date // Set global $...


22

UPDATE December 23 2014 There is a better method using date_query property of WP_Query class: $args = array( 'post_type' => 'post', 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-image' ) ) ), 'cat' ...


21

I ran into the same problem several times, following changes worked for me in the past: while (have_posts()) : the_post(); //some html <li class="icon-date"><?php echo get_the_date( 'Y-m-d' ); ?></li> <li class="icon-time"><?php the_time( 'H:i:s' ); ?></li> Instead of the_date(), use get_the_date(). The only thing to be ...


17

Yes, you can. All you need is make a filter for wp_get_archives(); so it accepts post_type parameter: function my_custom_post_type_archive_where($where,$args){ $post_type = isset($args['post_type']) ? $args['post_type'] : 'post'; $where = "WHERE post_type = '$post_type' AND post_status = 'publish'"; return $where; } then call this: ...


17

the_date() prints the date only if the same date was not printed before. No, that's not consistent with other similar functions. But that’s how it worked in WordPress’ ancestor b2/cafelog, and backwards compatibility always trumps logic … :) To print the date always use get_the_date() <?php echo get_the_date(); ?> or <?php echo mysql2date( ...


17

Call wp_update_post() with a special value for 'post_date' and 'post_date_gmt': $time = current_time('mysql'); wp_update_post( array ( 'ID' => 123, // ID of the post to update 'post_date' => $time, 'post_date_gmt' => get_gmt_from_date( $time ) ) );


16

In addition to birgire's solution, as of WordPress 3.7, you can use Date parameters. Your arguments would look like this to filter posts from the last 7 days: $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', // Using the date_query to filter posts from last week '...


15

get_the_date must be used inside the Loop. For outside the loop use get_the_time. $posts = get_posts(array('numberposts'=>-1)); //Get all published posts foreach ($posts as $post){ echo get_the_time('Y-m-d', $post->ID); //Echos date in Y-m-d format. } Consider replacing 'Y-m-d' in this example with get_option('date_format') as this will display ...


15

First, your date format has to be in descending order from largest to smallest units, i.e.: year, month, day, hour, minute, second, etc., otherwise MySQL can't query or order on the field. In this example I use year - month - day: $today = date( 'Y-m-d' ); $args = array( 'post_type' => 'vehicle', 'meta_query' => array( array( ...


12

Alright, You can just hook into the filter pre_get_posts and check is_admin. Put this in your theme or plugin: function wpse_81939_post_types_admin_order( $wp_query ) { if (is_admin()) { // Get the post type from the query $post_type = $wp_query->query['post_type']; if ( $post_type == 'Videos') { $wp_query->set('orderby', 'date'...


12

You seemed to have solved everything but the issue with the time: found the issue %d should be %s. but I see that it saves the server time not the current timezone WordPress has a number of date/time related functions. In this case, it sounds like what you need is current_time(), which... Returns the blog's current local time in one of two formats, ...


12

What date was/is the post published? Media uploads are added to the folder when the post/page was published, not the upload date. Was the post originally published in Feb 2015? https://core.trac.wordpress.org/ticket/10752


11

Some countries use a Daylight Saving Time (DST): Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn. Sometimes there is no 2 am. Sometimes you get 2 am two times. To avoid cron jobs running twice or not at all WP needs the GMT (or more precise: UTC). UNIX time stamps cannot be used because they ...


11

The field user_registered is not a in the *_usermeta table, but the in *_users. Nor do there seem to be any appropriate hooks to alter the SQL statement when using WP_User_Query. It seems you will need to use a direct SQL statement. Another (minor) complication is that the registered date is stored as a date-time. function ...


10

Do not use the post_date field for anything it isn’t made for. Use a post meta field instead. The post_date is bound to post_date_gmt, you would get strange side effect even you could get an earlier date into that. So create post meta fields and query those per tax query. Ignore the default field. In answer to your comment: Do not use a taxonomy. ...


10

This is not really an answer, just an attempt to find the specific context for this problem. Please install the following plugin on your site, try to set the three dates and add your result to the second <pre> in the table below. /* Plugin Name: WPSE Sysinfo */ add_action( 'admin_footer', 'wpse_sysinfo' ); function wpse_sysinfo() { $bit = ...


10

Don't use the_date(), instead use the_time(). the_date returns the date only, the_time returns the date + the time. I don't know the reason why wordpress won't return more than one date when the_date is used in a loop. But it has to do with the fact that the value is the same. If you use the_time the value is never the same, therefore it always returns the ...


10

The post_date and post_date_gmt serves as the date that the post was created. For scheduled posts this will be the date on which the post is scheduled to be published. There is no reliable native method to determine the date when a scheduled post was added. For scheduled posts, you can try the post_modified or post_modified_gmt dates as this will correspond ...


9

get_current_user_id() give you the user id of the logged in user. And that is: you. You have to get all users: <?php $users = get_users(); foreach( $users as $user ) { $udata = get_userdata( $user->ID ); $registered = $udata->user_registered; printf( '%s member since %s<br>', $udata->...


9

Use the fourth parameter for get_post_time(): $time = get_post_time( 'F j, Y', // format TRUE, // GMT get_the_ID(), // Post ID TRUE // translate, use date_i18n() ); get_post_time() calls mysql2date() internally, and it passes the $translate argument through. In mysql2date() we find this: if ( $translate ) ...


9

Use get_query_var() to get the date parts: $year = get_query_var('year'); $monthnum = get_query_var('monthnum'); $day = get_query_var('day'); In wp_title() a call to get_query_var('m') is used for the month too, but I got always just a 0 as value even on an URL like /2008/09/05/. If you want to print the month name, use: $GLOBALS['wp_locale']-&...


8

Your question is pretty old, but I just wanted to add a real solution to your question. Here's a function that will return an array of years you have posts published in. You can put this function in functions.php or in a plugin or whatever you want. function get_posts_years_array() { global $wpdb; $result = array(); $years = $wpdb->...


7

The reason it's going to be infinite is that every time you save the post, it's calling change_year...which then calls wp_update_post ... which fires the save_post filter. After some review and research, I'm thinking that you should probably avoid the save_post filter. Try using this filter: http://codex.wordpress.org/Plugin_API/Filter_Reference/...


7

Yes, as you - so far - have no publish date. You could use $post->post_modified, which will always be the date of the latest modification to the post data. Debug: Try hooking into the filter and dump both vars: function date_dump_callback( $date, $d ) { echo '<pre>'; print_r( $date ); print_r( $d ); echo '</pre>'; return $date; } ...


7

Use date_i18n(): date_i18n( 'Y. F j.', strtotime( get_the_time( "Y-m-d" ) ) ); From the function’s description: Retrieve the date in localized format, based on timestamp. If the locale specifies the locale month and weekday, then the locale will take over the format for the date. If it isn't, then the date format string will be used instead. ...


7

Question and expectations While the literal form of this question is practical in context (year 1899) it is a little vague in theoretical sense. How old is old? How far into the past we might want to go? What about the future? Since WordPress had started out as blogging engine, in that contextual sense it evolved to handle following span of time: dates WP ...


7

There is no need to craft a custom SQL query in order to achieve this. Since version 4.1, WordPress's query classes have supported complex/nested meta queries. So you can craft a query like this: $args['meta_query'] = array( // Use an OR relationship between the query in this array and the one in // the next array. (AND is the default.) ...


6

WordPress lets you add custom cron schedules, which is normally what you'd want to do in this situation, in conjunction with wp_schedule_event(). But, they work based on intervals rather than specific dates/times. For instance, add_filter( 'cron_schedules', 'addCustomCronIntervals' ); function addCustomCronIntervals( $schedules ) { $schedules[ self::...


Only top voted, non community-wiki answers of a minimum length are eligible