0

I used to use the published date to sort posts on my page. Now I am sorting the posts with a custom date/time field.

When using the published date I used the_date function, which only shows the date once if two posts are on the same day. For example:

March 14 
Headline for Post 1 
Headline for Post 2 (second post on the same day)

March 13
Headline for post 3 (only one post on this day)

March 12
Headline for post 4
Headline for post 5 (second post on same day)

However, when I use the custom date the date displays above each post. How can I make the custom date field not show when two posts have the same day? Thanks.

  • Had you looked at the_date() source? It's rather short and simple to implement to skip same days. – Rarst Mar 14 '14 at 21:53
  • It looks promising, but how to use with a custom field? – Mark Devlin Mar 14 '14 at 22:11
0

You can use date_query with is very usefull. In this snipet we get a week most viewed posts.

<?php 
                                        $args = array(
                                            'date_query' => array(
                                                array(
                                                    'year' => date('Y'),
                                                    'week' => date('W'),
                                                ),
                                            ),
                                           'post_type'    => 'post',
                                           'post_status'  => 'publish',
                                           'meta_key' => 'post_views_count',
                                           'orderby' => 'meta_value',
                                           'order' => 'DESC',
                                           'showposts' => '5'
                                        );
                                        $my_query = new WP_Query( $args );
                                        ?>

Here is more examples for use a date_query argument. http://www.hongkiat.com/blog/wordpress-date-query/

  • Does date_query work with custom fields? – Mark Devlin Mar 14 '14 at 22:09
  • As you can see, there is a meta_key. Yes, it works. But you need check the custom field meta key id not name, if you use a ACF. Field key name is like a fied_3433232 for example. – Foxsk8 Mar 14 '14 at 22:12
  • Thanks for the clarification. It looks like this method can show selections of posts, but will it replicate the_dates behavior of skipping the date on posts that are on the same day? – Mark Devlin Mar 14 '14 at 22:54
  • This won't work if the date is in a custom meta field as the_date depends on globals set up by setup_postdata() – s_ha_dum Mar 15 '14 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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