{"id":7915,"date":"2011-04-08T05:44:22","date_gmt":"2011-04-08T09:44:22","guid":{"rendered":"http:\/\/info.template-help.com\/?p=7915"},"modified":"2020-01-30T11:11:37","modified_gmt":"2020-01-30T16:11:37","slug":"how-to-display-posts-by-custom-fields","status":"publish","type":"post","link":"https:\/\/www.templatemonster.com\/help\/how-to-display-posts-by-custom-fields.html","title":{"rendered":"How to display posts by custom fields"},"content":{"rendered":"<p>\r\n<p>This tutorial will show you how to display posts by custom field values in WordPress.<\/p>\r\n<p class=\"attention\">Before you start this tutorial please make sure you learned how to <a href=\"\/help\/how-to-add-pages-in-wordpress.html\">add pages<\/a>, create page templates and learned how to use <a href=\"\/help\/wordpress-custom-fields-overview.html\">custom fileds<\/a> in WordPress. <\/p>\r\n<p>1. Access your theme directory (<strong>wp-content\/themes\/theme###<\/strong>)<\/p>\r\n<p>2. Create new page, create new page template and assign it to the page. <\/p>\r\n<p>3. Open the created page template file<\/p>\r\n<p> To display a posts by specific tag we&#8217;ll use the WordPress function <strong>&quot;query_posts()&quot;<\/strong><\/p>\r\n<p>&nbsp;<\/p>\r\n<p> The page template file content would be as follows:<\/p>\r\n<pre>\r\n&lt;?php\r\n\/*\r\nTemplate Name: Posts by Tag\r\n*\/\r\n?&gt;\r\n&lt;?php get_header(); ?&gt;\r\n\t\t&lt;div class=\"container\"&gt;\r\n\t\t\t&lt;div class=\"indent\"&gt;\r\n\t\t\t\r\n\t\t\t\t&lt;?php \r\n\t\t\t\t\r\n\t\t\t\tquery_posts(array(\r\n\t\t\t\t'meta_key' =&gt; 'your_custom_field_key',\r\n\t\t\t\t'meta_value' =&gt; 'your_custom_field_value'\r\n\t\t\t));\r\n\t\t\t\t\r\n\t\t\t\tif ( have_posts() ) while ( have_posts() ) : the_post();\r\n\t\t\t\t\r\n\t\t\t\t\t\techo '&lt;li&gt;';\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\tthe_title();\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\techo '&lt;\/li&gt;';\r\n\t\t\t\t\t\t\r\n\t\t\t\t endwhile; \r\n\t\t\t\t\r\n\t\t\t\twp_reset_query(); ?&gt;\r\n\t\t\t\t\t\r\n\t\t\t&lt;\/div&gt;\r\n\t\t&lt;\/div&gt;\r\n&lt;?php get_footer(); ?&gt;\r\n\r\nReplace the <strong>your_custom_field_key<\/strong> with custom field key and <strong>your_custom_field_value<\/strong> with custom key value<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>As you can see we inserted the <strong>&quot;<span class=\"brush:php\">query_posts<\/span>()&quot;<\/strong> function:<\/p>\r\n<pre>\r\n\tquery_posts(array(\r\n\t\t\t\t'meta_key' =&gt; 'your_custom_field_key',\r\n\t\t\t\t'meta_value' =&gt; 'your_custom_field_value'\r\n\t\t\t));\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>Activated the posts loop that will allow WordPress yto display posts:<\/p>\r\n<pre>\r\n\t\t\t\tif ( have_posts() ) while ( have_posts() ) : the_post();\r\n\t\t\t\t\r\n\t\t\t\t\t...\r\n                    \t\t\t\t\t\t\r\n\t\t\t\t endwhile; \r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>Added some HTML markup for the posts list. <\/p>\r\n<pre>\r\n\t\t\t\t\t\techo '&lt;li&gt;';\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\tthe_title();\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\techo '&lt;\/li&gt;';\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>And closed the <strong>&quot;<span class=\"brush:php\">query_posts<\/span>()&quot;<\/strong> function after the loop.<\/p>\r\n<pre>\r\n\twp_reset_query(); ?&gt;\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>You can also define the specific category for the posts display so the page will display the posts by custom fields from the specific category. To do this modify the query the gollowing way:<\/p>\r\n<pre>\r\n\tquery_posts(array(\r\n\t\t\t\t'meta_key' =&gt; 'your_custom_field_key',\r\n\t\t\t\t'meta_value' =&gt; 'your_custom_field_value',\r\n                'category_name' =&gt; 'your_category_name'\r\n\t\t\t));\r\n<\/pre>\r\n<\/p>\r\n<\/p>\r\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial will show you how to display posts by custom field values in WordPress. Before you start this tutorial please make sure you learned how to add pages, create page templates and learned how to use custom fileds in WordPress. 1. Access your theme directory (wp-content\/themes\/theme###) 2. Create new page, create new page template [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[247,728],"tags":[2564,789,894,890,896,873,592,2405],"_links":{"self":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/7915"}],"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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/comments?post=7915"}],"version-history":[{"count":1,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/7915\/revisions"}],"predecessor-version":[{"id":86009,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/7915\/revisions\/86009"}],"wp:attachment":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/media?parent=7915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/categories?post=7915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/tags?post=7915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}