How to change the order of posts?

Above view of several business people working at meeting

Well, order of posts is sorted by date of publication which is kind of natural, but as always, you are the owner of the website, so you have fully rights to change it and sort your posts as you want. How to do this?

First what you must do is adding an attribute to your posts. You can do it by adding a code in functions.php:

1
2
3
4
function wpzen_add_attributes_to_post() {
    add_post_type_support(‘post’, ‘page-attributes’);
}
add_action(‘init’, ‘wpzen_add_attributes_to_post’);

Okay, you’ve add a attribute but to WordPress it’s totally inert and that is why the next step is to another code.

function wpzen_change_post_order($query) {
    if($query->is_main_query()) {
        $query->set(‘orderby’, ‘menu_order’);
    }
}
add_action(‘pre_get_posts’, ‘wpzen_change_post_order’);
Okay, thanks to this code, your posts will be displayed according to the value of attribute “Order” which you can set as you want.