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');
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.