• Hi everyone!

    I’ve made (with Advanced Custom Fields) a date picker where you can choose from which to which date you want to show a post on the site. For example I want a post to show from 01-01-2014 to 01-01-2015 and after the last date the post won’t be shown anymore.

    I’ve found a hack with the where filter, but I can’t seem to figure out how to use that function with variables.

    <?php
    // WP_Query arguments
    $args = array (
    	'post_type'              => 'vacature',
    	'pagination'             => false,
    	'posts_per_page'         => '3',
    );
    
    $start = get_field('online_vanaf');
    $end = get_field('online_tot');
    
    function filter_where( $where = '' ) {
    
    global $start;
    global $end;
    
    $where .= " AND post_date >= '$start' AND post_date < '$end'";
    return $where;
    }
    
    add_filter( 'posts_where', 'filter_where' );
    
    $vacatures = new WP_Query( $args );
    
    // The Loop
    if ( $vacatures->have_posts() ) {
    	while ( $vacatures->have_posts() ) {
    		$vacatures->the_post();
    
    ?>

    You can see my code above. I don’t get an error, WordPress just won’t show the posts anymore..
    Any help is welcome!

    Thanks in advance.

  • The topic ‘Show post when between two dates’ is closed to new replies.