Hot answers tagged

50

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.


29

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 $...


26

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 '...


18

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, ...


14

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->...


14

This will definitely work....It worked for me... $username = get_the_author_meta( 'login', $author_id ); $args = array( 'post_type' => 'any', 'orderby' => 'date', 'order' => 'DESC', 'suppress_filters' => true, 'tax_query' => array( array( '...


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


10

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']-&...


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 ...


8

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.) ...


7

Few months ago, I updated the Codex for get_users() and WP_User_Query, regarding the date_query support on the user's registration date, in WordPress 4.1+. Then I also added a simple example on how to find users that registered during the last 12 hours. But the current question is how to find users that registered today. We could simply use relative date ...


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 ...


6

You just need get current date and add it on data_query in wp_query, Look this: <?php $getdate = getdate(); $args = array( 'date_query' => array( array( 'year' => $getdate["year"] ), ), ); $query = new WP_Query( $args ); ?> and then use loop: <?php if ( $query->have_posts() ): while ( $query->...


6

The example above disables the ordering feature by clicking columns. Sortable & for multiple custom post types: function wpse_819391_post_types_admin_order( $wp_query ) { if ( is_admin() && !isset( $_GET['orderby'] ) ) { // Get the post type from the query $post_type = $wp_query->query['post_type']; if ( in_array( $...


6

The handy thing about how ACF stores dates as YYYYMMDD is you can treat them like integers and get a similar level of functionality as if you were using "true" dates. For example, to get all dates after 1st Jan 2011, use > 20110101. Those before June 1st 2012? < 20120601. And for your case, all dates within 2004? >= 20040101 && <= ...


5

EDIT -> while this answer still works for < WP4.4, since 4.4 support for Custom Post Types is now included in wp_get_archives() There finally is a simple, quick and easy solution for date based archives of Custom Post Types in WordPress! This has been a long standing issue that is recorded here in the WP Core Trac. It has yet to be solved but one of the ...


5

If you wish to get posts between two dates, then use the before and after parameters in the date_query parameter, $query_string = array( 'post_type' => 'post', 'date_query' => array( 'column' => 'post_date', 'after' => '2012-04-01', 'before' => '2012-04-30' ), 'tax_query' => array( array( 'taxonomy' =&...


5

You need the 2 digit month number that you want to query on and then use the code below. This should be easy with php (for example, see this post). In the code below $month is the number of the month in a 2 digit format, eg March would be 03. $start_date = date('Y'.$month.'01'); // First day of the month $end_date = date('Y'.$month.'t'); // 't' gets the ...


5

You should use date_i18n(): $timestamp = get_post_meta($post_to_edit->ID, '_single_date', true); $friendly_date = date_i18n( get_option('date_format'), $timestamp ); ?><input value="<?= $friendly_date ?>" name="_single_date" />


5

WordPress has a special function for translating dates, called date_i18n. General usage: echo date_i18n( $dateformatstring, $unixtimestamp, $gmt); Supposing you have German as your site's language this would be: echo date_i18n( 'j. F Y', false, false); You can also import the time format from the admin settings, like this: echo date_i18n(get_option('...


4

You can move the code from OP's answer into a 404 template filter and force WP to load the date.php file instead of 404.php. It'll still send the 404 header, but render the page with a different template. function wpd_date_404_template( $template = '' ){ global $wp_query; if( isset($wp_query->query['year']) || isset($wp_query->query['...


4

I would save an option on post save: add_action( 'save_post', function($id,$p) { if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX) || ($p->post_status === 'auto-draft') ) { return; } update_option('_last_site_update',$p->post_date); }, 1,2 ); ...


4

The following query retrieves the oldest post of a specified user/author: $user_id = 42; // or whatever it is $args = array( 'posts_per_page' => 1, 'post_status' => 'publish', 'author' => $user_id, 'orderby' => 'date', 'order' => 'ASC', ); $first_post = new WP_Query($args); if ($first_post->have_posts()) { $...


4

Needed to enter correct date format: Ymd or yyyy-mm-dd.


4

You can achieve this with the date_query parameters added in version 3.7. To modify the main query on your posts page before it is run and apply the date_query parameters, we use the pre_get_posts action: function historical_posts_list( $query ){ if( $query->is_home() && $query->is_main_query() ){ $date_query = array( ...


4

I needed the same info and just figured out by myself. You have to make a custom query to get the first _wp_old_date from the postmeta table as @dipak_pusti suggested. Run the following statement: global $wpdb; $post_id = 1234; $meta_key = '_wp_old_date'; $post_create_date = $wpdb->get_var( $wpdb->prepare( " SELECT MIN($wpdb->postmeta....


4

Here you go. This goes in functions.php This changes the time on your website everywhere. Now you can keep using <?php echo get_the_date(); ?> in your loop or theme files. // Relative date & time function wpse_relative_date() { return human_time_diff( get_the_time('U'), current_time( 'timestamp' ) ) . ' ago'; } add_filter( 'get_the_date', '...


4

You need to use a meta_query to query custom fields. If you are going to store date and time in custom fields for the purpose of ordering or comparisons, you will need to store it in the format of yyyy-mm-dd h:m:s or as a unix timestamp. If you have two custom fields start and end and you need to get posts between a cetain start and end date, you can try ...


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