Categories

Featured templates

WordPress. How to sort custom posts by custom field

Norman Fisher February 4, 2013
Rating: 5.0/5. From 1 vote.
Please wait...

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
    meta_key=order1&orderby=meta_value_num&order=ASC
    

    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

Awesome Wordpress Themes
This entry was posted in WordPress Tutorials and tagged custom, field, posts, sort, WordPress. Bookmark the permalink.

Submit a ticket

If you are still unable to find a sufficient tutorial regarding your issue please use the following link to submit a request to our technical support team. We'll provide you with our help and assistance within next 24 hours: Submit a ticket