{"id":27342,"date":"2013-02-04T04:43:39","date_gmt":"2013-02-04T09:43:39","guid":{"rendered":"http:\/\/www.templatemonster.com\/help\/?p=27342"},"modified":"2017-08-15T06:46:19","modified_gmt":"2017-08-15T10:46:19","slug":"wordpress-how-to-sort-custom-posts-by-custom-field","status":"publish","type":"post","link":"https:\/\/www.templatemonster.com\/help\/wordpress-how-to-sort-custom-posts-by-custom-field.html","title":{"rendered":"WordPress. How to sort custom posts by custom field"},"content":{"rendered":"<p>\r\n<\/p>\r\n<p>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 &ldquo;Filterable 3 cols&rdquo; page where you&rsquo;d like  to sort the posts by custom field. In order to do this,<\/p>\r\n<div class=\"inline-banner\">\r\n<a href=\"https:\/\/www.templatemonster.com\/service-center\/\">\r\n<img src=\"\/help\/files\/banners\/inline\/Banner-3-(835-x100).jpg\" \/>\r\n<\/a>\r\n<\/div>\r\n<ol>\r\n<li>Log into your WordPress admin panel  and go to the menu <strong>Appearance -&gt;  Editor<\/strong>.<\/li>\r\n<p><a href=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_1.jpg\"><img loading=\"lazy\" src=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_1.jpg\" alt=\"\" title=\"wordpress_posts_in_alphabetical_order_sorting_1\" width=\"171\" height=\"144\" class=\"alignnone size-full wp-image-27343\" srcset=\"https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_1.jpg 171w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_1-150x126.jpg 150w\" sizes=\"(max-width: 171px) 100vw, 171px\" \/><\/a><\/p>\r\n<li>Look for the file used for your  portfolio page among the file names (templates) listed on the left. Since our  page is called &ldquo;Filterable 3 cols&rdquo;, the file we need is Filter Folio 3 cols  Page Template (<strong>page-Portfolio3Cols-filterable.php<\/strong>).  In your case this may be a different file name. Click on that to open its code.  Click on that name.<\/li>\r\n<p><a href=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_11.jpg\"><img loading=\"lazy\" src=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_11.jpg\" alt=\"\" title=\"wordpress_posts_in_alphabetical_order_sorting_1\" width=\"171\" height=\"144\" class=\"alignnone size-full wp-image-27344\" srcset=\"https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_11.jpg 171w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_11-150x126.jpg 150w\" sizes=\"(max-width: 171px) 100vw, 171px\" \/><\/a><\/p>\r\n<li>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.<\/li>\r\n<li>Copy the content of the file and  paste it to some file on your computer so that you always have a back-up copy.<\/li>\r\n<li>In the code you opened, look for the  following line\u00a0 using Ctrl +F<\/li>\r\n<pre class=\"brush:php\">\r\n$wp_query->query(\"post_type=portfolio&#038;paged=\".$paged.'&#038;showposts='.$items_count3);\r\n<\/pre>\r\n<li>Add\r\n<pre class=\"brush:php\">\r\nmeta_key=order1&#038;orderby=meta_value_num&#038;order=ASC\r\n<\/pre>\r\n<p>to your line<\/li>\r\n<p>so it will look like this:\u00a0\u00a0<\/p>\r\n<pre class=\"brush:php\">\r\n $wp_query->query(\"post_type=portfolio&#038;paged=\".$paged.'&#038;meta_key=order1&#038;orderby=meta_value_num&#038;order= ASC&#038;showposts='.$items_count3);\r\n <\/pre>\r\n<p>(where <strong>order1<\/strong> is the name of  your custom field, you can use any other name)<\/p>\r\n<li>Click <strong>Update File<\/strong> at the bottom to save your changes.\u00a0 You may check the page. No errors\u00a0 and no posts means you are doing everything  correctly.<\/li>\r\n<li>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 <strong>Portfolio<\/strong> menu and open the  post you want to come first. <\/li>\r\n<p><a href=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_3.jpg\"><img loading=\"lazy\" src=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_3-300x181.jpg\" alt=\"\" title=\"wordpress_posts_in_alphabetical_order_sorting_3\" width=\"300\" height=\"181\" class=\"alignnone size-medium wp-image-27345\" srcset=\"https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_3-300x181.jpg 300w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_3-150x90.jpg 150w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_3-1024x619.jpg 1024w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_3.jpg 1252w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\r\n<li>On the post page, scroll down till  you see the custom field block. If you cannot see it, you need to click on<strong> &ldquo;Screen Options&rdquo; \u00a0<\/strong>and check <strong>&ldquo;Custom Field&rdquo; <\/strong>there.<\/li>\r\n<p><a href=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_4.jpg\"><img loading=\"lazy\" src=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_4-300x195.jpg\" alt=\"\" title=\"wordpress_posts_in_alphabetical_order_sorting_4\" width=\"300\" height=\"195\" class=\"alignnone size-medium wp-image-27346\" srcset=\"https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_4-300x195.jpg 300w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_4-150x97.jpg 150w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_4.jpg 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\r\n<li>Click on <strong>Add New Custom Field<\/strong>. Under <strong>Name <\/strong>you need to add the name of the custom field. In our case, its <strong>order1<\/strong><\/li>\r\n<li>Under <strong>Value \u00a0<\/strong>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. <\/li>\r\n<p><a href=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_5.jpg\"><img loading=\"lazy\" src=\"\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_5-300x91.jpg\" alt=\"\" title=\"wordpress_posts_in_alphabetical_order_sorting_5\" width=\"300\" height=\"91\" class=\"alignnone size-medium wp-image-27348\" srcset=\"https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_5-300x91.jpg 300w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_5-150x45.jpg 150w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2013\/02\/wordpress_posts_in_alphabetical_order_sorting_5.jpg 822w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\r\n<li>Click on &ldquo;<strong>Update<\/strong>&rdquo; in the right column to save your changes and proceed to  adding the custom field to the rest of the posts.<\/li>\r\n<li>Now, if you go to the page with the  posts, the posts should be ordered by the values of <strong>order1<\/strong> custom field (ascending order).<\/li>\r\n<\/ol>\r\n<p>Feel free to check the detailed video tutorial below:<\/p>\r\n<p><a href=\"http:\/\/www.youtube.com\/watch?v=QEHC-ltXNIE?width=1280&amp;height=720\" rel=\"prettyPhoto\">WordPress. How to sort custom posts by custom field<\/a><\/p>\r\n<\/p>\r\n<\/p>\r\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial is going to show you how to sort custom posts of your WordPress template by custom field.<\/p>\n","protected":false},"author":30,"featured_media":27357,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[728],"tags":[789,907,1255,1298,2405],"_links":{"self":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/27342"}],"collection":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/users\/30"}],"replies":[{"embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/comments?post=27342"}],"version-history":[{"count":1,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/27342\/revisions"}],"predecessor-version":[{"id":82685,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/27342\/revisions\/82685"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/media\/27357"}],"wp:attachment":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/media?parent=27342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/categories?post=27342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/tags?post=27342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}