- This topic is empty.
-
AuthorPosts
-
December 10, 2014 at 8:37 am #190523SeathParticipant
Hello,
I need to hide posts with custom date field. For example: I have custom post type with bunch of stuff in it. I am displaying all my posts on archive. Then each post have custom field “date” which is normally empty. When I add date to the date field in some post I need the post to show on the archive for next 2 weeks and then after 2 weeks be hidden from the archive.
I have try everything I can think of and failed. I would really appreciate any help.
Thank you.December 10, 2014 at 9:42 am #190528SenffParticipantIf the date is a custom field, and you want to show posts based on the value of that custom field (= nothing to do with the publication date), you’ll have to filter by that using something like this:
$myposts = query_posts (array( 'orderby'=>'meta_value', 'meta_key'=>'my-custom-date-field', 'order'=>'asc' ) ); while ( have_posts() ) : the_post(); echo '-'.the_title().'-'.get_field('my-custom-date-field').'<br>'; endwhile; wp_reset_query();
This will just do a query of all your posts and sort them based on the value of the custom field “my-custom-date-field” (from old to more recent).
Next step: to only show posts that have a custom date value that comes after today’s date, you have to include a meta query within your query:
$myposts = query_posts ( array( 'orderby'=>'meta_value', 'meta_key'=>'my-custom-date-field', 'order'=>'asc', 'meta_query'=> array( array( 'key'=>'my-custom-date-field', 'value'=>date('Ymd'), 'compare' => '>' ) ) ) );
And then finally, in order to filter it out to show only posts that have a date value that is between today and 14 days from now:
$myposts = query_posts ( array( 'orderby'=>'meta_value', 'meta_key'=>'my-custom-date-field', 'order'=>'asc', 'meta_query'=> array( array( 'key'=>'my-custom-date-field', 'value' => array(date('Ymd'), date('Ymd', strtotime('14 days'))), 'compare' => 'BETWEEN' ) ) ) );
Make sure that the date format of your custom field matches the date format of your query (in this case Ymd).
December 10, 2014 at 11:02 am #190532SeathParticipantThank you for the replay,
I am sorry I didn’t explain my self properly. When I add date to my custom field “way_end_date” I need the post to show on the front for 14 days then stop showing, but all other posts what don’t have any date in the custom field need to still show.For example I have post-1 post-2 and post-3 they all show in the front. now I add today’s date to post-2 and it will show for next 14 days then will be hidden from the front, but post-1 and post-3 continue to show.
I’m terrible in explaining whats in my head.
This is what I have now: it still doesn’t work!
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.