📜  wp order archive page post by title - PHP (1)

📅  最后修改于: 2023-12-03 15:35:44.769000             🧑  作者: Mango

WP Order Archive Page Post by Title - PHP

Have you ever wanted to order your WordPress archive pages alphabetically by post title instead of by date? Well, fear not! With a little bit of PHP code, you can easily achieve this.

Code
function order_archive_by_title($query) {
    if ($query->is_archive()) {
        $query->set('orderby', 'title');
        $query->set('order', 'ASC');
    }
}

add_action('pre_get_posts', 'order_archive_by_title');
Explanation

This code creates a function called order_archive_by_title which is hooked onto the pre_get_posts action. This ensures that the function will be called before the posts are retrieved from the database.

Within the function, we check if the current query is an archive page ($query->is_archive()). If it is, we set the orderby parameter to title and the order parameter to ASC to ensure that the posts are ordered alphabetically by post title in ascending order.

Finally, we add the function to the pre_get_posts action using the add_action function.

Conclusion

By using this simple code snippet, you can easily order your WordPress archive pages by post title. This can be especially useful for websites that have a large number of posts and want to make it easier for visitors to find what they're looking for.