Sorting Article Order in WordPress Using the Post Feature

reorder blog posts to highlight blog posts

Many users just want to reorder blog posts to highlight blog posts as featured content. WordPress comes with a default feature to achieve this, it is called Sticky Post. With the Sticky Post feature, users can highlight articles on all other posts on their blog page.

How to make a sticky post

Just edit the blog post to pin it to the top. On the article editing screen, check the box next to the “Stick this post to the front page” choice further down the “Visibility” piece.

post feature

After that, click the “Update” button to save the changes. Now visit the website and see the selected article pinned to the top. Depending on the theme been used, sticky posts might be highlighted in different ways.

How to reorder blog posts using code?

Another way to reorder the article in WordPress is the use of code to modify the WordPress query (advanced). This method requires adding code to the WordPress website. For an advanced user, who wants to customize the publishing order, modify the default WordPress query.

For illustration, take a look at the code snippet below. It allows the exhibition of articles in chronological order (older articles first).

// function to modify default WordPress query
function wpb_custom_query ($ query) {
// Make sure we only modify the main query on the homepage
if ($ query-> is_main_query () &&! is_admin () && $ query-> is_home ()) {
// Set parameters to modify the query
$ query-> set ('orderby', 'date');
$ query-> set ('order', 'DESC');
}
}

// Hook our custom query function to the pre_get_posts
add_action ('pre_get_posts', 'wpb_custom_query');

This code just modifies the order by and order parameters in the default WordPress query. However, because some plugins or themes have modified the default query, this code may sometimes not work as expected. To solve this problem, use the following supress_filters parameter:

// function to modify default WordPress query
function wpb_custom_query ($ query) {
// Make sure we only modify the main query on the homepage
if ($ query-> is_main_query () &&! is_admin () && $ query-> is_home ()) {
// Set parameters to modify the query
$ query-> set ('orderby', 'date');
$ query-> set ('order', 'DESC');
$ query-> set ('suppress_filters', 'true');
}
}

// Hook our custom query function to the pre_get_posts

add_action ('pre_get_posts', 'wpb_custom_query');

The order by parameter provides many options. Please see the full list of options on the WP Query codex page.

You May Also Like

About the Author: BW

Leave a Reply

Your email address will not be published. Required fields are marked *