Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress – Hide posts based on custom date field

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #190523
    Seath
    Participant

    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.

    #190528
    Senff
    Participant

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

    #190532
    Seath
    Participant

    Thank 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!

    http://codepen.io/seath/pen/dPMbJK

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.