Use this code in functions.php to change the order of blog posts and/or filter blog ‘post items.
Sources
- https://help.elegantthemes.com/en/articles/4532770-how-to-change-the-default-sorting-order-of-posts-in-the-blog-module
- https://www.elegantthemes.com/blog/tips-tricks/how-to-re-order-your-blog-posts-with-2-methods
- https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
Change the sort order of blog posts
This code is only used if the CSS ID is set to ‘hws-post-order’ in Advanced Tab → CSS ID & Classes → CSS ID. Remove the line 2 and 6 if it should be applied to all blog modiles.
- Change the ‘order’ and ‘orderby’ value to change the order criteria, like date, ascending etc. See orderby parameters in the links above.
// START custom blog post order
function custom_blog_order($query, $args) {
if (isset($args['module_id']) && $args['module_id'] === 'hws-post-order') {
$query->query_vars['orderby'] = 'title';
$query->query_vars['order'] = 'ASC';
$query = new WP_Query($query->query_vars);
}
return $query;
}
add_filter('et_builder_blog_query', 'custom_blog_order', 10, 2);
// END custom blog post order
Code language: PHP (php)
Filter blogpost and change the sort order
This code filters blog posts on the custom ‘enddate’ field* being equal or larger than today.
// START custom blog post order with enddate filter
function custom_blog_order($query, $args) {
// Ensure that the current date is in the correct format.
if (isset($args['module_id']) && $args['module_id'] === 'hws-post-filter-order') {
$today = date('Ymd'); // ACF date format (bijv. 20251030)
// Add the meta query to retrieve only posts with enddate >= today
$meta_query = array(
array(
'key' => 'enddate',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERIC'
)
);
$query->query_vars['meta_query'] = $meta_query;
$query->query_vars['orderby'] = 'meta_value';
$query->query_vars['meta_key'] = 'enddate';
$query->query_vars['order'] = 'ASC';
$query = new WP_Query($query->query_vars);
}
return $query;
}
add_filter('et_builder_blog_query', 'custom_blog_order', 10, 2);
// END custom blog post order with enddate filter
Code language: PHP (php)
* You can add such a custom field with the ACF Advanced Custom Fields plugin:
- Add a field group
- Add a Date Picker field
- Field Label: the name that shows in the post editor
- Field Name: the name in de database which will be used in the filter code, in this case ‘enddate’
- Display Format: how the date should show in the editor, e.g. 31/10/2025 (d/m/Y)
- Return Format: how the date should return for use in functions, in this case 20251031 (Ymd)
- Set a week start and default if needed
- Link it to the post type.