Support

Account

Home Forums ACF PRO Exclude Posts by Date Reply To: Exclude Posts by Date

  • I’m a little surprised that works at all because you can’t nest post__not_in inside of meta_query.

    Do the standard “Posts” also have the date field? I’m assuming that this is not the case. If it is, then you’re query will not return any “posts” because that meta value is not set.

    What you need to do is a query on your events to get the list of posts that you don’t want to include

    
    $today = date('Ymd');
    $args = array(
      'post_type' => 'events',
      'posts_per_page' => -1,
      'meta_query' => array(
        array(
          'key' => 'event_date',
          'value' => $today,
          'compare' => '<'
        )
      )
    );
    $old_events = new WP_Query($args);
    $post__not_in = array();
    if ($old_events->have_posts()) {
      $post__not_in = wp_list_pluck($old_events->posts, 'ID');
    }
    

    now you can use what you code from the above for post__no_in

    
    query_posts(array(
      'post_type' => array('post','events'),
      'showposts' => 20,
      'post__not_in' => $post__not_in,
    ));