If this tutorial is not what you were looking for, you still have any questions, suggestions or concerns - feel free to let us know. Please help us to serve you better!

Your Name

Your Email

Your Message (required)


Featured templates

WordPress. How to sort custom posts by custom field

This tutorial is going to show you how to sort custom posts of your WordPress template by custom field. For instance, your template comes with a “Filterable 3 cols” page where you’d like to sort the posts by custom field. In order to do this,

  1. Log into your WordPress admin panel and go to the menu Appearance -> Editor.
  2. Look for the file used for your portfolio page among the file names (templates) listed on the left. Since our page is called “Filterable 3 cols”, the file we need is Filter Folio 3 cols Page Template (page-Portfolio3Cols-filterable.php). In your case this may be a different file name. Click on that to open its code. Click on that name.
  3. Alternatively, you can locate the same file on your FTP. Copy it to your Desktop in any text editor and upload it back when you are done with your changes.
  4. Copy the content of the file and paste it to some file on your computer so that you always have a back-up copy.
  5. In the code you opened, look for the following line  using Ctrl +F
  6. $wp_query->query("post_type=portfolio&paged=".$paged.'&showposts='.$items_count3);
  7. Add

    to your line

  8. so it will look like this:  

     $wp_query->query("post_type=portfolio&paged=".$paged.'&meta_key=order1&orderby=meta_value_num&order= ASC&showposts='.$items_count3);

    (where order1 is the name of your custom field, you can use any other name)

  9. Click Update File at the bottom to save your changes.  You may check the page. No errors  and no posts means you are doing everything correctly.
  10. Now you need to add the custom field to the posts you are using on that particular page. In your WordPress admin go to the Portfolio menu and open the post you want to come first.
  11. On the post page, scroll down till you see the custom field block. If you cannot see it, you need to click onScreen Options”  and check Custom Fieldthere.
  12. Click on Add New Custom Field. Under Name you need to add the name of the custom field. In our case, its order1
  13. Under Value  you need to add 1 if you want this post to come first or, for example 8, if you have 8 posts on this page and you want it to come last.
  14. Click on “Update” in the right column to save your changes and proceed to adding the custom field to the rest of the posts.
  15. Now, if you go to the page with the posts, the posts should be ordered by the values of order1 custom field (ascending order).

Feel free to check the detailed video tutorial below:

WordPress. How to sort custom posts by custom field

  • Muhammad Morshed

    Thanks, Very helpful post :)

    No votes yet.
    Please wait...
Live Chat
We help you to choose the right product.
Start chat