- Web templates
- E-commerce Templates
- CMS & Blog Templates
- Facebook Templates
- Website Builders
WordPress. How to sort custom posts by custom field
February 4, 2013
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,
- Log into your WordPress admin panel and go to the menu Appearance -> Editor.
- 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.
- 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.
- Copy the content of the file and paste it to some file on your computer so that you always have a back-up copy.
- In the code you opened, look for the following line using Ctrl +F
- Add
meta_key=order1&orderby=meta_value_num&order=ASC
to your line
- 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.
- 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.
- On the post page, scroll down till you see the custom field block. If you cannot see it, you need to click on “Screen Options” and check “Custom Field” there.
- Click on Add New Custom Field. Under Name you need to add the name of the custom field. In our case, its order1
- 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.
- Click on “Update” in the right column to save your changes and proceed to adding the custom field to the rest of the posts.
- Now, if you go to the page with the posts, the posts should be ordered by the values of order1 custom field (ascending order).
$wp_query->query("post_type=portfolio&paged=".$paged.'&showposts='.$items_count3);
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)
Feel free to check the detailed video tutorial below: