Support

Account

Home Forums Front-end Issues Displaying posts using a custom date field Reply To: Displaying posts using a custom date field

  • You should not alter the template because that’s really not needed. You should alter the way that posts are ordered is to create a pre_get_posts filter https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts and add a meta_query to it.

    You would put this in your functions.php file

    
    function my_pre_get_posts($query=false) {
      if (is_admin() || !$query || !is_a($query, 'WP_Query') ||
        !$query->is_main_query()) {
        return;
      }
      if (isset($query->query_vars['post_type']) &&
          $query->query_vars['post_type'] == 'post') {
        $query->set('meta_key', 'start_date');
        $query->set('meta_type', 'DATE');
        $query->set('orderby', 'meta_value');
        $query->set('order', 'ASC');
      }
    }
    add_action('pre_get_posts', 'my_pre_get_posts');