How to Arrange WordPress Posts in Any Order
How to Arrange WordPress Posts in Any Order
Have you ever tried to change the standard order of posts on your site’s blog page? By default, posts are ordered based on the date that each post is published, and there’s no built-in way to change the order in which posts appear.
If you do want to change the post order, you have three options: change the post publish date, write some code to sort posts using a parameter other than the date of publication, or find a plugin that will do the job for you.
Changing the publish date is not a viable option for many blogs. So, in this article, we’ll look at the second and third options on the list. First, I’ll show you how to build a custom plugin to implement a custom post order. Second, we’ll take a look at a two plugins available from the WordPress plugin directory that can be used to create a custom post order.
This post assumes a certain level of WordPress programming knowledge. If you’ve never created a simple plugin or a page template before, you will struggle to follow along. If you do struggle to follow along, the following posts will help you learn what you need to know to understand the material in this tutorial:
- Creating Custom Page Templates in WordPress
- How to Create a WordPress Plugin
- An In-Depth Guide to Conquering WP_Query
If you aren’t interested in building a custom plugin you can skip to the list of plugins available from the WordPress plugin directory that make the process a lot easier.
In addition, I’ve pulled all of the code in this tutorial into a GitHub repo. If you’d like to see what the finished product should look like, you can view and download all of the code from GitHub.
Build Your Own Custom Post Order Plugin
There are two major steps to take to implement a custom post order:
- Add a custom field to posts that can be used as a basis for sorting the posts.
- Implement the custom sort order by modifying the main WordPress loop or building a custom loop and adding it to a sidebar widget or custom page template.
Let’s start by adding a custom field to the WordPress post editing screen. However, before doing that you’ll need to fire up your WordPress development environment, create a new plugin folder, and create a plugin file in that folder. If you want to see what my plugin’s structure looks like, you can see the finished product at GitHub.
Set Up the Custom Field
While you could just use the Custom Fields meta box in the post edit screen to add custom meta data to each post, I prefer to add a custom meta box and field right to the backend. That way, you can’t accidentally assign meta data to the wrong field.
The first step in adding a custom meta box to the backend is to create the meta box and add it to the post edit screen.
That bit of code, added to your plugin file will create the custom meta box.
You’ll notices that the callback function in the bit of code above is
'jpen_custom_post_order'. Let’s create that function next and add it to our plugin file. It will add a field to the meta box we just created.
That bit of code starts by setting a nonce. Next, the code creates a variable called
$current_pos and assigns the value of the current post sort order to that variable. Next, two paragraph elements create the visible content of the meta box field and the current value is echoed into the field if a current value exists. Here’s what the box will look like:
Finally, we need to store user input to the database. We can do that by adding this bit of code to our plugin:
That code first checks to make sure that the nonce has been set and that the user has permission to make changes to the post. If everything checks out, the post meta data is updated with the new custom post order value.
Display the Custom Field in the Admin
In the last section we added a custom meta box to the post edit screen and programmed it to store a numeric value. A little later we’ll use that numeric value to create a custom post order. However, before we get to that, we have another problem to solve.
As things stand, to see the current post sort order value we have to open each post and take a look at the custom meta box we just added to the post edit screen. That isn’t very convenient. Let’s add the custom sort order value to the admin post list so that we can quickly see the current post order value assigned to each post.
First, we need to add a custom column to the post list in the admin area. We can do that by adding this bit of code to our plugin:
Next, we need to pull up the custom post order value for each post and list it in the new column. That’s not too difficult, and we can do it by adding this function to our plugin file:
Great. Now, when we visit the blog post list in the admin we can easily see which posts have been assigned a custom sort order value.
Here’s how things are looking when we view the blog post list in the admin area:
Put the Custom Post Order to Good Use
Now that we’ve made it possible to assign a custom order to posts, it’s time to put that custom order to good use. However, before we can do that we’ll have to answer this question: “How do we want to use the custom sort order?”
There are several different ways you might want to implement the custom sort. Here are a few ideas:
- Sort all of your posts into a custom order and display the custom sorted list on your blog posts page. You probably wouldn’t want to do this on a busy blog, but if you use WordPress to host a series of instructional posts that and don’t add new posts frequently, this could be a valuable way to sort posts in any order.
- Create a curated list of posts and display them in the order of your choice using a custom page template. For example, you could curate the list to only include posts that also belong to a specific category and then sort them into whatever order you wish.
- Create a blog post list that begins with a few custom sorted posts and then includes all of the rest of your posts in their standard order.
Really, the sky is the limit. If you can think up a use for the custom sort order, and can figure out how to implement your idea, then it’s a viable idea. Let’s quickly walk through the three ideas above so you can see how each would be accomplished.
Replace Posts on the Blog Page with a Custom Sorted List
The easiest way to use the custom sort order is to replace the standard list of posts on your site’s blog page with the custom sorted list of posts. To do that, all you need to do is drop the following function into your plugin:
Keep in mind that this function will only turn up posts that have been assigned a custom sort order value. Any posts that haven’t been assigned a custom sort order value will not be displayed on your blog page. In other words, if you do this, you’re going to have to assign a custom sort order value to every post that you want to see displayed.
1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox - free!
Create a Curated List of Custom Sorted Posts
Creating a curated list of custom sorted posts will require the use of the
WP_Query class. What you will need to do is create a query that includes the parameter you wish to use to curate your list, and then also add the custom sort order to the query. Here’s what that might look like:
This query will first look for posts that belong to the category with the ID of 94. Next, it will pick out only those posts that have been assigned a custom post order value. Finally, it will sort the posts by the custom post order value.
Add Sorted Posts to the Top of the Blog Posts Lists
Another option would be to add the custom sorted posts to the top of the blog posts list, and then follow the custom sorted posts with the rest of the blog posts sorted in their usual way. Doing this is a bit complex and requires that you create two custom queries using the
The first query will get the custom sorted posts and display them according to their custom order value. However, we only want the list of custom sorted posts to appear on the first page of the blog, so we’ll have to wrap the entire first query in an if statement that tests whether or not we’re on the first page of blog posts.
The second query will grab all posts and sort them in the usual way, but skip over any posts that have been assigned a custom sort order value. In addition, in order to enable pagination of the posts pulled up by the second query we’ll have to perform some trickery with the global
Here’s one way we could combine two queries to produce the desired result:
You could use that set of queries to replace the standard query on your blog page in three steps:
- Create a home.php file as a copy of your parent theme’s index.php.
- Drop those queries in to replace the content loop.
- Upload the new home.php to your child theme’s root directory.
Follow those steps and when the blog page is displayed the new home.php will be used as the page template.
Plugins to Get the Job Done
If you aren’t a developer or don’t need the flexibility of a custom-coded solution, there are several plugins available from the WordPress Plugin Directory that make post sorting easy. However, many of the available options are poorly supported and irregularly updated. Let’s look at two options that are actively supported by their respective developers and receive frequent updates.
Post Types Order is active on more than 400,000 sites, regularly updated, and the developer is active in addressing support requests posted in the WordPress plugin directory. All of this means that the plugin has managed to retain a rating of 4.6 out of 5 stars on the strength of more than 150 user reviews.
The plugin can be used to create a custom sort of posts and custom posts types. To use the plugin, install and active it, and then visit the settings page which can be found at Settings > Post Types Order. Next, visit the admin list of posts you wish to sort and simply drag-and-drop the posts into the order you wish to see them displayed on the site front end. The next time you visit any post list on the site front end you’ll see that the order of the posts has been updated to match the order in the admin list.
Order Your Posts Manually is active on less than 1,000 WordPress websites. However, it has a solid user rating of 4.6 out of 5 stars, and the developer works to resolve every support request posted to the WordPress plugin directory. Taken together, that makes it a plugin worth considering.
To use the plugin, install it, activate it, and then go to Settings > Order Your Posts Manually. Configure a few options and click Order My Posts. Clicking that button will take you to Tools > Order Your Post Manually and generate a list of all of your posts. Simply drag and drop your posts into the order you wish to see them displayed and click Save Changes.
If you want to display posts in a specific order you have a few different options. The most flexible option is to build your own plugin and then manually implement the custom sort. However, if developing a custom solution isn’t a possibility, you can get the job done with an easy-to-use plugin from the WordPress directory.